javascript - Kendo 网格弹出编辑模式不显示组合框数据

标签 javascript kendo-ui grid popup edit

当我在弹出窗口中时,我在下拉列表中显示组合框数据时遇到问题 我的剑道网格中的编辑模式。当网格中的可编辑参数更改为“内联”时,组合框的行为就像它应该的那样。我认为问题出在自定义弹出模板上,但多次更改仍然没有结果。

这是 .cshtml 文件中的脚本:

<script id="popup_editor" type="text/x-kendo-template">
    <label for="name">Page Name</label>
    <input name="name"
           data-bind="name"
           data-value-field="id"
           data-text-field="name"
           data-role="combobox" />
</script>

这是 JavaScript:

var griddata = new kendo.data.DataSource({
    transport: {
        read: {
            url: serviceRoot + "Approval/Get",
            type: "POST",
            contentType: jsonType,
            cache: false
        },
        destroy: {
            url: serviceRoot + "Approval/Delete",
            type: "PUT",
            complete: function(e) {
                refreshData();
            }
        },
        create: {
            url: serviceRoot + "Approval/Create",
            type: "PUT",
            complete: function(e) {
                refreshData();
            }
        },
        update: {
            url: serviceRoot + "Approval/Inline",
            type: "PUT",
            complete: function(e) {
                refreshData();
            }
        }
    },
    pageSize: 10,
    serverPaging: true,
    serverFiltering: true,
    serverSorting: true,
    scrollable: true,
    height: 700,
    schema: {
        data: "list",
        total: "total",
        model: {
            id: "id",
            fields: {
                id: { editable: false, nullable: false },
                name: { editable: true, nullable: false, validation: { required: true }, type: "string" },
                keyName: { editable: true, nullable: false, validation: { required: true }, type: "string" },
                countryName: { editable: true, nullable: false, validation: { required: true }, type: "string" },
            }
        }
    }
});

$("#grid").kendoGrid({
    dataSource: griddata,
    selectable: "row",
    allowCopy: true,
    scrollable: true,
    resizable: true,
    reorderable: true,
    sortable: {
        mode: "single",
        allowUnsort: true
    },
    toolbar: [{ name: "create", text: "Create New Content" }}],
    edit: function(e) {
        if (e.model.isNew() === false) {
            $('[name="PageName"]').attr("readonly", true);
        }
    },
    columns: [
        { field: "id", hidden: true },
        { field: "name", title: "Page Name", editor: PageNameComboBoxEditor, width: "200px" },
        {
            command: [
                { name: "edit" },
                { name: "destroy" }
            ],
            title: "&nbsp;",
            width: "250px"
        }
    ],
    editable: {
        mode: "popup",
        template: kendo.template($("#popup_editor").html())
    },
    pageable: {
        refresh: true,
        pageSizes: [5, 10, 15, 20, 25, 1000],
        buttonCount: 5
    },
    cancel: function(e) {
        $("#grid").data("kendoGrid").dataSource.read();
    }
}); 

function PageNameComboBoxEditor(container, options) {
    ComboBoxEditor(container, options, "name", "id", "ApprovalPage/Get", options.model.id, options.model.name);
}
function ComboBoxEditor(container, options, textfield, valuefield, url, defaultid, defaultname) {
    $("<input required data-text-field=\"" + textfield + "\" data-value-field=\"" + valuefield + "\" data-bind=\"value:" + options.field + "\"/>")
        .appendTo(container)
        .kendoComboBox({
            autoBind: false,
            dataTextField: textfield,
            dataValueField: valuefield,
            text: defaultname,
            value: defaultid,
            select: function(e) {
                var dataItem = this.dataItem(e.item);
                var test = dataItem;
            },
            dataSource: {
                transport: {
                    read: {
                        url: serviceRoot + url,
                        type: "GET"
                    }
                }
            }
        });
}

任何方向将不胜感激!

最佳答案

首先,我注意到您有拼写错误和一些双重初始化,并且指定的值不同,这会导致问题(不确定这是否是您的问题,所以请尝试删除它),

<input name="name"
data-bind="name" -> typo maybe? no data-binding declaration like these
data-value-field="id" -> double init, you have it on your ComboBoxEditor function dataValueField: valuefield,
data-text-field="name" -> double init, you have it on your ComboBoxEditor function dataTextField: textfield,
data-role="combobox" />

