我正在尝试使用嵌套的UL和jqueryUI selectable构建一个两级多选控件。
目前,我正在将选择限制在子级别,但我真正想要的是能够选择父级LI并且还选择所有它的子级LI。更进一步,我希望能够选择所有Child LIs并选择他们的Parent。如果选择少于所有Child LI,则会导致未选择Parent。
基本标记:
<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>
和当前的javascript:
$('#theParentList').selectable({filter: 'li li'});
我如何完成第一部分:选择父母选择所有儿童?
谢谢!
更新
我现在已经掌握了这个概念的大部分内容:
选择父母选择所有儿童;
取消选择一个孩子将取消选择它的父母
什么仍然不起作用:选择所有父母的孩子应该选择父母
这就是我现在所拥有的:
标记:
<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>
和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);
}
});
这是一个小提琴:http://jsfiddle.net/JUvTD/
答案 0 :(得分:1)
回答我自己的问题,以防其他人需要相同的帮助
选择父母将选择其所有孩子 选择所有孩子将选择他们的父母 取消选择父母将取消选择所有孩子 取消选择一个孩子也将取消选择它的父母
这是一个工作小提琴: http://jsfiddle.net/QvqCE/1/
和javascript
$('#theParentList').selectable({
filter: 'li div',
selecting: function (event, ui) {
if ($(ui.selecting).parent().hasClass('level-1')) {
//this is a level-1 item, so select all of it's children
var ch = $(ui.selecting).parent().find('.level-2 .ui-selectee');
$(ch).not(".ui-selected").addClass("ui-selecting");
} else {
//this is a level-2 item, so check to see if all of it's siblings are also selected
var sibs = $(ui.selecting).parent().siblings().find('.ui-selectee');
var notSelected = 0;
for (var i = 0; i < sibs.length; i++) {
if ($(sibs[i]).hasClass('ui-selected') || $(sibs[i]).hasClass('ui-selecting')) {
notSelected = notSelected
} else {
notSelected = notSelected + 1;
}
}
if (notSelected === 0) { //all siblings are selected, so select their level-1 parent as well
$(ui.selecting).parent().parent().parent().find('>:first-child').not(".ui-selected").addClass("ui-selecting");
}
//otherwise, just select as usual
}
},
unselected: function (event, ui) {
if ($(ui.unselected).parent().hasClass('level-1')) {
//unselect all of it's children
$(ui.unselected).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
$(ui.unselected).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
}
}
});
答案 1 :(得分:0)
除了rolfsf的答案之外,如果你想要将父母和孩子绑在一起的行为略有不同:
如果仍然选择了所有孩子,则父母将保持选中状态。以及
如果取消选择子项,也取消选择父级,
您可以将此回调函数添加到可选小部件的初始化中:
unselecting: function (event, ui) {
if ($(ui.unselecting).parent().hasClass('level-1')) {
//if all children still selected, don't deselect this
var sibs = $(ui.unselecting).next().find('.ui-selectee');
if (sibs.length > 0) {
var notSelected = 0;
for (var i = 0; i < sibs.length; i++) {
if ($(sibs[i]).hasClass('ui-selected') || $(sibs[i]).hasClass('ui-selecting')) {
notSelected = notSelected;
} else {
notSelected = notSelected + 1;
}
}
if (notSelected === 0) { //all children still selected, so keep their level-1 parent selected as well
$(ui.unselecting).addClass("ui-selecting");
}
}
} else {
//if unselecting a child, unselect parent as well
$(ui.unselecting).parent().parent().prev().removeClass('ui-selected').removeClass("ui-selecting");
}
}
请在此处查看jsfiddle:http://jsfiddle.net/gav9q/