Search code examples
javascriptjqueryasp.net-mvckendo-gridkendo-asp.net-mvc

Kendo mvc Grid ClientTemplate javascript function not working


I have a Kendo mvc Grid and using client template as a column and i wrote a javascript function in the template to return the script block but its seems not working and no javascript error. I also tried to write the script to the client template directly but its not working too.

//html in client template

    .Columns(columns =>
    {

     columns.Template(e =>
     { }).ClientTemplate(

         "<div class='table-responsive'>" +
              "<table border='0' class='table' >" +

                 ...................................                

              "</table>" +
         "</div>"+
          "#=AlignDiv(Id)#"
                         );
      })

//javascript function to return a script block as a string

      <script type="text/javascript">
      function AlignDiv(Id) {
      var result = "<script> $('.calDiv"+Id+"').map(function () {" +
           "return $(this).Height();" +
       "}).get()," +
       "maxHeight = Math.max.apply(null, heights);" +
       "$('.calDiv" + Id + "').height(maxHeight);" +
        "alert('test')<\/script>";
      return result;
  }

Thanks a lot, Dennis


Solution

  • In order to format Kendo Grid Column value with conditionally chosen action you can use one of the suitable examples below. For more information: How Do I Have Conditional Logic in a Column Client Template?


    UI for Javascript:

    {
        field: "EmployeeName", type: "string", width: "55px", title: "Employee Name", 
                template: "#= GetEditTemplate(data) #"
    }
    

    UI for MVC:

    ...
    columns.Bound(t => t.EmployeeName)
    .Title("Status Name")
    .Template(@<text></text>)
    .ClientTemplate("#= GetEditTemplate(data)#").Width("55px");
    ...
    

    Javascript Method:

    <script>
    //Change the color of the cell value according to the given condition
    function GetEditTemplate(data) {
        var html;
    
        if (data.StatusID == 1) {
            html = kendo.format(
            //"<a class=\"k-button\" href='" + '@Url.Action("Edit1", "Controller")' + "/{0}" + " '>Edit</a>  ",
            "<span class='text-success'>" +
            data.EmployeeName
            + "</span>"
            );
        }
        else {
            html = kendo.format(
            //"<a class=\"k-button\" href='" + '@Url.Action("Edit2", "Controller")' + "/{0}" + " '>Edit</a>  ",
            "<span class='text-danger'>Cancel</span>"
            );
        }
        return html;
    }
    </script>