0

i have a dropdown and on selecting items item i want to update the content of this dropdown list using ajax call,everything is fine but i don't know how to put the array returned by ajax call in callback function as data should be used to update my dropdown list. thankx in advance

  • possible duplicate of [jQuery: Best practice to populate drop down?](http://stackoverflow.com/questions/815103/jquery-best-practice-to-populate-drop-down) – trejder Oct 27 '14 at 07:00

2 Answers2

0

If there's a lot of data that needs to be added after the ajax call, what I've done in the past is set the html in the controller (yii) and pass the fully updated list as the return for the ajax call. It's much easier to replace the options like this in your ajax success callback than having to convert a ton of variables into javascript. Hopefully that helps.

in your Yii controller I would do something like this:

$response = [];
$response["newDropdownValues"] = "<option>Value 1</option><option>Value 2</option>";
$this->controller->sendResponse(200, $response);

And in your ajax callback you'd write something like:

success: function(output) {
            var reply = jQuery.parseJSON(output);
            $(this).find('select').html(reply.newDropdownValues);
    },
dstefanis
  • 247
  • 1
  • 7
0

You need to do a better research, because question like that has already been answered in Stack Overflow (probably more than once). Here you have best, perfect, yet very simple example.

In general, you have to use jQuery's .each method to iterate through all elements. You can either iterate over your result (since you said, it is an array) or your select's children. Either way should work, pick solution that best suits you.

There is another question here that can bring you a great idea, how to solve this problem.

These are only two examples, narrowed only to Stack Overflow. If you do better research, I'm pretty sure, you'll find hundreds of similar examples in the Internet.

Community
  • 1
  • 1
trejder
  • 17,148
  • 27
  • 124
  • 216