EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Juan Antonio Martínez on December 22, 2021, 02:54:53 AM



Title: combobox receives json in non expected format
Post by: Juan Antonio Martínez on December 22, 2021, 02:54:53 AM
Hello.
I have a server that respond to my ajax request with this format:
Code:
[ item1 , item2 , item3 ... ]
And I need to populate combobox.
So use this onLoadSuccess method:
Code:
      
<input class="easyui-combobox" name="domain" id="domain" style="width:90%;"
             data-options="
             url:'/webapp/services/serviceX',
             method:'get',
             valueField:'id',
             textField:'text',
             panelHeight:'auto',
             onLoadSuccess: function() {
              let res=[];
              data=$('#domain').combobox('getData');
              if( typeof(data[0].id)!=='undefined' ) return; // to avoid infinite onLoadSuccess recursion
              for(n=0;n<data.length;n++) { res.push( { 'id': n, 'text': data[n] }); }
              $('#domain').combobox('loadData',res);
             }
      ">

But seems too dirty. What's the right way to do this task?
Thanks in advance.
Juan Antonio


Title: Re: combobox receives json in non expected format
Post by: jarry on December 22, 2021, 07:30:15 PM
Please define the 'loadFilter' function to convert the original data to the expected format.
Code:
$('#cc').combobox({
data:data,
valueField:'id',
textField:'text',
loadFilter: function(data){
var data = $.map(data, function(item,i){
return {id:i+1,text:item}
});
return data;
}
})


Title: Re: combobox receives json in non expected format
Post by: Juan Antonio Martínez on December 23, 2021, 02:49:29 AM
Thanks a lot. Works fine.
I was unsure about how loadFilter() method works

Juan Antonio