Show category if column value is not finded

2 posts, 1 answers
  1. anxo
    anxo avatar
    1 posts
    Member since:
    Aug 2020

    Posted 17 Aug 2020 Link to this post

    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>

  2. Answer
    Petar
    Admin
    Petar avatar
    472 posts

    Posted 19 Aug 2020 Link to this post

    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

Back to Top