Search code examples
javascriptkendo-uitelerikkendo-gridtelerik-grid

How to pass hardcoded string value from javascript onclick with template option using kendo ui grid?


I am having 2 grids on my page with name grid1 and grid2.

Now i want to pass grid name as hardcoded to my 1 common javascript function for deleting records from grid like below:

for Grid 1 delete function:

field: "Id",
template:<a  title="delete" onclick="javascript:return Delete(<#=Id#>,<#=grid1#>);" > //showing error in console  grid1 is not defined

for Grid 2 delete function:

field: "Id",
template:<a  title="delete" onclick="javascript:return Delete(<#=Id#>,<#=grid2#>);" > //showing error in console  grid2 is not defined.

My javascript function:

function Delete(id, gridname) {
        console.log(id,gridname)
}

Solution

  • Please try with the below code snippet. If you write any text between # (hash) the grid try to find that field in your datasource that's why you got the undefined error.

    JS Function:

    function Delete(id, gridname) {
                var grid = $("#" + gridname).data("kendoGrid");
                console.log(id, gridname)
    }
    

    for Grid 1 delete function:

    field: "Id",
    template: "<a title='delete' onclick='javascript:return Delete(\"#:Id#\",\"grid1\");'></a>",
    

    for Grid 2 delete function:

    field: "Id",
    template: "<a title='delete' onclick='javascript:return Delete(\"#:Id#\",\"grid2\");'></a>",
    

    Let me know if any concern.