Switch case with an object

Hi All,

I need some help with JS which is a part of a sigma grid ajax function. Here’s an array with variable names as MySql table column names :


var dsOption= {

	fields :[
		{name : 'username'  },
		{name : 'full_name'  },
		{name : 'userlevel'  },
		{name : 'email'  },
		{name : 'phone'  }
		
	],
	recordType : 'object'
}

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!

Is the userlevel stored as a number or a string? If it’s as a string, you would need to use ‘9’ instead of 9

I’ve tried both 9 and ‘9’… By the way, 9 is a tiny integer, retrieved from a DB table. And THANKS for your reply :slight_smile:

From what I’m seeing of your dsOption object, it only has two properties, fields and recordType. userlevel is a deeper value within the fields object.

That’s correct! So how can I retrieve the value of ‘userlevel’?

I’m sorry but I don’t know anything about sigma grid Ajax. There is other help out there though for it, such as this tutorial on loading data from a database.
http://sigmawidgets.com/forum/ajax-data-grid-view/sigma-grid-2-1-tutorial-2-load-data-from-database/

Since fields is an array of name, did you try:



switch (dsOption.fields[2].name) {
case 9:
case ‘9’:
dsOption.fields[2].name = ‘admin’;
break;
}


That sounds logical! I’ve tried it but instead of ‘9’ (or 9), the var dsOption.fields[2].name had the value userlevel

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?

Well, yes : a script (UsersController.php called from a SigmaGrid JS file) returns values for the user from a DB table :


$json=json_decode(stripslashes($_POST["_gt_json"]));

if($json->{'action'} == 'load'){
  $sql = "SELECT * FROM users";
  $handle = mysql_query($sql);
		
  $retArray = array();
  while ($row = mysql_fetch_object($handle)) {
    $retArray[] = $row;
  }
  $data = json_encode($retArray);
  $ret = "{data:" . $data .",\
";
  $ret .= "recordType : 'object'}";
  echo $ret;

}

and here’s the java code which calls the script above :


var grid_demo_id = "myGrid1";

var dsOption= {

	fields :[
		{name : 'username'  },
		{name : 'full_name'  },
		{name : 'userlevel'  },
		{name : 'email'  },
		{name : 'phone'  }
		
	],
	recordType : 'object'
}

var colsOption = [
     {id: 'username' , header: "Username" , width :80 },
     {id: 'full_name' , header: "Full Name" , width :150 , editor:{type:'text'}},
	 {id: 'userlevel' , header: "Userlevel" , width :70, width :80 , editor : { type :"text" }},
	 {id: 'email' , header: "Email" , width :150, width :160 , editor : { type :"text" }},
	 {id: 'phone' , header: "Phone" , width :70, width :80 , editor : { type :"text" }}
       
];


var gridOption={
	id : grid_demo_id,
	loadURL : '/grid/UsersController.php',
	saveURL : '/grid/UsersController.php',
	width: "700",  //"100%", // 700,
	height: "200",  //"100%", // 330,
	container : 'gridbox', 
	replaceContainer : true,
	encoding : 'UTF-8', // Sigma.$encoding(), 
	dataset : dsOption ,
	columns : colsOption ,
	clickStartEdit : true ,
	defaultRecord : {'full_name':"",'userlevel':"",'email':"",'phone':""},
	pageSize:100,
	toolbarContent : 'reload | del | save | print'
};


var mygrid=new Sigma.Grid( gridOption );
Sigma.Util.onLoad(function(){mygrid.render()});

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 :slight_smile:

Thank you in advance!

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.

That’s the sort of thing that I would do.

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


var colsOption = [
     {id: 'username' , header: "Username" , width :80 },
     {id: 'full_name' , header: "Full Name" , width :150 , editor : {type:'text'}},
	 {id: 'name' , header: "Userlevel" , width :70, width :80 , editor : {type:'select', options : {'option_name_1' : 'value_1', 'option_name_2' : 'value_2', 'option_name_3' : 'value_3'}}},
	 {id: 'email' , header: "Email" , width :150, width :160 , editor : { type :"text" }},
	 {id: 'phone' , header: "Phone" , width :70, width :80 , editor : { type :"text" }}
       
];

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.

What you want is this:

Column Property: Renderer

You would simply use that property to pass a function which you than run your case statement and send back the converted value for display.

You were right oddz!!! And the solution can be found at http://sigmawidgets.com/forum/ajax-data-grid-view/fill-in-a-dropdown-list/

Thank you guys both for your help!