How to dynamically build an array to use as datasource for a kendodropdownlist widget ?

1 Answer 171 Views
Data Source DropDownList
Rafael
Top achievements
Rank 1
Iron
Rafael asked on 19 Sep 2023, 09:38 PM
   

Hello, the following code works like a charm:


var arr = [
      {text:"Option 11",value:"Value 11"},
      {text:"Option 22",value:"Value 22"}
     ];
    

    arr.push({text:"Option 33",value:"Value 33"});

    var datasource = new kendo.data.DataSource({
      data: arr
    });
    datasource.read();

    jQuery("#operation").data("kendoDropDownList").setDataSource(datasource);

However if I try this:


var arr = [
      {text:"Option 11",value:"Value 11"},
      {text:"Option 22",value:"Value 22"}
     ];

for(i=0;i<operators.length;i++) {
      
      var singleElement = {
        text:operators[i],
        value:operators[i]
      };

      arr.push(singleElement);
}

var datasource = new kendo.data.DataSource({
      data: arr
    });

datasource.read();

jQuery("#operation").data("kendoDropDownList").setDataSource(datasource);

I get [object Object] where I should have the elements
of operators as values and text on the dropdownlist.

If I try this:


var arr = [
      {text:"Option 11",value:"Value 11"},
      {text:"Option 22",value:"Value 22"}
     ];

for(i=0;i<operators.length;i++) {
      
      var singleElement = "{text:\""+operators[i]+",value:\""+operators[i]+"\"}";


      arr.push(singleElement);
}

var datasource = new kendo.data.DataSource({
      data: arr
    });

datasource.read();

jQuery("#operation").data("kendoDropDownList").setDataSource(datasource);

I get "undefined" where I should have the elements
of operators as values and text on the dropdownlist.

Any idea on how can get the values from "operators" into the datasource and show up on the dropdownlist ? 

Thanks,

Rafael

 

1 Answer, 1 is accepted

Sort by
0
Rafael
Top achievements
Rank 1
Iron
answered on 20 Sep 2023, 05:37 PM

Update, on this:

for(i=0;i<operators.length;i++) {
      arr.push({text: +':'+operators[i],value: +':'+operators[i]});
}

 

I get the the values I need but preceded by NaN(ie: NaNUser1, NaNUser2...)

 

 

Nikolay
Telerik team
commented on 22 Sep 2023, 03:43 PM

Hi Rafael,

I tried with the first code snippet and the DrowpDownList seems to be loading the data correctly. However, I don't have the "operators" and I am mot able to dig further.

Here is the Dojo I prepared: https://dojo.telerik.com/IyAcOliW

Could you please modify it to showcase the problem?

Regards,

Nikolay

Rafael
Top achievements
Rank 1
Iron
commented on 22 Sep 2023, 05:10 PM

Hello Nikolay, operators is a an array of strings, like "abc", "def", or "ghi". 

I´ve solved it already, for the record, what worked was this:

 user = operators[i];
 arr.push({text: ''+ user, value: ''+ user });

Rafael

Nikolay
Telerik team
commented on 27 Sep 2023, 09:30 AM

Hi Rafael,

Thank you for the update. I am glad to hear you managed to resolve the situation.

Regards,

Nikolay

Tags
Data Source DropDownList
Asked by
Rafael
Top achievements
Rank 1
Iron
Answers by
Rafael
Top achievements
Rank 1
Iron
Share this question
or