EasyUI Forum
November 30, 2025, 06:45:03 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: Howto remove items from a combobox dynamically?  (Read 13414 times)
Stefan B.
Full Member
***
Posts: 152


Software Architekt


View Profile Email
« on: March 19, 2015, 01:04:23 AM »

I want to remove items from a combobox dynamically.
How can i do this?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 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();
})
}
})
Logged
Stefan B.
Full Member
***
Posts: 152


Software Architekt


View Profile Email
« Reply #2 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.

Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 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
Logged
Stefan B.
Full Member
***
Posts: 152


Software Architekt


View Profile Email
« Reply #4 on: March 23, 2015, 12:50:06 AM »

THX it works fine  Cheesy
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!