SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 :
    Code:
    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 :
    Code:
    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!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    userlevel is a deeper value within the fields object.
    That's correct! So how can I retrieve the value of 'userlevel'?

    Full time ADMIN - art community
    Part time coder - dsign

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    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-d...from-database/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot bgil's Avatar
    Join Date
    Apr 2009
    Location
    So. California
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since fields is an array of name, did you try:

    Code JavaScript:
    [CODE][FONT=Courier New]
            switch (dsOption.fields[2].name) {
       case 9:
       case '9':
         dsOption.fields[2].name = 'admin';
         break;
            }
    [/FONT][/CODE]
    -Brad

  8. #8
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgil View Post
    Since fields is an array of name, did you try:

    Code JavaScript:
    [CODE][FONT=Courier New]
            switch (dsOption.fields[2].name) {
       case 9:
       case '9':
         dsOption.fields[2].name = 'admin';
         break;
            }
    [/FONT][/CODE]
    That sounds logical! I've tried it but instead of '9' (or 9), the var dsOption.fields[2].name had the value userlevel...

    Full time ADMIN - art community
    Part time coder - dsign

  9. #9
    SitePoint Zealot bgil's Avatar
    Join Date
    Apr 2009
    Location
    So. California
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    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?
    -Brad

  10. #10
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgil View Post
    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 :
    PHP Code:
    $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 .",\n";
      
    $ret .= "recordType : 'object'}";
      echo 
    $ret;


    and here's the java code which calls the script above :
    Code:
    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

    Thank you in advance!

    Full time ADMIN - art community
    Part time coder - dsign

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    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.

    Code:
    userLevel
    =========
    id   name
    --   ----
    1    guest
    5    member
    9    admin
    Code sql:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    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.
    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!

    Full time ADMIN - art community
    Part time coder - dsign

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dujmovicv View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    But why?
    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
    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!!!!

    Full time ADMIN - art community
    Part time coder - dsign

  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    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.
    The only code I hate more than my own is everyone else's.

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    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.
    The only code I hate more than my own is everyone else's.

  17. #17
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were right oddz!!!! And the solution can be found at http://sigmawidgets.com/forum/ajax-d...dropdown-list/

    Thank you guys both for your help!

    Full time ADMIN - art community
    Part time coder - dsign


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •