Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new method 'removeOption' #158

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions documentaion_at_loudev.com.html.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--- index.html 2014-09-18 12:01:30.776022500 +0200
+++ index_new.html 2014-09-18 12:10:26.224562000 +0200
@@ -305,7 +305,32 @@
</table>
<p></p>
<pre><code data-language="javascript" class="rainbow"><span class="selector">$</span>(<span class="string">'#your-select'</span>).<span class="function call">multiSelect</span>(<span class="string">'addOption'</span>, { value: <span class="string">'test'</span>, text: <span class="string">'test'</span>, index: <span class="constant numeric">0</span>, nested: <span class="string">'optgroup_label'</span> });</code></pre>
- <br>
+ <h4>.multiSelect('removeOption', Hash)</h4>
+ <p>
+ Dynamically remove option from the multiselect.
+ <br>
+ The options hash is described bellow:
+ </p><table class="table table-bordered table-striped">
+ <thead>
+ <tr>
+ <th width="120">key</th>
+ <th>type</th>
+ <th>required</th>
+ <th>desription</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>value</td>
+ <td>String</td>
+ <td>true</td>
+ <td>The value of the option to remove</td>
+ </tr>
+ </tbody>
+ </table>
+ <p></p>
+ <pre><code data-language="javascript" class="rainbow"><span class="selector">$</span>(<span class="string">'#your-select'</span>).<span class="function call">multiSelect</span>(<span class="string">'removeOption'</span>, { value: <span class="string">'test'</span> });</code></pre>
+ <br>
<h3>Keyboard</h3>
<table class="table table-bordered table-striped">
<thead>
12 changes: 12 additions & 0 deletions js/jquery.multi-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@
})
},

'removeOption' : function(options){
var that = this;

if (options.value) options = [options];
$.each(options, function(index, option){
if (option.value && that.$element.find("option[value='"+option.value+"']").length === 1){
that.$element.find("option[value='" + option.value + "']").remove();
that.$container.find("ul.ms-list [id^='" + option.value + "']").remove();
}
})
},

'escapeHTML' : function(text){
return $("<div>").text(text).html();
},
Expand Down
37 changes: 37 additions & 0 deletions test/spec/multiSelectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,41 @@ describe("multiSelect", function() {
});
});
});

describe('manipulation of values', function(){

describe('when calling the "addOption" method', function(){
beforeEach(function(){
select.multiSelect();
select.multiSelect('addOption', { value: 'test', text: 'test' });
});

it('should add a new option to the original select', function() {
expect( select.children('option').length ).toEqual(11);
});

it('should add a new li entry to the multiselect container', function() {
expect( $("div.ms-container .ms-selectable li").length ).toEqual(11);
expect( $("div.ms-container .ms-selection li").length ).toEqual(11);
});
});

describe('when calling the "removeOption" method', function(){

beforeEach(function(){
select.multiSelect();
select.multiSelect('removeOption', {value: 'value1' });
});

it('should remove the specified option from the original select', function() {
expect( select.children('option').length ).toEqual(9);
});

it('should remove the specified li entry from the multiselect container', function() {
expect( $("div.ms-container li[id^='value1-']").length ).toEqual(0);
});
});

});

});