SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  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,705
    Mentioned
    102 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,705
    Mentioned
    102 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,705
    Mentioned
    102 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 Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 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.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 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.

  11. #11
    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
  •