kendo ui - 数据列模板函数在网格中未定义

时间:2014-06-19 13:03:22

标签: javascript jquery kendo-ui kendo-mvvm

我有两个文件:customer.jsadd-customer-template.htmladd-customer-template.html上有一个网格,如下所示。

<div id="leadsGrid"  data-role="grid" 
                                                         data-bind="source: leadsDS"                                                     
                                                         date-scrollable="true"
                                                         data-editable="popup"
                                                         data-toolbar="['create']"
                                                         data-columns='[                                                                
                                                            {   
                                                                field: "salesPerson", title: "Sales Person", 
                                                                editor: "salesPersonDropDownEditor", 
                                                                template: "#= getSalesPersonName(salesPerson)#"
                                                            }, 
                                                            {field: "date", title: "Date", format: "{0:MM-dd-yyyy}"}, 
                                                            {field: "expectedDate", title: "Expected Date", format: "{0:MM-dd-yyyy}"}, 
                                                            {field: "expectedIncome", title: "Expected Income", format: "{0:c}"},
                                                            {field: "details", title: "Details"},
                                                            {field: "description", title: "Description"},
                                                            {command: ["edit", "destroy"], title: "&nbsp;"}]'>
                                    </div>

customer.js有2个函数salesPersonDropDownEditorgetSalesPersonName,如下所示。

var salesPersonDropDownEditor = function(container, options) {
              $('<input data-bind="value:' + options.field + '"/>')
                .appendTo(container)
                .kendoDropDownList({      
                  dataTextField: "salesPersonName",
                  dataValueField: "salesPersonID",
                  dataSource: new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: "../public/js/salesPersons.json",
                                    dataType: "json"
                                }
                            }
                        }) 
                });
            }

    var getSalesPersonName= function(salesPersonID) {
              for (var idx = 0, length = customerAdd.salesPersonData.length; idx < length; idx++) {
                  if (customerAdd.salesPersonData[idx].CategoryID === customerAdd.salesPersonData.salesPersonID) {
                    return customerAdd.salesPersonData[idx].salesPersonName;
                  }
              }
            }

我想在列sales person上显示下拉列表,但我收到错误salesPersonDropDownEditor未定义。当我在salesPersonDropDownEditor附近添加“”时,它不会抛出错误。现在它抛出错误getSalesPersonName未定义。

如何在从网格编辑时调用这些功能并显示下拉列表???

1 个答案:

答案 0 :(得分:1)

这是关于使用声明性初始化(即使用data-属性来配置网格)的否定之一。您必须在customer.js文件中配置网格,使其与您的功能位于相同的范围内。

添加-客户template.html

<div id="leadsGrid"></div>

<强> customer.js

$('#leadsGrid').kendoGrid({
  dataSource: leadsDS,
  scrollable: true,
  editable: 'popup',
  toolbar: ['create'],
  columns: [
    {   
      field: 'salesPerson', 
      title: 'Sales Person', 
      editor: salesPersonDropDownEditor, 
      template: getSalesPersonName
    },
    // shortened for brevity
  ]
});

var salesPersonDropDownEditor = function(container, options) {
  // hidden for brevity
};

var getSalesPersonName = function() {
  // hidden for brevity
};