Remove item based on value

This commit is contained in:
Josh Johnson 2016-04-08 09:07:41 +01:00
parent 50c53ce3ad
commit b9bae53e53
2 changed files with 45 additions and 22 deletions

File diff suppressed because one or more lines are too long

View file

@ -55,10 +55,11 @@ export class Choices {
} }
// Bind methods // Bind methods
this.init = this.init.bind(this);
this.render = this.render.bind(this);
this.destroy = this.destroy.bind(this);
this.onKeyDown = this.onKeyDown.bind(this); this.onKeyDown = this.onKeyDown.bind(this);
this.onClick = this.onClick.bind(this); this.onClick = this.onClick.bind(this);
this.render = this.render.bind(this);
this.init = this.init.bind(this);
// Let's have it large // Let's have it large
this.init(); this.init();
@ -312,25 +313,44 @@ export class Choices {
* Remove item from store * Remove item from store
* @param * @param
*/ */
removeItem(item) { removeItem(itemOrValue) {
if(!item) { if(!itemOrValue) {
console.error('removeItem: No item was passed to be removed'); console.error('removeItem: No item or value was passed to be removed');
return; return;
} }
let id = item.getAttribute('data-choice-id'); // We are re-assigning a variable here. Probably shouldn't be doing that...
let value = item.innerHTML; let item;
if(itemOrValue.nodeType) {
// Run callback item = itemOrValue;
if(this.options.callbackOnRemoveItem){ } else {
if(isType('Function', this.options.callbackOnRemoveItem)) { for (var i = this.list.children.length - 1; i >= 0; i--) {
this.options.callbackOnRemoveItem(value); let listItem = this.list.children[i];
} else { if(listItem.innerHTML === itemOrValue.toString()) {
console.error('callbackOnRemoveItem: Callback is not a function'); item = listItem;
break;
}
} }
} }
this.store.dispatch(removeItemFromStore(id)); if(item) {
let id = item.getAttribute('data-choice-id');
let value = item.innerHTML;
// Run callback
if(this.options.callbackOnRemoveItem){
if(isType('Function', this.options.callbackOnRemoveItem)) {
this.options.callbackOnRemoveItem(value);
} else {
console.error('callbackOnRemoveItem: Callback is not a function');
}
}
this.store.dispatch(removeItemFromStore(id));
} else {
console.error('Item not found');
}
} }
/** /**
@ -517,12 +537,12 @@ export class Choices {
delimiter: ' ', delimiter: ' ',
editItems: true, editItems: true,
maxItems: 5, maxItems: 5,
// callbackOnRemoveItem: function(value) { callbackOnRemoveItem: function(value) {
// console.log(value); console.log(value);
// }, },
// callbackOnAddItem: function(item, value) { callbackOnAddItem: function(item, value) {
// console.log(item, value); console.log(item, value);
// } }
}); });
let choices2 = new Choices({ let choices2 = new Choices({
@ -553,4 +573,7 @@ export class Choices {
element: input6, element: input6,
items: ['josh@joshuajohnson.co.uk', 'joe@bloggs.co.uk'] items: ['josh@joshuajohnson.co.uk', 'joe@bloggs.co.uk']
}); });
choices6.addItem('josh2@joshuajohnson.co.uk');
choices6.removeItem('josh@joshuajohnson.co.uk');
})(); })();