Chia sẽ code tạo cây checkbox như sau:
Chức năng:
- Click Node cha sẽ check/uncheck tất cả Sub node của nó.
- Check/uncheck tất cả Sub node sẽ tự động check/uncheck Node cha của nó.
Code như sau:
HTML:
<div class="tree">
<ul>
<li>
<div>
<img alt="" class="expand" src="images/minus.png" />
<img alt="" class="collapse" src="images/plus.png" />
</div>
<div>
<input type="checkbox" id="invoice" />
</div>
<div>Node 1</div>
<ul>
<li>
<div>
<label>
<input type="checkbox" id="list" /> Sub node 1 A
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 1 B
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 1 C
</label>
</div>
</li>
</ul>
</li>
<li>
<div>
<img alt="" class="expand" src="images/minus.png" />
<img alt="" class="collapse" src="images/plus.png" />
</div>
<div>
<input type="checkbox" id="payment" />
</div>
<div>Node 2</div>
<ul>
<li>
<div>
<label>
<input type="checkbox" id="payment-list" /> Sub node 2 A
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 2 B
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 2 C
</label>
</div>
</li>
</ul>
</li>
<li>
<div>
<img alt="" class="expand" src="images/minus.png" />
<img alt="" class="collapse" src="images/plus.png" />
</div>
<div>
<input type="checkbox" id="product" />
</div>
<div>Node 3</div>
<ul>
<li>
<div>
<label>
<input type="checkbox" id="product-list" /> Sub node 3 A
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 3 B
</label>
</div>
</li>
<li>
<div>
<label>
<input type="checkbox" /> Sub node 3 C
</label>
</div>
</li>
</ul>
</li>
</ul>
</div>
CSS:
.tree { width:500px }
.tree ul { list-style:none; }
.tree li { padding-top:10px }
.tree ul, .tree li, .tree div { float:left }
.tree ul, .tree li { width:100% }
.tree .expand { width:15px;height:15px; }
.tree .collapse { width:15px;height:15px;display:none }
JS:
$(".tree input[type='checkbox']").click(function() {
var check = this.checked;
// If clicked parent checkbox then check/uncheck all children checkbox
if ($(this).closest("li").find("ul input[type='checkbox']").length > 0) {
$(this).closest("li").find("ul input[type='checkbox']").each(function() {
$(this).prop('checked', check);
});
} else {
// If check/uncheck all children then check/uncheck parent checkbox
$(this).closest("ul").closest("li").find("input[type='checkbox']:first").prop('checked', $(this).closest("ul").find("input[type='checkbox']").not(':checked').length == 0);
}
});
Đăng ký:
Đăng Nhận xét (Atom)
Thời gian và chiếc lá
Thời gian trôi nhanh Đời người mong manh Như lá trên cành Rồi cũng hết xanh Đã rõ tử sanh Chẳng chuộng lợi danh Phong ý trong...
-
Vô sắc lưu ảnh Vô Sắc Lưu Ảnh Á nh nguyệt diệu như tơ Q uấn quanh màu đêm nhạt N am phong cũng thẩn thờ V ...
-
Thạch Thảo Thanh Hoa Diệp lạc viện tử trung Phong xúy quá tư niệm Kiến thạch thảo thanh hoa Thị hữu tình chi mỹ Na...
-
Sơn Thủy Lướt sóng Cái giang tầm kê đảo Thanh sơn bích thủy tựa non bồng Hiếu kỳ, vọng hướng đông tây Trang ngô hữu ngạn, tre...

Không có nhận xét nào:
Đăng nhận xét