【问题标题】:kendo ui: how to remove a dataItem of a dataSource bound to some comboBox, inside combobox dataBound eventkendo ui:如何在组合框 dataBound 事件中删除绑定到某个组合框的数据源的数据项
【发布时间】:2013-12-15 18:58:47
【问题描述】:

我有一个网格,在其中的某个列中,我使用 columns.editor 函数创建了一个组合框编辑 UI。 我的目标是每次用户从组合框中选择一些值 - 同时填充新创建的网格记录 - 时,这个值是 从下一条记录的组合框的列表选项中删除。

我尝试过的事情之一如下所示:

function equipmentDropDownEditor(container, options) {
    var equipmentComboBox = $('<input id="equipmentDropDownEditor" required data-text-field="name" data-value-field="name" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoComboBox({
            autoBind: false,
            dataSource: equipmentTypesDS,
            dataBound: function(e) {

              var equipmentData = e.sender.dataSource.data();

              if(currentlyInsertedEquipmentTypes.length > 0){

                for(var i=0;i<currentlyInsertedEquipmentTypes.length;i++){

                    $.each( equipmentData, function( index, selectedEquipmentData ) {

                            if (selectedEquipmentData.name == currentlyInsertedEquipmentTypes[i]){

                                  var dataItem = e.sender.dataSource.at(index);
                                  console.log("dataItem: " + dataItem.name + " is being removed");
                                  e.sender.dataSource.remove(dataItem);

                            }
                    });
                }

              }

            }
        });
}

我创建了一个名为“currentlyInsertedEquipmentTypes”的全局数组变量,其中保存了所有用户已选择的值 (例如,如果用户在网格内创建了 2 条记录,并在第一个和“工作站”选项的组合框中选择了“笔记本电脑”选项 在第二个的组合框中 --> currentInsertedEquipmentTypes = ["laptop", "workstation"] )。

在组合框 dataBound 事件中,我检查用户是否已经选择了值(currentlyInsertedEquipmentTypes.length>0) 如果他有,我在绑定的数据源中找到相应的对象并将其删除,这样它就不会在下一条记录的组合框列表中可用。 即使发生数据项删除,这也是整个事情崩溃的地方。

我是否遗漏了删除数据项后应该做的事情?我应该以某种方式将数据源重新绑定到组合框吗? 任何帮助将不胜感激。

[编辑]

---- 组合框数据源代码

var equipmentTypesDS= new kendo.data.DataSource({

transport: {
    read: {
        url: "api/equipment_types",
        type: "GET",
        data: {
            //"equipment_category": 1
        },
        dataType: "json"
    }
},
schema: {
    data: "data",
    total: "total"
}

});

---剑道网格代码:

                    $("#popup_equipment").kendoGrid({  
                        dataSource: {
                            schema:{
                                model:{
                                    id: "equipment_type_id",
                                    fields:{
                                        equipment_type_id: { editable: false },
                                        name: { }, //validation: {required: true}, defaultValue: "LAPTOP", 
                                        items:{ type: "number", defaultValue:1, validation: { required: true, min: 1} }
                                    }
                                }
                            }
                        },
                        toolbar: ["create"],
                        columns: [
                            { field: "name", title: "εξοπλισμός", width: "300px", editor: equipmentDropDownEditor, template: "#=name#" },
                            { field: "items", title:"πλήθος", width: "80px"},
                            { command: ["destroy"], title: "&nbsp;", width: "100px" }
                        ],
                        //editable: "inline",//true,
                        editable:{confirmation: false},
                        scrollable: false,
                        selectable: false
                     });

[编辑 2]

$("#popup_equipment").kendoGrid({
    dataSource: {
        schema:{
            model:{
                id: "equipment_type_id",
                fields:{
                    equipment_type_id: { editable: false },
                    name: { }, //validation: {required: true}, defaultValue: "LAPTOP", 
                    items:{ type: "number", defaultValue:1, validation: { required: true, min: 1} }
                }
            }
        }
    },
    toolbar: ["create"],
    columns: [
        { field: "name", title: "εξοπλισμός", width: "60%", editor: equipmentDropDownEditor, template: "#=name#" },
        { field: "items", title:"πλήθος", width: "20%"},
        { command: ["destroy"], title: "&nbsp;", width: "20%" }
    ],
    editable:{confirmation: false},
    scrollable: false,
    selectable: false,
    save: function(e){
        console.log("GRID SAVE EVENT! ", e);
        var equipment_name = e.values.name;
        equipmentTypesDS.get(equipment_name).used = true;
        console.log("equipmentTypesDS", equipmentTypesDS);
        console.log("END OF GRID SAVE EVENT!");
    }
});




