0

My JsonResult as follows

[{"name":"Group 1"},{"name":"Group 2"},{"name":"Group 3"}]

I'm little bit confused while writing for loop, so How to iterate over this or get the values of name inside buildSelect function in editoptions in jqGrid? My colModel code in jqGrid as follows

 { name: 'GroupName', index: 'GroupName', width: 60, align: 'center', search: false, editable: true, edittype: 'select',
   editoptions: {
                 dataUrl: "/Category/GetCategoryGroup",
                 buildSelect: function (response) {
                                  if (response && response.length) {
                                        // for loop iteration to get name values           
                                  }                                                 
                            }                                          
                 } 
 },
prakash2089
  • 498
  • 4
  • 16
  • You don't know how to iterate over an array? – Felix Kling Aug 16 '13 at 10:18
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Aug 16 '13 at 10:19
  • @FelixKling: My question is not how to iterate over an array. Myself doesn't know how to use this inside function in buildSelect(jqGrid) and to return it as dropdownlist? – prakash2089 Aug 16 '13 at 10:53
  • See: http://stackoverflow.com/a/4102155/218196, the "UPDATED" section. – Felix Kling Aug 16 '13 at 11:15

4 Answers4

0
 if (response && response.length) {
    var contents='<select>';
    for(var i=0; i< response.length; i++){
        var t = response[i];
        var g_name = t.name;
        contents+="<option>"+g_name+"</option>";
    }
   contents+="</select>";
   document.getElementById("contentDropdown").innerHTML = contents; 
}

where you want to show dropdown create a div like this:

<div id="contentDropdown"></div>

hope this will help you.

  • You shouldn't use `for...in` to iterate over arrays. http://stackoverflow.com/questions/2265167/why-is-forvar-item-in-list-with-arrays-considered-bad-practice-in-javascript – Felix Kling Aug 16 '13 at 10:28
  • @komal: sorry this not works inside buildSelect in jqGrid. I have to show these three groups in dropdownlist in jqGrid(form editing) – prakash2089 Aug 16 '13 at 10:49
0
var groups = [{"name":"Group 1"},{"name":"Group 2"},{"name":"Group 3"}];
for(var i = 0; i< groups.length; i++) {
 var name = groups[i].name;
}
col
  • 397
  • 3
  • 10
0

same like an array

var myOb = [{"name":"Group 1"},{"name":"Group 2"},{"name":"Group 3"}] ; 

for (var i=0; i < myOb.length; i++) { 
 console.log(myOb[i]);
 console.log(myOb[i].name);
 console.log(myOb[i]['name']);
}

You can do further loop into edit options

madhu131313
  • 7,003
  • 7
  • 40
  • 53
0

Another approach to iterate the array

var groups = [{"name":"Group 1"},{"name":"Group 2"},{"name":"Group 3"}]; 

$.each( groups, function() {
    alert(this.name);
});
Ankit
  • 44
  • 1