Refresh combobox data

When using JS 1 combobox, I uset this to refresh combobox data from the client side:

In controller:
[HttpGet]
        [OutputCache(Duration = 1)]
        public JsonResult GetMjesta()
        {
            ViewBag.MjestoSel = db.Mjesto.OrderBy(o => o.Naziv).ToList();
            return Json(ViewBag.MjestoSel, JsonRequestBehavior.AllowGet);
        }

In view:
var src = '@Url.Action("GetMjesta", "Mjestoes", new { area = "" })';
                $.ajax({
                    cache: false,
                    url: src,
                    type: "get",
                }).done(function (data) {
                    $("#cboxMjesto").ejAutocomplete({
                        dataSource: data
                    });
                }).fail(function () {
                    alert('error');
                });

Is there a way to do something like that in JS 2 combobox?

Thanks!

3 Replies

PO Prince Oliver Syncfusion Team April 30, 2018 05:32 AM UTC

Hi Bernard, 

Thank you for contacting Syncfusion support. 

Yes, you can access the refreshed EJ2 Combo box’s data source in client. This can be done by accessing the control’s instance and setting the new data source in the dataSource property. Then you need to call the dataBind method to apply those changes in the control. Kindly refer to the following example code snippet. 

<script> 
    var src = '@Url.Action("GetMjesta", "Mjestoes", new { area = "" })'; 
    $.ajax({ 
        cache: false, 
        url: src, 
        type: "get", 
    }).done(function (data) { 
        // Creating instance for Combo box 
       var comboObj = document.getElementById("cboxMjesto").ej2_instances[0];  
       // updating the new data in dataSource property 
        comboObj.dataSource = data; 
        // calling the dataBind method to update the changes in combo box 
        comboObj.dataBind(); 
    }).fail(function () { 
        alert('error'); 
    }); 
</script> 

Regards, 
Prince 



BJ Bernard Jurlina April 30, 2018 07:26 AM UTC

Excellent, Prince, thank you!

And how about setting the value from the client? In EJ1 I used $('#cboxUgo').ejComboBox({ value: parseInt(uid) }); 

In EJ2 I tried 
var c_pid = document.getElementById('cboxPartner').ej2_instances[0];
c_pid.value = pid;
but it's not working. Maybe I have to call c_pid.dataBind() after setting the value?

Thanks!
Bernard.


PO Prince Oliver Syncfusion Team May 1, 2018 06:22 AM UTC

Hi Bernard,  
 
Thank you for your update.  
 
Yes, you need to call the dataBind method to apply those changes in the control, after setting the value property. Kindly refer to the following example code snippet.  
 
<script>  
        // Creating instance for Combo box  
       var c_pid= document.getElementById("cboxPartner").ej2_instances[0];  
       // setting the value using value property  
        c_pid.value= pid;  
        // calling the dataBind method to update the changes in combo box  
        c_pid.dataBind();  
</script>  
 
Regards,  
Prince  



Loader.
Up arrow icon