function equipmentDropDownEditor(container, options) {
    var equipmentComboBox = $('<input id="equipmentDropDownEditor" required data-text-field="name" data-value-field="name" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoComboBox({
            autoBind: false,
            dataSource: equipmentTypesDS,
        });
}


var equipmentTypesDS=  new kendo.data.DataSource({

    transport: {
        read: {
            url: "api/equipment_types",
            type: "GET",
            data: {
                //"equipment_category": 1
            },
            dataType: "json"
        }
    },
    schema: {
        data: "data",
        total: "total",
        model:{
            id: "name"
        }
    },
    filter: { field: "used", operator: "neq", value: true }
});

【问题讨论】:

    标签: combobox kendo-ui datasource


    【解决方案1】:

    我会建议一种不同的方法。而不是删除元素将其过滤掉。

    示例:我定义了一个包含城市列表(您的插入设备)的数据源,如下所示:

    var cityDS = new kendo.data.DataSource ({
        data : [
            { City : "Seattle", used : false },
            { City : "Tacoma", used : false },
            { City : "Kirkland", used : false },
            { City : "Redmond", used : false },
            { City : "London", used : false },
            { City : "Philadelphia", used : false },
            { City : "New York", used : false },
            { City : "Boston", used : false }
        ],
        schema : {
            model : {
                id : "City"
            }
        },
        filter: { field: "used", operator: "eq", value: false }
    });
    

    如您所见,我添加了一个名为used 的字段,它只是说明City 是否已被使用。我将它设置为这个数据源的id。另外,我设置了一个filter,表示我只想要那些used id 等于(eq)到false

    编辑器功能几乎是你的:

    function cityDropDownEditor(container, options) {
        var equipmentComboBox = $('<input required data-text-field="City" data-value-field="City" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoComboBox({
            autoBind: false,
            dataSource: cityDS
        });
    }
    

    但没有dataBound 或任何其他事件处理程序。

    最后在Grid时我保存了一条记录,我从filter那个城市列表中。比如:

    var grid = $("#grid").kendoGrid({
        dataSource: ds,
        editable  : "popup",
        pageable  : true,
        toolbar: [ "create" ],
        columns   :
        [
            { field: "FirstName", width: 90, title: "First Name" },
            { field: "LastName", width: 200, title: "Last Name" },
            { field: "City", width: 200, editor : cityDropDownEditor }
        ],
        save : function(e) {
            console.log("e", e);
            var city = e.model.City;
            cityDS.get(city).used = true;
        }
    }).data("kendoGrid");
    

    如果您在没有元素的情况下启动 Grid,这可能会起作用,否则您必须方便地初始化 used 字段。它可能需要一些额外的代码来处理更改 City 的情况,但根据您的描述,情况似乎并非如此。

    你可以在这里看到这个运行:http://jsfiddle.net/OnaBai/ZH4aD/

    【讨论】:

    • 您好 OnaBai,感谢您的快速回复。不幸的是,我在将您的解决方案应用于我的案例时遇到了问题。我的错我没有引用与问题相关的整个代码,我只是在上面的问题中添加了一些代码。您可以看到将绑定到网格组合框的数据源。我不是创建数据的人。我通过发出 API 请求来获取它们。根据我的检查,kendo 数据源文档没有提供向数据源添加新字段的方法。如果是这种情况,是否有另一种解决问题的方法?
    • 他们没有提到如何添加字段,因为在 JavaScript 中你不需要声明它们,你可以随时做。由于您没有收到used,您可以假设如果没有定义记录,那是因为没有使用City。检查我修改后的 JSFiddle 显示:jsfiddle.net/OnaBai/ZH4aD/2(请检查现在的条件是 neqtrue 匹配两者,如果它是假的以及它没有定义)。
    • 我按照您的示例进行操作,但是每次我按下向下箭头以进行选择时都会触发保存事件,因此对于位于 i 上方的每个项目,“used”属性都设置为 true最后在列表中选择。结果,尽管不应该过滤许多项目。在您的情况下,虽然不会触发保存事件..知道什么可能导致这种行为吗?非常感谢!
    • 我无法重现您所说的按下向下箭头时正在执行的“保存”。您使用的是哪种版本模式(弹出式、内联式、...)?你能分享一些重现你所说的代码吗?我的 JSFiddle 会发生在你身上吗?
    • 在 [EDIT 2] 下面你可以看到我的新代码。我使用内联编辑模式。从组合框中选择项目有两种方法。第一种是用鼠标打开列表并选择适当的项目。另一种是按下键盘的向下箭头并导航到正确的项目,将列表中的项目逐一传递。在第二种情况下,当我按下向下箭头时,会触发网格的保存事件,并且此代码 cityDS.get(city).used = true;被执行。因此,如果我想在列表中选择它的第五个项目,我为它上面的所有四个项目设置 used 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2011-11-17
    • 2012-10-27
    相关资源
    最近更新 更多