No Idea For Name
No Idea For Name

Reputation: 11577

Kendo Grid Custom Column

I have the following code:

          $("#grid").kendoGrid({
            dataSource: {
                type: "odata",
                data: scannedResult.targetList,
                pageSize: 20
            },
            height: 550,
            groupable: true,
            sortable: true,
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 5
            },
            columns: [{
                field: "proccess",
                title: "Contact Name",
                width: 200
            }, {
                field: "status",
                title: "status"
            }, {
                field: "comment",
                title: "comment"
            }]
        });

creating a kendo simple grid. for detail here is my plunker.

now the field status can be 1 of 3 values: passed, failed, skipped. I would like that the statuscolumn will show an icon instead of the value. While the code for that is rather simple, i do not know how to make the column a custom column.

Is there a way to make a column a custom column?

Upvotes: 4

Views: 6917

Answers (2)

Gram
Gram

Reputation: 404

This has already been answered, but I want to show how I'd write this if people are confused when linking to jQuery selector.

// Custom Template that takes in entire Row object
function statusTemplate(dataRow) {
  return `<span class="label label-${dataRow.status.toLowerCase()}">${dataRow.status}</span>`;
}

// Column definitions for grid
columns: [{
  field: "proccess",
  title: "Contact Name",
  width: 200
}, {
  field: "status",
  title: "status",
  template: statusTemplate
}, {
  field: "comment",
  title: "comment"
}]

http://jsfiddle.net/dentedio/hdokxme9/1/

Upvotes: 1

OnaBai
OnaBai

Reputation: 40887

You should use a template definition. Something like:

  • Define the template.
<script id="status-template" type="text/kendo-templ">
     # if (data.status === 1) { #
         <span>Status1</span>
     # } else if (data.status === 2) { #
         <span>Status 2</span>
     # } else { #
         <span>Status 3</span>
     # } #
 </script>
  • Reference the template from the column definition
        columns: [{
            field: "proccess",
            title: "Contact Name",
            width: 200
        }, {
            field: "status",
            title: "status",
            template: $("#status-template").html()
        }, {
            field: "comment",
            title: "comment"
        }]

See it running here: http://jsfiddle.net/OnaBai/5x8wt0f7/

Obviously, the template can emit any HTML code, it might be links, images...

Upvotes: 7

Related Questions