You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery Kendo Grid模板调用customTemplate函数报未定义错误如何解决

报错原因

Kendo Grid 的字符串模板(#=xxx# 格式)执行时会默认在全局作用域下查找调用的函数。你将代码抽离到独立JS文件时,大概率把customTemplate函数写在了DOM就绪闭包($(function(){}))内部,该函数仅在闭包内可访问,全局作用域下不存在,因此模板解析时会抛出未定义错误。

解决方案

推荐优先使用函数类型模板的方案,无需污染全局作用域,也能完全规避作用域问题:
将列配置里的字符串模板替换为直接调用customTemplate的函数即可,修改后的Grid列配置如下:

{ 
  field: "editor",
  title: "Editor",
  // 直接传入模板函数,和customTemplate在同一作用域下即可正常调用
  template: function(data) {
    return customTemplate(data.type, data.editor);
  },
  editor: chooseEditor
}

如果需要保留原有字符串模板的写法,只需将customTemplate挂载到全局window对象即可:

// 将函数暴露到全局作用域
window.customTemplate = function(type,value) {
  if (value == null)
    return "";
  switch (type) {
    case "date":
      return kendo.toString(kendo.parseDate(value), 'yyyy/MM/dd');
    default:
      return value;
  }
}

内容的提问来源于stack exchange,提问作者HereToLearn_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:54:01