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 update option. #3

Open
wants to merge 1 commit 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
61 changes: 43 additions & 18 deletions listgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,7 @@
if ($select.attr('multiple'))
$listGroup.attr('data-toggle', 'items');

$select.find('option').each(function (i, item) {
var $item = $(item);

var $new = $('<a>')
.attr('href', '#')
.addClass('list-group-item')
.attr('data-value', $item.val())
.text($item.text());

if ($item.is(':disabled'))
$new.addClass('disabled');

if ($item.css('display') === 'none')
$new.addClass('hidden');

$listGroup.append($new);
});
this.duplicateOptions($select, $listGroup);

$select.change(function () {
$listGroup.listgroup({
Expand Down Expand Up @@ -160,6 +144,45 @@

return $listGroup;
};

/**
* Update the listgroup. It is useful when you update the select element and you want to copy the options
* in the listgroup.
*/
SelectList.prototype.update = function() {
var $select = this.$element;

//Remove old values
var $listGroup = this.$element.data('listgroup').$listGroup;
$listGroup.empty();

this.duplicateOptions($select, $listGroup);
}

/**
* Duplicates the options from the select element to the listgroup.
* @param $select The jQuery select object.
* @param $listGroup The jQuery listgroup object.
*/
SelectList.prototype.duplicateOptions = function($select, $listGroup) {
$select.find('option').each(function (i, item) {
var $item = $(item);

var $new = $('<a>')
.attr('href', '#')
.addClass('list-group-item')
.attr('data-value', $item.val())
.text($item.text());

if ($item.is(':disabled'))
$new.addClass('disabled');

if ($item.css('display') === 'none')
$new.addClass('hidden');

$listGroup.append($new);
});
}


// LIST GROUP PLUGIN DEFINITION
Expand All @@ -175,12 +198,14 @@
: new ListGroup(element, option)));

if (option) {

if (option.unselect)
list.unselect(option.unselect);

if (option.select)
list.select(option.select);

if (option == "update")
list.update();
}
});
};
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,21 @@

equal($listgroup.find('.list-group-item.active').length, 0, 'all item are unselected')
})

test('should update listgroup after select is updated and the update option is called', function () {
var $select = $('<select>_</select>')
.append('<option value="1" selected>foo</option>')
.append('<option value="2" selected>foo</option>')
.listgroup()

var $listgroup = $select.siblings('.list-group')

$select.empty();

$select.append('<option value="3" >foo</option>');

$select.listgroup('update');

equal($listgroup.find('.list-group-item').length, 1, 'items are updated')
})
})