EasyUI Forum
April 29, 2024, 09:33:54 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: combobox receives json in non expected format  (Read 3619 times)
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« 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
Logged
jarry
Administrator
Hero Member
*****
Posts: 2262


View Profile Email
« Reply #1 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;
}
})
Logged
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« Reply #2 on: December 23, 2021, 02:49:29 AM »

Thanks a lot. Works fine.
I was unsure about how loadFilter() method works

Juan Antonio
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!