We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Change the datasource programmatically

I have a dropdownlist that I create with the following code:

$('td#' + selectedCol + ' .units').ejDropDownList({
                                                width: 250,
                                                popupHeight: 300,
                                                watermarkText: "Select a unit",
                                                dataSource: unitsOfMeasure,
                                                fields: {
                                                    text: "text",
                                                    value: "value"
                                                }
                                            });

now, when the user click a button, I'd like to change the dataSource and load another set of items, contained in the variable unitOfMeasure2, 
How can I do it in the fastest and most performant way?

Thanks
Best

1 Reply

PO Prince Oliver Syncfusion Team January 29, 2019 07:36 AM UTC

Hi Marco, 

Thank you for contacting Syncfusion support. 

We can use the dataSource property from the control’s instance and assign new data to the control in the button click handler. Kindly refer to the following code snippet. 

<script> 
    var ddlObj, unitsOfMeasure, unitOfMeasure2; 
    ddlObj = $('td#' + selectedCol + ' .units').ejDropDownList({ 
        width: 250, 
        popupHeight: 300, 
        watermarkText: "Select a unit", 
        dataSource: unitsOfMeasure, 
        fields: { 
            text: "text", 
            value: "value" 
        } 
    }).data("ejDropDownList"); 
 
    function btnClick() { 
        ddlObj.setModel({ dataSource: unitOfMeasure2}); 
    } 
</script> 

We have attached a sample for your reference, please find the sample at the following location: https://jsplayground.syncfusion.com/q2cpcg5q 

Please let us know if you need any further assistance on this. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon