简体   繁体   中英

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

Ok now I have this Kendo Grid code:

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);
});

where I check if a column of a row has any non empty value or not, and if so I place a button there.

When I try to assign the value to attachment, ('value = attachment' part) I'm getting undefined as a result, but if I enclose attachment like this:

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

I can print the actual link assigned to it. How can I get the value of the attachments (individually, not as a list or array or sth) when I click the button associated with it?

Here is the fiddle: https://jsfiddle.net/phyrax/zz1h65f5/

Thanks in advance!

Kendo grid column template is a template for HTML content.

If you want to place some element in html code, you should use template. As I see, you want to execute JS code inside this template. To do this you should define <script> $value = #= attachment# </script> in your template.

Other question is what you want to do, because when you do something like this, it is executed for each row during page load. So the value of $value will be always set to last row.

EDIT to first comment:

You should consider to define template as

<input type="button" class="info" value="IT WORKS" onclick="setAttachment('#= attachment#')" />

and in global JS code define function

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

This is more proper way to solve this task. Make action on click trigger, not immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM