javascript - Adding items to a Kendo multiSelect - Stack Overflow

I've got a Kendo Grid and on its DataBound event, I want to add its data source to a Kendo MultiSe

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.

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.

Share Improve this question edited Jan 21, 2015 at 11:59 chiapa asked Jan 21, 2015 at 11:18 chiapachiapa 4,41211 gold badges70 silver badges108 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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./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./2014.3.1316/styles/kendo.mon.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic./2014.3.1316/styles/kendo.default.min.css" />

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305060a4418771.html

相关推荐

  • javascript - Adding items to a Kendo multiSelect - Stack Overflow

    I've got a Kendo Grid and on its DataBound event, I want to add its data source to a Kendo MultiSe

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信