Kendo Grid: Setting Model fields on combox selection breaks the combobox up/down arrow scrolling

[亡魂溺海] 提交于 2019-12-12 03:07:05

问题


this follows in from another post to do with setting a grid's selected fields when we have a data source field (and combo fields) with a json object (as opposed to a simple string).

So if we look at the change event handler for the following combox...

function createCombo(container, options, data) {
  var input = $('<input name="' + options.field + '" />')
  input.appendTo(container)
  input.kendoComboBox({
  autoBind: true,
  filter: "contains",
  placeholder: "select...",
  suggest: true,
  dataTextField: "display",
  dataValueField: "rego",
  dataSource: data,
  change: function () {                     
    var dataItem = this.dataItem();
    var dataField = options.field.split('.');

     var fieldName = dataField[0];          
     options.model.set(fieldName + '.rego', dataItem.rego);
     options.model.set(fieldName + '.display', dataItem.display);      
    }
 });
 }

I am setting my 2 fields as follows...

    options.model.set(fieldName + '.rego', dataItem.rego);
    options.model.set(fieldName + '.display', dataItem.display);      

(each item in the combo, and the grid data source has a json object with a 'rego' and 'display' field, see full example here.

This seemed to work exactly as I wanted, but I had just had someone point out to me that when you scroll the combobox with the up/down arrow keys, it just seems to toggle between 2 values in the list, as opposed to iterating through all the items. If I remove the 2 'options.model.set' statements, the combo then behaves.

I am really hoping there is a work around this, but everything I Have tried makes no different.

If there were any suggestion to finish this off, it would be greatly appreciated!

Thanks in advance for any help


回答1:


Since you're modifying the model manually, you should to remove the name=... attribute from the input (otherwise the grid will also modify the model; you could also use name="car.rego" - it has to be the value field - and then not set the combobox value in the config) and also only call set for the last change you make on the model (otherwise, the grid's save event will get triggered twice, once with invalid data).

So you editor would look like this:

function createCombo(container, options, data) {
    var dataField = options.field.split('.');
    var fieldName = dataField[0];

    var input = $('<input/>')
    input.appendTo(container)
    input.kendoComboBox({
        autoBind: true,
        filter: "contains",
        placeholder: "select...",
        suggest: true,
        dataTextField: "display",
        dataValueField: "rego",
        dataSource: data,
        value: options.model[fieldName].rego,
        change: function (e) {
            var dataItem = this.dataItem();
            options.model[fieldName]['rego'] = dataItem.rego;
            options.model.set(fieldName + '.display', dataItem.display);
        }
    });
}

Also, your data should be consistent (in one DS, you use "C1" as rego, in the other "CAR1").

(demo)



来源:https://stackoverflow.com/questions/28535116/kendo-grid-setting-model-fields-on-combox-selection-breaks-the-combobox-up-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!