EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: ejzhang on March 18, 2015, 06:34:32 PM



Title: Howto add items to a combobox dynamically?
Post by: ejzhang on March 18, 2015, 06:34:32 PM
I want to add items to a combobox dynamically many times in scripts, and keep the items before added.
How can i do? thanks very much!


Title: Re: Howto add items to a combobox dynamically?
Post by: stworthy on March 18, 2015, 08:36:50 PM
Please extend an 'appendItem' method to achieve this functionality.
Code:
$.extend($.fn.combobox.methods, {
appendItem: function(jq, item){
return jq.each(function(){
var state = $.data(this, 'combobox');
var opts = state.options;
var items = $(this).combobox('getData');
items.push(item);
$(this).combobox('panel').append(
'<div id="' + state.itemIdPrefix+'_'+(items.length-1) + '"  class="combobox-item">' +
(opts.formatter ? opts.formatter.call(this, item) : item[opts.textField]) +
'</div>'
)
})
}
})

Usage example:
Code:
$('#cc').combobox('appendItem', {
  id: '12',
  text: 'text12'
});