Kendo the enumeration value to the enumeration name in the grid control

Source: Internet
Author: User

We often encounter situations where the enumeration values need to be converted to names for display in the development process. We have such a data source object as follows:

var users = [        1, Name: "Similar1", Status:1},        2, Name: "Similar2", Status:2}
    
     ];
    

Where the field status represents the status of the user, 1 means "available" and 2 means "disabled". We use the Kendo grid general configuration as follows:

columns: [        "id", Title: "Number" },        "name", Title: "Username" },        "status", Title: "State " }    ],

The corresponding effect is as follows:

By the visible, our status that column is directly the number that is displayed, which is not the result we want. What we need is to convert 1, 2, respectively, to text: available, disabled. At first I thought of the conditional judgment through the template, when status is 1 o'clock, the display is available, and the display is disabled for 2 o'clock. The code is as follows:

columns: [        "id", Title: "Number" },        "name", Title: "Username" },        "status", Title: "Status", Template: "#= (status = = 1)?" Available ': ' Disable ' # ' }    ]//  or columns: [        "id", Title: "Number" },        " Name ", Title:" Username " },        " status ", Title:" State ", Template:" #if (status = = 1) {# #}else{# disabled #}# " }
    ]

After the thought that this method is too rotten, oneself seem to accept not. The way the template is judged is too low-maintenance, and if you add a few states in a few days, you have to write a bunch of If. Therefore, to find the official API documentation, looking for a better solution, so the following gains, the code is as follows:

// 1. First we define an object like enumeration function var statusenum = [    "available", Value:1},    "disabled", Value:2  }    ]//2. Then make the following adjustments to the configuration in the kendo grid columns        : ["id", Title: "Number" },         "Name", Title: "Username" },        "status", Title: "State", Values:statusenum}    ]

Do the above 2 steps OK, this way is not easier to accept it? It's easier to maintain than before, and it's clear to see ...

Kendo the enumeration value to the enumeration name in the grid control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.