Kendo Grid: how to get the selected item from a Co

2020-04-23 05:39发布

问题:

I have a Kendo grid which I am using within Angular, and have a field with a combo box, that has the editor set to the following function...

 function comboCellTemplate(container, options) {
  var input = $('<input name="' + options.field + '" />')
  input.appendTo(container)
  var combobox = input.kendoComboBox({
    autoBind: true,
    filter: "contains",
    placeholder: "select...",
    suggest: true,
    dataTextField: "description",
    dataValueField: "code",
    dataSource: data,
  });

And data is a list of simple json objects...

[
  {code: 'code1', description: 'desc1'}
  {code: 'code2', description: 'desc2'}
[

Each field in the grid data is bound to the same objects (ie with a code and description field)

I a previous post, to get sorting and filtering working I need to bind a field to the display field...

 {
      field: "Category.description",
      title: "Category",
      editor: comboCellTemplate,
      template: "#=Category.description#"
  },

When I do this, the combo box seems to set the field of the grid to the code. How can I get this to set the grid data to the whole data object (ie the {code, description})

I have tried adding a on - change handler to do this

  input.on('change', function () {
    var val = input.val();              
            //var dataItem = input.dataItem();
    options.model.set(options.field, val + 'xx');
  });

but can't see how to get the "selected Item" from the combo

I don't seem to be able to find this in the help (in particular when using Angular)

Any help here would be greatly appreciated. regards, Peter

回答1:

I think you can simply add a change handler to the editor and set it from there:

function comboCellTemplate(container, options) {
    var input = $('<input name="' + options.field + '" />')
    input.appendTo(container)
    var combobox = input.kendoComboBox({
        autoBind: true,
        filter: "contains",
        placeholder: "select...",
        suggest: true,
        dataTextField: "description",
        dataValueField: "code",
        dataSource: data,
        change: function () {
            options.model.set(options.field, this.dataItem());
        }
    });
}


回答2:

Ok, I think I have got to the bottom of this (after lots of diving through the doco)

I could get everything to work after I discovered that you can give a column a "magical" compare function.

So using this, my field can go back to binding to the whole json object .. and add the sortable as follows...

{
  field: "Category",
  title: "Category",
  editor: comboCellTemplate,
  template: "#=Category.description#",
  sortable:{
        compare: function (a, b){
          return a.Category.description.localeCompare(b.Category.description);
        }

},

Now everything works exactly as I expect, and I don't need to do anything extra in the combobox. I hope this ("simple when you know how") tip can save someone else all the time it took me to find it.