[英]What is the best practices to make checkbox tree using indeterminate javascript HTML?
I am developing a checkbox tree where i need three states of checkbox, I know there is two state of checkbox, but i have to use indeterminate我正在开发一个复选框树,我需要三种状态的复选框,我知道复选框有两个 state,但我必须使用不确定
Check查看
Uncheck取消选中
Indeterminate不定
How can i developed these type of tree using javascript?我如何使用 javascript 开发这些类型的树?
<div id="page-wrap">
<h1>Indeterminate Checkboxes</h1>
<ul>
<li>
<input type="checkbox" name="tall" id="tall">
<label for="tall">Tall Things</label>
<ul>
<li>
<input type="checkbox" name="tall-1" id="tall-1">
<label for="tall-1">Buildings</label>
</li>
<li>
<input type="checkbox" name="tall-2" id="tall-2">
<label for="tall-2">Giants</label>
<ul>
<li>
<input type="checkbox" name="tall-2-1" id="tall-2-1">
<label for="tall-2-1">Andre</label>
</li>
<li>
<input type="checkbox" name="tall-2-2" id="tall-2-2">
<label for="tall-2-2">Paul Bunyan</label>
</li>
</ul>
</li>
<li>
<input type="checkbox" name="tall-3" id="tall-3">
<label for="tall-3">Two sandwiches</label>
</li>
</ul>
</li>
<li>
<input type="checkbox" name="short" id="short">
<label for="short">Short Things</label>
<ul>
<li>
<input type="checkbox" name="short-1" id="short-1">
<label for="short-1">Smurfs</label>
</li>
<li>
<input type="checkbox" name="short-2" id="short-2">
<label for="short-2">Mushrooms</label>
</li>
<li>
<input type="checkbox" name="short-3" id="short-3">
<label for="short-3">One Sandwich</label>
</li>
</ul>
</li>
</ul>
</div>
$(function() {
// Apparently click is better chan change? Cuz IE?
$('input[type="checkbox"]').change(function(e) {
var checked = $(this).prop("checked"),
container = $(this).parent(),
siblings = container.siblings();
container.find('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
function checkSiblings(el) {
var parent = el.parent().parent(),
all = true;
el.siblings().each(function() {
return all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
});
if (all && checked) {
parent.children('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
checkSiblings(parent);
} else if (all && !checked) {
parent.children('input[type="checkbox"]').prop("checked", checked);
parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
checkSiblings(parent);
} else {
el.parents("li").children('input[type="checkbox"]').prop({
indeterminate: true,
checked: false
});
}
}
checkSiblings(container);
});
});
You can use already developed plugins (or read their code if you are just curious how to manipulate the checkboxes' states).您可以使用已经开发的插件(如果您只是好奇如何操作复选框的状态,也可以阅读它们的代码)。 Just drop one of them.
只需放下其中一个。 If you choose niTree plugin I can help you with any questions concerning the code:
如果您选择niTree插件,我可以帮助您解决有关代码的任何问题:
https://github.com/AlexLibs/niTree https://github.com/AlexLibs/niTree
This is a trivial one from scratch: http://jsfiddle.net/pimvdb/MgYs7/1/ .这是一个从头开始的微不足道的: http://jsfiddle.net/pimvdb/MgYs7/1/ 。
var span = document.getElementById('span');
span.setAttribute('data-state', 0);
span.onselectstart = function() { return false }; // prevent selecting, doesn't look too good
span.onclick = function() {
var state = span.getAttribute('data-state') - 0; // convert to number
if(state === 0) { // set next symbol
span.innerHTML = 'V';
} else if(state === 1) {
span.innerHTML = 'X';
} else {
span.innerHTML = '';
}
span.setAttribute('data-state', (state + 1) % 3); // set state; 3 will be 0 again
};
I would use values -1, 0 and 1. Every onclick the attached function checks its value.我会使用值 -1、0 和 1。每个 onclick 附加的 function 都会检查它的值。
-1 = Indeterminate -1 = 不确定
0 = unchecked 0 = 未选中
1 = checked. 1 = 检查。
If 0 change to 1如果 0 变为 1
If 1 change to -1如果 1 变为 -1
If -1 change to 0如果 -1 变为 0
If the value = -1 add this property (eg via jQuery) to the checkbox: $(checkbox).prop("indeterminate", true);
如果 value = -1 添加这个属性(例如通过 jQuery)到复选框:
$(checkbox).prop("indeterminate", true);
For every other value the indeterminate value should be false
.对于每个其他值,不确定值应该是
false
。
See this link for example例如,请参阅此链接
Of course your tree logic should act on what its value is going to be eg unchecking children or setting parent at indeterminate.当然,您的树逻辑应该根据其值将采取行动,例如取消选中子项或将父项设置为不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.