Skip to content

Commit

Permalink
work around for jquery bug #3827 where the logic handling on a checkb…
Browse files Browse the repository at this point in the history
…ox.trigger() is completely opposite from native element.click(). fixes #47
  • Loading branch information
ehynds committed Dec 14, 2010
1 parent ec0fd9e commit b23ff7e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/jquery.multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,9 @@ $.widget("ech.multiselect", {
return this.value === val;
}).attr('selected', (checked ? 'selected' : ''));

// issue 14: if this event is natively fired, the box will be checked
// before running the update. using trigger(), the events fire BEFORE
// the box is checked. http://dev.jquery.com/ticket/3827
self.update( !e.originalEvent ? checked ? -1 : 1 : 0 );
// setTimeout is to fix multiselect issue #14 and #47. caused by jQuery issue #3827
// http://bugs.jquery.com/ticket/3827
setTimeout($.proxy(self.update, self), 10);
});

// close each widget when clicking on any other element/anywhere else on the page
Expand Down Expand Up @@ -408,15 +407,11 @@ $.widget("ech.multiselect", {
},

// updates the number of selected items in the button
update: function( offset ){
if( offset === undefined ){
offset = 0;
}

update: function(){
var o = this.options,
$inputs = this.labels.find('input'),
$checked = $inputs.filter(':checked'),
numChecked = $checked.length + offset,
numChecked = $checked.length,
value;

if( numChecked === 0 ){
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,64 @@
el.multiselect("destroy");
});

test("selectedList", function(){
expect(2);

var html = '<select multiple><option value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>';

el = $(html).multiselect({
selectedList: 3
});

el.multiselect("checkAll");
equals( button().text(), 'foo, bar, baz', 'after checkAll, button text is a list of all options in the select');
el.multiselect("destroy");

el = $(html).multiselect({
selectedList: 2
});

el.multiselect("checkAll");
equals( button().text(), '3 selected', 'after checkAll with a limited selectedList value, button value displays number of checked');
el.multiselect("destroy").remove();
});

function asyncSelectedList( useTrigger, message ){
expect(1);
stop();

var html = '<select multiple><option value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>',
checkboxes;

el = $(html).appendTo("body").multiselect({
selectedList: 2
});

checkboxes = el.multiselect("widget").find(":checkbox");

if( useTrigger ){
checkboxes.eq(0).trigger('click');
checkboxes.eq(1).trigger('click');
} else {
checkboxes.eq(0)[0].click();
checkboxes.eq(1)[0].click();
}

setTimeout(function(){
equals( button().text(), 'foo, bar', message);
el.multiselect("destroy").remove();
start();
}, 10);
}

test("selectedList - manual trigger - jQuery", function(){
asyncSelectedList( true, 'manually checking items with trigger()' );
});

test("selectedList - manual trigger - native", function(){
asyncSelectedList( false, 'manually checking items with element.click()' );
});

test("height", function(){
expect(2);

Expand Down

0 comments on commit b23ff7e

Please sign in to comment.