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