EasyUI Forum
June 16, 2024, 02:32:31 AM *
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 - How should I build a Drop Down List from database  (Read 19124 times)
correohs
Newbie
*
Posts: 11


View Profile
« on: February 03, 2013, 12:12:20 PM »

I want to made a dropdown list using the easyui. I tried different option with out luck

<input class="easyui-combobox"
         name="lo_client_id"
         data-options="
               url:'get_clients.php',
               valueField:'client_code',
               textField:'client_desc',
               panelHeight:'auto'
         ">



With the combobox_data1.json file works my get_client_php does not work.

I read that formatter can help on it. I dont know how to uses it, even when I saw examples of it I couldn't figure it out.  

Could Anyone could help me on it?

Thanks


My get_client_php bring this info.



{"total":"4","rows":
[{
"client_id":"1","client_code":"341896","client_desc":"Subway,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"2","client_code":"341897","client_desc":"Wendy,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"4","client_code":"341898","client_desc":"McDonald,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"6","client_code":"351889","client_desc":"Taco Bell, Inc","client_location_id":"0","client_location_desc":""
}]}


The example combobox_data1.json file bring this info


[{
   "id":1,"text":"Java","desc":"Write once, run anywhere"
},{
   "id":2,"text":"C#","desc":"One of the programming languages designed for the Common Language Infrastructure"
},{
   "id":3,"text":"Ruby","selected":true,   "desc":"A dynamic, reflective, general-purpose object-oriented programming language"
},{
   "id":4,"text":"Perl","desc":"A high-level, general-purpose, interpreted, dynamic programming language"
},{
   "id":5,"text":"Basic","desc":"A family of general-purpose, high-level programming languages"
}]
« Last Edit: February 07, 2013, 08:49:14 AM by correohs » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: February 03, 2013, 07:13:58 PM »

The data to fill the combobox list should be an array object. To solve this issue, make the 'get_client.php' returning json array:
Code:
[{
"client_id":"1","client_code":"341896","client_desc":"Subway,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"2","client_code":"341897","client_desc":"Wendy,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"4","client_code":"341898","client_desc":"McDonald,Inc","client_location_id":"0","client_location_desc":""
},{
"client_id":"6","client_code":"351889","client_desc":"Taco Bell, Inc","client_location_id":"0","client_location_desc":""
}]

Another way to solve this issue is to override the loader object.
Code:
var cloader = function(param, success, error){
var opts = $(this).combobox('options');
if (!opts.url) return false;
$.ajax({
type: opts.method,
url: opts.url,
data: param,
dataType: 'json',
success: function(data){
if (data.rows){
success(data.rows);  // used the data.rows array to fill combobox list
} else {
success(data);
}
},
error: function(){
error.apply(this, arguments);
}
});
}
Then apply this cloader object to combobox.
Code:
<input class="easyui-combobox"
         name="lo_client_id"
         data-options="
               url:'get_clients.php',
               loader: cloader,
               valueField:'client_code',
               textField:'client_desc',
               panelHeight:'auto'
         ">
Logged
correohs
Newbie
*
Posts: 11


View Profile
« Reply #2 on: February 04, 2013, 07:07:29 AM »

Thanks!

I modified my php file

$items = array();
   while($row = mysql_fetch_object($rs)){
      array_push($items, $row);
   }
   $result = $items;

   echo json_encode($result);

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!