EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Stefan B. on March 19, 2015, 01:04:23 AM



Title: Howto remove items from a combobox dynamically?
Post by: Stefan B. on March 19, 2015, 01:04:23 AM
I want to remove items from a combobox dynamically.
How can i do this?


Title: Re: Howto remove items from a combobox dynamically?
Post by: stworthy on March 19, 2015, 08:47:32 AM
Please extend a new 'deleteItem' 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>'
)
})
},
deleteItem: function(jq, index){
return jq.each(function(){
var state = $.data(this, 'combobox');
$(this).combobox('getData').splice(index,1);
var panel = $(this).combobox('panel');
panel.children('.combobox-item:gt('+index+')').each(function(){
var id = $(this).attr('id');
var i = id.substr(state.itemIdPrefix.length+1);
$(this).attr('id', state.itemIdPrefix+'_'+(parseInt(i)-1));
});
panel.children('.combobox-item:eq('+index+')').remove();
})
}
})


Title: Re: Howto remove items from a combobox dynamically?
Post by: Stefan B. on March 20, 2015, 01:37:18 AM
THX for this implementation. But I have 2 other questions.

1) Is there a method to get the current selected Item index?
2) Is there a method to get the complete Item object data? There are only the methodes getText and getValue but not getSelected to fetch the complete item data.



Title: Re: Howto remove items from a combobox dynamically?
Post by: stworthy on March 20, 2015, 08:37:55 AM
Please try the code below to get the selected row object and index.
Code:
var cc = $('#cc');
var state = cc.data('combobox');
var opts = state.options;
var value = cc.combobox('getValue');
var el = opts.finder.getEl(cc[0], value);
var index = el.attr('id').substr(state.itemIdPrefix.length+1);
var row = opts.finder.getRow(cc[0], value);
console.log(row); // the row object
console.log(index) // the row index


Title: Re: Howto remove items from a combobox dynamically?
Post by: Stefan B. on March 23, 2015, 12:50:06 AM
THX it works fine  :D