在KendoUI网格中对自定义下拉列表进行排序

时间:2015-09-08 00:06:09

标签: jquery sorting kendo-ui kendo-grid

我正在使用KendoUI Jquery网格。我有一个自定义的组合框编辑器,它与一个数据源绑定。

我的问题是我可以完美地对这个网格的其他列进行排序,但是这个关于排序的列给出了意想不到的随机结果。任何人都可以帮助我如何排序这个专栏?代码附件。

{ field: "Status", editor: statusDropDownEditor, template: '#=GetStatusColor(Status.Text)#', width: "90px" }

var statusData = new kendo.data.DataSource({
        data: [
            {  Text: "New", Value: "a9d59cd8-f569-45c4-bfdd-05f41db9e2f3" },
            {  Text: "Ready", Value: "e4aa129f-44b6-44e3-b5da-2ecc5c5c20c0" },
            {  Text: "Query", Value: "50b1af07-71a0-462f-86ec-a164d43a9b65" },
            {  Text: "Cancelled", Value: "79ee44ea-bc39-4a71-ad6d-c47886d0f69b" }
        ]
    });

function statusDropDownEditor(container, options) {
        $('<input data-text-field="Text" data-value-field="Value" data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataTextField: "Text",
                dataValueField: "Value",
                dataSource: statusData
            });
    }

2 个答案:

答案 0 :(得分:1)

使用此链接中的示例代码解决了此问题。

http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/foreign-key-column-sorting-by-text

示例代码段                 

  <script>
    var categories = [
        { "value": 1, "text": "Beverages"},
        { "value": 2, "text": "Condiments"},
        { "value": 3, "text": "Confections"},
        { "value": 4, "text": "Dairy Products"},
        { "value": 5, "text": "Grains/Cereals"},
        { "value": 6, "text": "Meat/Poultry"},
        { "value": 7, "text": "Produce"},
        {"value": 8, "text": "Seafood"}];

    //create dictionary of text-values for the FKC
    var categoriesDict = {};
    for (var i=0; i<categories.length;i++) {
        categoriesDict[categories[i].value] = categories[i].text;
    }

    $(document).ready(function () {
      var dataSource = new kendo.data.DataSource({
        pageSize: 20,
        data: products,
        change: function(e) {
          if (!e.action) {
            this.data();
          }
        },
        schema: {
          model: {
            id: "ProductID",
            fields: {
              ProductID: { editable: false, nullable: true },
              ProductName: { validation: { required: true} },
              CategoryID: { field: "CategoryID", type: "number", defaultValue: 1 },
              UnitPrice: { type: "number", validation: { required: true, min: 1} }
            },
            //define calculated field:
            CategoryName: function() {
              return categoriesDict[this.get("CategoryID")];
            }
          }
        }
      });

      $("#grid").kendoGrid({
        dataSource: dataSource,
        groupable: true,
        pageable: true,
        sortable: true,
        height: 540,
        toolbar: ["create"],
        columns: [
          { field: "ProductName", title: "Product Name" },
          //bind column to the calculated field and specify custom editor:
          { field: "CategoryName()", width: "200px", title: "Category", editor: categoryDropDownEditor },
          { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "200px" },
          { command: "destroy", title: " ", width: "150px"}],
        editable: true
      });
    });


    function categoryDropDownEditor(container, options) {
      //specify the value field in the "data-bind" attribute
      $('<input required data-text-field="text" data-value-field="value" data-bind="value:CategoryID"/>')
      .appendTo(container)
      .kendoDropDownList({
        autoBind: false,
        dataSource: categories
      });
    }
  </script>

答案 1 :(得分:0)

默认下拉列表在值字段上排序。因此,您的下拉列表将按照guids排序。要解决此问题(如果可能),您可以重构代码以使用增量整数(1,2,3,4)作为数据源的值。我目前有同样的问题,我还没有找到一种方法来配置文本字段下拉列表的排序。