-1

Here is some PHP code that generates a json (javascript) object in response to a web service requestion. I'm not sure how to iterate over this object. I've looked at a lot of examples and they are in jQuery and don't deal the case (as mine does) of having sub-objects which I need to render in my select object.

Can anyone show me how to use a javascript object like this to populate a select list?

There are also sub objects with value indexs

data = object(
    0 => object(
        'label' = > 'test1',
        'value' = > 1
    ),
    1 => object(
        'label' = > 'test1',
        'value' = >
        0 = > object(
            'label' = > 'sub testing1',
            'value' = > 1
        ),
        1 = > object(
            'label' = > 'sub testing2',
            'value' = > 1
        ),
    ),
    3 = > object(
        'label' = > 'test3',
        'value' = >
        0 = > object(
            'label' = > 'sub testing - test 3',
            'value' = > 33
        ),
    )
)
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 1
    How is this a json object? – Hogan Oct 24 '13 at 15:55
  • possible duplicate of [Iterating over every property of an object in javascript using Prototype?](http://stackoverflow.com/questions/587881/iterating-over-every-property-of-an-object-in-javascript-using-prototype) – Sébastien Oct 24 '13 at 15:56
  • Actually this is associative array of php as ajax json response. I want to populat multi select with this array. – Shahid Najam Afridi Oct 24 '13 at 15:56
  • Sébastien , Because no body is ready to help me that's why i posted it again. – Shahid Najam Afridi Oct 24 '13 at 15:58
  • selects are 2d, this data is nested, so it won't fit. – dandavis Oct 24 '13 at 15:59
  • we can separate sub values by -- (child) (parent and child) – Shahid Najam Afridi Oct 24 '13 at 16:01
  • I have created a json , hope this will help;- [ { "label": "test1", "value": 1 }, { "label": "test1", "value": [ { "label": "sub testing1", "value": 1 }, { "label": "sub testing2", "value": 1 } ] }, { "label": "test3", "value": [ { "label": "sub testing - test 3", "value": 33 } ] } ] – Anand Jha Oct 24 '13 at 16:22
  • @dandavis - drop downs can be nested... see this example of how to do it in jQuery http://stackoverflow.com/questions/19214051/jquery-convert-nested-list-of-radio-buttons-to-dropdown-with-optgroup-headings – Hogan Oct 24 '13 at 18:48

1 Answers1

0

Once you have the php data transformed into JSON format, you can use the resulting object to generate a drop down. The best way to my mind to do this with PrototypeJS is to convert it into a hash.

$H(obj);

A hash can be easily used by iterating through the key-value pairs like so:

var selectOpts = new String();
$H(obj).each(function(pair){
    selectOpts += "<option value=\"" + pair.key + "\">" + pair.value + "</option>";
});

The above is assuming the key is the actual value while value is the "label". With your dataset you might want to obtain the value object and access the label and value with dotr notation.

If you are using Script.aculo.us, then you might considering creating the HTML options using the module Builder:

selectOpt = Builder.node("option", { [attribs] });

In that case you'need to array push the "built" option nodes and append it inside a select element.

  • There is no need to use JQuery. I don't know why there is a comment which misleads people to think that JQuery is a better solution. – Shubhojoy Mitra Nov 21 '13 at 15:55