I'm trying to build a two-level multi-select control using nested ULs and jqueryUI selectable.
Currently, I'm restricting selections to the Child level, but what I really want is to be able to select a Parent LI and have all it's Child LIs select as well. Going further, I would want to be able to select all Child LIs and have their Parent be selected. Anything less than all Child LIs selected would result in the Parent being un-selected.
The basic markup:
<ul id="theParentList">
<li>Parent 1
<ul>
<li>Child 1.1</li>
<li>Child 1.2</li>
</ul>
</li>
<li>Parent 2
<ul>
<li>Child 2.1</li>
<li>Child 2.2</li>
</ul>
</li>
</ul>
and the current javascript:
$('#theParentList').selectable({filter: 'li li'});
How would I accomplish the first part: selecting a Parent selects all of it's Children?
Thanks!
UPDATE:
I've got it most of this concept working now:
Selecting a Parent selects all of it's Children;
Deselecting a Child will deselect it's Parent
What still isn't working: Selecting all of a Parent's Children should select the Parent
Here's what I've got, at this point:
Markup:
<ul id="theParentList">
<li class="level-1">
<div>Parent 1</div>
<ul class="level-2">
<li><div>Child 1.1</div></li>
<li><div>Child 1.2</div></li>
</ul>
</li>
<li class="level-1"><div>Parent 2</div>
<ul class="level-2">
<li><div>Child 2.1</div></li>
<li><div>Child 2.2</div></li>
</ul>
</li>
</ul>
and the js:
function SelectSelectableElement (selectableContainer, elementsToSelect){
$(elementsToSelect).not(".ui-selected").addClass("ui-selecting");
}
function handleSelected (){};
function handleSelection (El){
if( $(El).parent().hasClass('level-1')){
var ch = $(El).parent().find('.level-2 .ui-selectee');
SelectSelectableElement('#theParentList', ch);
}else{
//this is a level-2 item
//if El and all of it's level-2 siblings are selected, then also select their level-1 parent
}
};
function handleUnselected (El){
if( $(El).parent().hasClass('level-1') ){
//unselect all of it's children
$(El).parent().children().each( function(){
$(this).find('.ui-selectee').removeClass('ui-selected').addClass('ui-unselected');
});
}else{
//this is a level-2 item, so we need to deselect its level-1 parent
$(El).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
}
};
$('#theParentList').selectable({
filter: 'li div',
selecting: function(event, ui){
handleSelection(ui.selecting);
},
selected: function(event, ui) {
handleSelected(ui.selected);
},
unselected: function(event, ui) {
handleUnselected(ui.unselected);
}
});
Here's a fiddle: http://jsfiddle.net/JUvTD/