Adding items to a Kendo multiSelect

2019-06-06 17:58发布

I've got a Kendo Grid and on its DataBound event, I want to add its data source to a Kendo MultiSelect. So, when the data from the grid is loaded, the below function is fired.

Here's what I have tried so far, without success, but from the code you can see what I intend:

function addToMultiSelect() {
    var multiSelect = $("#multiSelect").data("kendoMultiSelect");
    var grid = $("#grid").data("kendoGrid");
    var places = grid.dataSource._data;

    for (var i = 0; i < places.length; i++) {
        var row = instPlaces[i];

        var id = row.Id;
        var mediumDescription = row.MediumDescription;
        alert(id + " - " + mediumDescription);

        multiSelect.dataSource.insert(i, { text: mediumDescription, value: id })
    }
    multiSelect.dataSource.read();
}

The alert properly shows the id and description of all the datagrid's items, so, I'm getting the data from the grid right.

I add them to the multiSelect's datasource, the last line is to refresh it and show the new data, but it doesn't. The multiSelect remains empty.

1条回答
倾城 Initia
2楼-- · 2019-06-06 18:17

Try the following:

function addToMultiSelect() {
    var multiSelect = $("#multiSelect").data("kendoMultiSelect");
    var grid = $("#grid").data("kendoGrid");
    var places = grid.dataSource.data();

    // Read original data content
    var multiData = multiSelect.dataSource.data();
    for (var i = 0; i < places.length; i++) {
        ...
        // insert new element into copy of multiselect data
        multiData.push({ text: mediumDescription, value: id })
    }
    // Write back the modified data
    multiSelect.dataSource.data(multiData);
}

See the following code example:

$(document).ready(function() {
  function addToMultiSelect(e) {
    var grid = this;
    var data = this.dataSource.data();

    var multiData = [];
    for (var i = 0; i < data.length; i++) {
      multiData.push({ text: data[i].ShipName, value: i });
    }
    multi.dataSource.data(multiData);
  }

  var multi = $("#multi").kendoMultiSelect({
    dataSource:  {
      data: [
        { text: "ship 1", value: 1 },
        { text: "ship 2", value: 2 },
        { text: "ship 3", value: 3 },
        { text: "ship 4", value: 4 }
      ]
    },
    dataTextField: "text",
    dataValueField: "value"
  }).data("kendoMultiSelect");

  $("#grid").kendoGrid({
    dataSource: {
      type: "odata",
      transport: {
        read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
      },
      schema: {
        model: {
          fields: {
            OrderID: { type: "number" },
            Freight: { type: "number" },
            ShipName: { type: "string" },
            OrderDate: { type: "date" },
            ShipCity: { type: "string" }
          }
        }
      },
      pageSize: 20,
      serverPaging: true,
      serverFiltering: true,
      serverSorting: true
    },
    dataBound: addToMultiSelect,
    height: 550,
    columns: [
      "OrderID",
      "Freight",
      {
        field: "OrderDate",
        title: "Order Date",
        format: "{0:MM/dd/yyyy}"
      },
      {
        field: "ShipName",
        title: "Ship Name"
      }, 
      {
        field: "ShipCity",
        title: "Ship City"
      }
    ]
  });
});
html { font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />

<script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>

Ship Names copied from Grid DataSource: <input id="multi"/>
Grid
<div id="grid"></div>

查看更多
登录 后发表回答