This is a migrated thread and some comments may be shown as answers.

Show category if column value is not finded

1 Answer 51 Views
Grid
This is a migrated thread and some comments may be shown as answers.
anxo
Top achievements
Rank 1
anxo asked on 17 Aug 2020, 09:57 AM

In the following example I want to show the Id of the category when the corresponding "columns.values" is not found. Is there a way to do it easily?

in the "burger" line it is blank, and I want that in that case at least its category is displayed

<div id="grid"></div>
<script>
$("#grid").kendoGrid({
  columns: [
    { field: "productName" },
    { field: "category", values: [
      { text: "Beverages", value: 1 },
      { text: "Food", value: 2 }
    ] }
  ],
  dataSource: [
    { productName: "Tea", category: 1 },
    { productName: "Ham", category: 2 },
    { productName: "Burger", category: 3 },
    
  ],editable: true,
});
</script>

1 Answer, 1 is accepted

Sort by
0
Accepted
Petar
Telerik team
answered on 19 Aug 2020, 06:17 AM

Hi Anxo,

The targeted functionality can be implemented using a template definition for the "category" column as demonstrated in this Dojo example.

The column template in the Grid is defined as follows:

columns: [
    { field: "productName" },
    { field: "category", template: "#=getTemplate(category)#" }
]

Where the getTemplate function is defined like this:

function getTemplate(category){

    var values = [
      { text: "Beverages", value: 1 },
      { text: "Food", value: 2 }
    ];
    var result;

    if(result === undefined){
      $.each(values, function(index, valueItem){
        if(valueItem.value === category){
          result = valueItem.text;
        }
      })
    }
    if(result === undefined){
      result = category;
    }
    return result
  }

I hope the above example will help you implement the targeted functionality in your application. 

Regards,
Petar
Progress Telerik

Tags
Grid
Asked by
anxo
Top achievements
Rank 1
Answers by
Petar
Telerik team
Share this question
or