kendo-uikendo-gridkendo-template

Kendo grid column template for column with values


My kendo grid has special column with values: "Unpaid", "Paid". From backend arrives 0 - means "Unpaid", 1 - means "Paid".

My config for this column:

{
columnMenu: true
encoded: true
field: "Invoice__state"
filterable: {cell: {…}}
resizable: true
sortable: true
title: "State"
type: "string"
values: Array(2)
0: {value: 1, text: "Paid"}
1: {value: 0, text: "Unpaid"}
}

Everysing fine (pic 1).

Now I need to make some markup for column - red for Unpaid, green for Paid. I want to use template for column. I just add simple template

template: "<span class="label label-danger">#: Invoice__state #</span>"

But now I see 0 or 1 instaed Unpaid or Paid (pic 2).

How I can modify template to show label instead values ?

pic1

pic2


Solution

  • You can do with help of attribute by condition.

    Here code for your reference.

    CSS Block :

      <style>
    .red{
        color:red;
    }
    
    .green {
        color:green;
    }
    </style>
    

    Javascript Block:

    <script>
    $("#grid").kendoGrid({
    columns: [
    { field: "productName" },
    { field: "category", values: [
    { text: "Beverages", value: 1 },
    { text: "Food", value: 2 }
    ],attributes: {
                class: "#=category ==1 ? 'red' : 'green' # #console.log(data)#"
              } }
    ],
    dataSource: [
    { productName: "Tea", category: 1 },
    { productName: "Ham", category: 2 }
    ]
    });
    </script>