但要使其正常工作,我通常会自定义编辑功能来为mode: popup声明kendo小部件,如下所示:

<!DOCTYPE html>
<html>

<head>
  <base href="http://demos.telerik.com/kendo-ui/grid/editing-popup">
  <style>
    html {
      font-size: 12px;
      font-family: Arial, Helvetica, sans-serif;
    }
  </style>
  <title></title>
  <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.429/styles/kendo.common-material.min.css" />
  <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.429/styles/kendo.material.min.css" />
  <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.429/styles/kendo.dataviz.min.css" />
  <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.429/styles/kendo.dataviz.material.min.css" />

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

<body>
  <div id="example">
    <div id="grid"></div>
    <script id="popup_editor" type="text/x-kendo-template">
      <div>
        <label for="name">Page Name</label>
        <input id="combo_box" name="name" data-role="combobox" />
      </div>
    </script>
    <script>
      $(document).ready(function() {
        var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
          dataSource = new kendo.data.DataSource({
            transport: {
              read: {
                url: crudServiceBaseUrl + "/Products",
                dataType: "jsonp"
              },
              update: {
                url: crudServiceBaseUrl + "/Products/Update",
                dataType: "jsonp"
              },
              destroy: {
                url: crudServiceBaseUrl + "/Products/Destroy",
                dataType: "jsonp"
              },
              create: {
                url: crudServiceBaseUrl + "/Products/Create",
                dataType: "jsonp"
              },
              parameterMap: function(options, operation) {
                if (operation !== "read" && options.models) {
                  return {
                    models: kendo.stringify(options.models)
                  };
                }
              }
            },
            batch: true,
            pageSize: 20,
            schema: {
              model: {
                id: "ProductID",
                fields: {
                  ProductID: {
                    editable: false,
                    nullable: true
                  },
                  ProductName: {
                    validation: {
                      required: true
                    }
                  },
                  UnitPrice: {
                    type: "number",
                    validation: {
                      required: true,
                      min: 1
                    }
                  },
                  Discontinued: {
                    type: "boolean"
                  },
                  UnitsInStock: {
                    type: "number",
                    validation: {
                      min: 0,
                      required: true
                    }
                  }
                }
              }
            }
          });

        dataSource.fetch();

        $("#grid").kendoGrid({
          dataSource: dataSource,
          pageable: true,
          height: 550,
          toolbar: ["create"],
          editable: {
            mode: "popup",
            template: kendo.template($("#popup_editor").html())
          },
          edit: function(e) {
            $("#combo_box").kendoComboBox({
              autoBind: false,
              dataTextField: 'ProductName',
              dataValueField: 'ProductID',
              filter: "contains",
              text: e.model.ProductName,
              value: e.model.ProductID,
              dataSource: ({
                type: "jsonp",
                serverFiltering: true,
                transport: {
                  read: {
                    url: crudServiceBaseUrl + "/Products",
                    dataType: "jsonp"
                  },
                }
              })

            });
          },
          columns: [{
            field: "ProductName",
            title: "Product Name"
          }, {
            field: "UnitPrice",
            title: "Unit Price",
            format: "{0:c}",
            width: "120px"
          }, {
            field: "UnitsInStock",
            title: "Units In Stock",
            width: "120px"
          }, {
            field: "Discontinued",
            width: "120px"
          }, {
            command: ["edit", "destroy"],
            title: "&nbsp;",
            width: "250px"
          }],

        });
      });
    </script>
  </div>


</body>

</html>

关于javascript - Kendo 网格弹出编辑模式不显示组合框数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897541/

相关文章:

c++ - 将网格内的数字转换为其对应的 x,y 坐标

javascript - 将过滤器的值作为 Angular 中的 js 函数的参数传递

javascript - 如何通过来自 Silverlight 的 CRM2011 上的 javascript 连接到保存方法

javascript - 如何删除 KnockOut 分页网格的行?

kendo-ui - 如何获取 selectedRow kendo UI 的索引

kendo-ui - Kendo Treeview 拖放总是父级的最后位置

css - ExtJS4 中网格面板的颜色图例

javascript - javascript在单线程时如何在请求后接收回调?

javascript - DIV 自动滚动

javascript - 检查剑道网格中的特定复选框