【问题标题】:How to call a typescript function from kendo template如何从剑道模板调用打字稿函数
【发布时间】:2018-04-25 17:24:26
【问题描述】:

我在我的项目中使用 Typescript 和 Kendo UI。在我想通过调用网格模板中的函数来自定义网格数据之前,它工作得很好。我将模板更新为:

    <td>#= Simple(Id) #</td>

并添加了该功能。它工作正常并在单元格中显示 id。

<script>
    function Simple(id) {
        return id;
    }
</script>

问题是我想在我的 Typescript 类中定义 Simple 方法,但我无法正确获取函数的范围。我试图将它添加到我的视图类或数据源中,但没有任何效果。由于函数未定义,因此无法呈现列。我不想定义一个全局函数。如何将 typescript 类中定义的函数绑定到网格?

【问题讨论】:

标签: templates kendo-ui typescript


【解决方案1】:

通常,传递给模板的是 Kendo 模型的实例,因此您必须在 kendo.data.Model.fn 上定义您的方法。

【讨论】:

    【解决方案2】:

    您需要在全局上下文中声明它。

    module MyModule {
        export class MyClass {
        //Do wathever you want
        //Template
        <td>#= Simple(Id) #</td>
        }
    }
    
    function Simple(id) {
            return id;
    }
    

    参考:Not able to call function in typescript from kendo template in kendotreelist

    【讨论】:

      【解决方案3】:

      我知道这是一个老问题,但我遇到过类似的问题并找到了解决方法。

      在您的 cshtml 文件中。你需要引用你生成的 javascript 文件,然后像这样实例化对象

      <script src="@Url.Content("~/Scripts/Notifications/notifications.js")"> </script>
      <script>
          var fe_head = new Fe.Upsm.Head();
          var fe_n = new Fe.Upsm.Notifications();
      </script>
      

      然后在您的打字稿文件中,我正在使用类似这样的命名空间和类

      namespace Fe.Upsm {
          export class Notifications {
              .. methods
              NotificationsGrid_onDataBound(){
              ... code here
              }
              ... more methods
      
          }
      }
      

      在剃刀中

      @(Html.Kendo().Grid<NotificationViewModel>()
            .Name("NotificationsGrid")
            .AutoBind(false)
            .NoRecords("No notifications yet")
            .TableHtmlAttributes(new { @class = "table" })
            .Events(m => m.DataBound("fe_n.NotificationsGrid_onDataBound")) // because you have instantiated your object you then qualify it the object name you have newed up the instance of and the method
      ..... more grid values
      

      希望这能解决其他人遇到的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-26
        • 2017-01-16
        • 2023-03-30
        • 2020-02-21
        • 1970-01-01
        • 2016-11-27
        相关资源
        最近更新 更多