Brian Crist
Brian Crist

Reputation: 814

How can i load data from ajax to Chosen jquery?

I have use chosen at http://harvesthq.github.io/chosen/ . Ok, i testing it load data from ajax . I founding anywhere, maybe no someone success with them.

<script src="theme/js/jQuery-2.1.3.min.js"></script>
    <link href="theme/chosen_v1.4.2/chosen.css" rel="stylesheet" />
    <script src="theme/chosen_v1.4.2/chosen.jquery.js"></script>
      <script type="text/javascript" charset="utf-8">
       $(document).ready(function () {
           $(".cb_bu_info").chosen({
               width: "95%",
               source: function (data) {
                   $.ajax({
                       type: "POST",
                       url: "../BUS/WebService.asmx/LIST_BU",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function (data) {
                           $("#cb_info").html('');
                           //$.each($.parseJSON(data.d), function (idx, obj) {
                           $.each(data, function (idx, obj) {
                               $("#cb_info").append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
                           });  
                          //$("#cb_info").trigger("liszt:updated");
                       },
                       error: function (data) {
                           console.log(data.d);
                       }
                   });
               }
           });

           $("#cb_info").trigger("liszt:updated");
        });
    </script>
<select id="cb_info" class="cb_bu_info"></select>

The data form ajax as

[{"BU_ID":"B01","BU_NAME":"Agro Feed","BU_DES":"Agro Feed","EDIT_DATE":"2015-05-05T00:00:00","EDIT_BY":"","FLAG":false},{"BU_ID":"B02","BU_NAME":"Agro Farm","BU_DES":"Agro Farm","EDIT_DATE":"2015-05-05T00:00:00","EDIT_BY":"","FLAG":false}]

Well , it's look ok , but when i run it , result not show in select option, see browser dev tool , I've not seen error. Anything is ok.What's the problem happen in here? Notes: only use Chosen Jquery

Upvotes: 9

Views: 31570

Answers (5)

wardah amaliyah
wardah amaliyah

Reputation: 1

try this. it works for me. please pay attention to the bold text

Ext.Ajax.request({
url:'<?php echo base_url()."index.php/";?>ttemuan31a/cari_divisi',
method:'post',
params:{

        divisi:vdivisi

    },
success:function(data)
{
    $("#divisi").chosen();
        //document.getElementById('detail_divisi').innerHTML=response.responseText;
    $('#divisi').empty();

    $.each(JSON.parse(**data.responseText**), function(i, item) {
        $('#divisi').append('<option value="'+item.id+'">'+item.namadivisi+'</option>');
        $("#divisi").trigger("chosen:updated");
    });
    }
});
}

Upvotes: 0

Bablu Ahmed
Bablu Ahmed

Reputation: 5030

You can try as follows it works for me

$.ajax({
        type: "POST",
        url: url,
        data: formData,
        processData: false,
        contentType: false,
        success:function(data){
            if($.trim(data) != "no"){
                $("#PROGRAM_ID").html(data);
                $("#PROGRAM_ID").trigger("chosen:updated");
            }               
        }
    });

Upvotes: 3

CodeGodie
CodeGodie

Reputation: 12142

After checking out the Chosen docs, there seems to not be a "source" option. What you need to do is first run your Ajax call, then fill your select options. Once the select is all filled, then run Chosen on that select element.

I would use the following JS code:

var url = "../BUS/WebService.asmx/LIST_BU";
$.getJSON(url, function(json){
    var $select_elem = $("#cb_info");
    $select_elem.empty();
    $.each(json, function (idx, obj) {
        $select_elem.append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
    });
    $select_elem.chosen({ width: "95%" });
})

Upvotes: 5

Brian Crist
Brian Crist

Reputation: 814

Ok, After some time with the help of suggestions from everybody, I have done

 function load_cb_info() {
            $.ajax({
                type: "POST",
                url: "../BUS/WebService.asmx/LIST_BU",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    $("#cb_info").html('');
                    $.each($.parseJSON(data.d), function (idx, obj) {
                    //$.each(data, function (idx, obj) {
                        $("#cb_info").append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
                    });
                    $("#cb_info").trigger("liszt:updated");
                    $("#cb_info").chosen({ width: "95%" });
                },
                error: function (data) {
                    console.log(data.d);
                }
            });
        }

And , I think this is an answer and everyone else can find it .Thank you.

Upvotes: 3

Gagan Jaura
Gagan Jaura

Reputation: 719

I have changed your jsfiddle. Try this out http://jsfiddle.net/GaganJaura/by4d528c/2/

I have moved the chosen() to bottom.

$("#cb_info").empty();
$.each(data, function (idx, obj) {
    $("#cb_info").append('<option value="' + obj.BU_ID + '">' + obj.BU_NAME + '</option>');
}); 
  $("#cb_info").trigger("liszt:updated");

$("#cb_info").chosen();

Upvotes: 1

Related Questions