Kendo UI - 网格中的工具提示

时间:2014-04-22 10:45:03

标签: javascript kendo-ui kendo-grid kendo-template kendo-tooltip

我正在尝试为我的网格创建一个工具提示:

$("#grid").kendoTooltip({
    autoHide: true,
    showOn: "mouseenter",
    width:125,
    height:125,
    position: "right",
    filter: ".k-grid-content a.hasTooltip",
    content: kendo.template($("#storeTerritory").html())
});

模板定义:

<script type="text/x-kendo-template" id="storeTerritory">
<div class="tooltipcontent">
    #for(var i = 0; i < Territories.length; i++){#
        #if (Territories != 'null' && Territories != '')  {#
            <p>#=Territories[i].TerritoryDescription#</p>
        #} else{#
            <p>Information not available</p>
      #}#
    #}#
</div>
</script>

我在这里设置了一个示例:
http://jsbin.com/iJunOsa/21/edit

当我鼠标悬停在'Wilton'上时,控制台出现ReferenceError: Territories is not defined错误

假设我要用普通的HTML替换storeTerritory模板的内容,然后出现工具提示:

<p>Wilton</p>

问题是什么?

3 个答案:

答案 0 :(得分:5)

问题是没有与工具提示相关的模型;为了做你想做的事,你需要使用一个函数来创建内容:

$("#grid").kendoTooltip({
    autoHide: true,
    showOn: "mouseenter",
    width: 125,
    height: 125,
    position: "right",
    filter: ".k-grid-content a.hasTooltip",
    content: function (e) {
        var row = $(e.target).closest("tr");
        var dataItem = $("#grid").data("kendoGrid").dataItem(row);

        var template = kendo.template($("#storeTerritory").html());
        return template(dataItem);
    }
});

(已更新demo

答案 1 :(得分:0)

问题是在模板的上下文中(运行时):

<script type="text/x-kendo-template" id="storeTerritory">
    <div class="tooltipcontent">
        #if (Territories != 'null' && Territories != '')  {#
            <p>#=Territories[i].TerritoryDescription#</p>
        #} else{#
            <p>Information not available</p>
        #}#
    </div>
</script>

没有Territoriesi,因此失败。

答案 2 :(得分:0)

 grid = $("#grid").kendoGrid({
      dataSource: dataSource,
      scrollable: true,
      filterable: true,
      toolbar: ["create"],
      columns: [
        { field: "ID", width: "50px" },
        { field: "Text", width: "200px", attributes: {
          style: 'white-space: nowrap '
        }  }],
      editable: "incell"
    }).data("kendoGrid");

    $("#grid").kendoTooltip({
      filter: "td:nth-child(2)", //this filter selects the second column's cells
      position: "right",
      content: function(e){
        var dataItem = $("#grid").data("kendoGrid").dataItem(e.target.closest("tr"));
        var content = dataItem.Text;
        return content;
      }
    }).data("kendoTooltip");

Demo