The field ‘userlevel’ can be 1, 5 or 9 and I don’t want to display numbers but strings like ‘guest’, ‘member’ and ‘admin’ respectively. My knowledge in JS is VERY modest but I simply have to modify this script… What I’ve done so far is :
switch (dsOption['userlevel']) {
case 9:
dsOption['userlevel'] = 'admin';
break;
}
which doesn’t seem to work… Do I need another approach maybe, could you guys please give some directions? Thank you!
I think we need more information. It sounds like a user signs on, and the server returns a 1, 5, or 9 - I would guess in JSON format. Can you show the code where that number is returned and into what variable it is placed?
The values for the column userlevel are 1, 5 or 9 but I don’t want to display NUMBERS, but guest, member and admin respectively… That’s where I need your help guys
If it were me, I would use a separate database table called userLevel [with fields called id and name] which correlates those numbers with an appropriate description, and make use of that table when getting the users.
userLevel
=========
id name
-- ----
1 guest
5 member
9 admin
SELECT users.*, userLevel.name
FROM users
LEFT OUTER JOIN userLevel
ON users.userlevel = userLevel.id
That way your database continues to do the job that it’s good at, and the scripting to retrieve and show the data continues to do what it’s good at as well.
Thank you Paul for your solution. I was thinking about to put a new table to solve this, but that will be the LAST option… I didn’t give up to change the value from number to a string inside the JS yet… Thanks anyway!
But why? It’s outside of the job description for that code. That code is supposed to get the data from the database, and show it on the screen. If you want different information to be displayed then don’t muck [sanitised] around with the code that gets the data from the database.
If you want different information to be shown, then it’s that information from the database that you need to be focusing on.
Good question and reasonable notice from your side. I hope I’ll manage to explain why I really need to retrieve the values of that object. In the meantime I actually used the method you’ve provided above (with another table and the join query), but it came out I’ll need to retrieve the object values eventually. Namely, one field in the grid needs to be edited (with a select-option). This is done with the code
You can see the static option => value pairs option_name_1 => value_1, etc, etc
Is there ANY chance I can use the VALUES of the variable ‘name’ from above??? I’ve tried dsOption.fields[2].name, dsOption.fields[2].value and several more, nothing helped… Really hope someone could figure this out - or give an alternate solution. Thank you in advance!!!
I have seen similar libraries and normally there is a way that a value can be transformed/modified before being rendered on screen using a callback. One would think that there is an option/property for each column defined in colsOption to accept the raw data value and modify it with a callback like other grid libs.