给剑道网格列模板中的全局变量赋动态值

Assigning dynamic value to a global variable in kendo grid column template

本文关键字:全局变量 动态 网格      更新时间:2023-09-26

现在我有了Kendo Grid代码:

var ds = [
    { "ID" : 1, "attach" : "true", "attachment" : "http://www.google.com" },
    { "ID" : 2, "attach" : "false", "attachment" : "" },
    { "ID" : 3, "attach" : "true", "attachment" : "http://www.wikipedia.com" },
    { "ID" : 4, "attach" : "false", "attachment" : "" }
];
var $value = "asd";
var grid = $("#grid").kendoGrid({ 
    dataSource: ds,
    columns: [
        { field: "ID", Title: "ID", filterable: false, sortable: false, hidden: false },
        { field: "attach", Title: "Attached?", filterable: false, sortable: false, hidden: false},
        {
            title: "Attachment Link",
            template: '#if(attachment != ""){' +
                          '$value = attachment' +
                          '#<input type="button" class="info" value="IT WORKS" />' +
                      '#}else{#' +
                          '<label>NOPE</label>' +
                      '#}#',
            headerTemplate: "<label> Attachment </label>",
            sortable: false,
            filterable: false,
            width: 100
        }
    ]
}).data("kendoGrid");
//this is where I have been playing around trying to get the value. its not working. Shocker :D 
//I changed this part frequently, this is just the latest form I got it in. 
$(".info").on("click", function() {
        var row = $(this).closest("tr");
        var item = grid.dataItem(row);
        var show = $value;
        alert("The item is:" + show);
});

检查一行的一列是否有非空值,如果有,我在那里放置一个按钮。

当我试图将值分配给附件时,('value = attachment'部分)我得到未定义的结果,但如果我附上附件如下:

'#if(attachment != ""){#' +
                '#= attachment#' +
                '<input type="button" class="info" value="IT WORKS" />'
'#}else{#' +
                '<label>NOPE</label>' +
'#}#',

我可以打印分配给它的实际链接。当我单击与之关联的按钮时,我如何获得附件的值(单独,而不是作为列表或数组或其他)?

小提琴在这里:https://jsfiddle.net/phyrax/zz1h65f5/

提前感谢!

剑道网格列模板是一个HTML内容模板。

如果你想在html代码中放置一些元素,你应该使用模板。正如我所看到的,你想在这个模板中执行JS代码。要做到这一点,你应该在模板中定义<script> $value = #= attachment# </script>

另一个问题是您想要做什么,因为当您做这样的事情时,它会在页面加载期间对每一行执行。因此,$value的值将始终设置为最后一行。

编辑第一个注释:

你应该考虑将模板定义为
<input type="button" class="info" value="IT WORKS" onclick="setAttachment('#= attachment#')" />

和在全局JS代码中定义函数

function setAttachment(attachmentValue)
{
   $value = attachmentValue;
}

这是解决这个任务更合适的方法。在点击触发时进行操作,而不是立即。