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

dynamic loading of list items

We have tried to create a dropdownlist that fills dynamically

tried to create a dropdownlist empty first and then use additem to add items

$('#selectCar').ejDropDownList({
                targetID: "carsList",
                watermarkText: "Select a car",
                width: "100%"
            });

var target = $('#selectCar').data("ejDropDownList");
var newitems = [
   { text: "India"},
   { text: "Pakistan"},
   ];
           
    target.addItem(newitems  );

this fails. looks like you cannot create a dropdownlist without items
tried it with adding an empty item first this works but I cant remove the empty items

I need to be able to change the items in the list dynamically

thanks




this



2 Replies

MS Michael Salzlechner January 25, 2017 02:11 PM UTC

looks like I may have solved this but if there is a better way let me know

here is what I ended up with

1) create the dropdownlist with an item with blank text
2) use the dataSource option to change the data dynamically

 $('#selectCar').ejDropDownList({
    dataSource: [{text:""}],
                targetID: "carsList",
                watermarkText: "Select a car",
                width: "100%"
            });

....

var newitems = [
   { text: "India"},
   { text: "Pakistan"},
   ];
          
  target.option("dataSource", newitems);












KV Karthikeyan Viswanathan Syncfusion Team January 25, 2017 04:17 PM UTC

Hi Michael Salzlechner,       
 
Thanks for contacting Syncfusion support. 
 
No Need to assign empty value to DropDownList. It will be rendered also without any datasource. Please find the below code snippet: 
 
 
<code> 
 
$('#bikeList').ejDropDownList({ 
                fields: { id: "empid", text: "text", value: "text" } 
            }); 
          $("#btn").click(function(){ 
                                                var inst = $('#bikeList').ejDropDownList("instance"); 
                                                inst.option("dataSource", BikeList); 
                                                 
                                    }); 
 
</code> 
 
 
JS Playground sample link: http://jsplayground.syncfusion.com/zg12b3yt  
 
Please let us know if you have any other queries.  
   
Regards,    
Karthikeyan V.   


Loader.
Live Chat Icon For mobile
Up arrow icon