在网格中未定义Kendo UI - data-column模板函数

kendo ui - data-column template function is undefined in grid

本文关键字:data-column 函数 UI 网格 未定义 Kendo      更新时间:2023-09-26

我有两个文件: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具有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未定义

我如何调用这些函数和显示下拉列表,而从网格编辑??

这是关于使用声明性初始化(即使用data-属性来配置网格)的负面影响之一。您必须在customer.js文件中配置您的网格,以便它与您的函数处于相同的作用域。

add-customer-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
};