1

我需要做一个三态树视图复选框,但到目前为止,我只能使其进入两种状态:

  1. 如果所有孩子都检查过,则父母检查。

  2. 如果部分孩子检查,父母没有检查。

但是如果部分孩子被选中,我想做到这一点,父母显示一个方形检查。这是我的代码的 JavaScript:

<script type="text/javascript">
$(function () {
    $("[id*=tvPermission] input[type=checkbox]").bind("click", function () {
        var table = $(this).closest("table");
        if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
            //Is Parent CheckBox
            var childDiv = table.next();
            var isChecked = $(this).is(":checked");

            $("input[type=checkbox]", childDiv).each(function () {
                if (isChecked) {
                    $(this).prop("checked", true);
                } else {
                    $(this).prop("checked", false);
                }
            });
        } else {
            //Is Child CheckBox
            var parentDIV = $(this).closest("DIV");

            if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
                $("input[type=checkbox]", parentDIV.prev()).prop("checked", true);
            } else {
                $("input[type=checkbox]", parentDIV.prev()).prop("checked", false);
            }
        }
    });
})

如何修改它以使其在树视图中成为三态,因为使用此代码,它仅显示两种状态。

有什么更好的方法可以实现三态模式?

顺便说一句,这是我的树视图的 asp.net 代码:

<asp:TreeView ID="tvPermission" runat="server" ShowCheckBoxes="All" NodeWrap="true" NodeIndent="2">

希望可以有人帮帮我。真的很感激。

提前致谢。

4

0 回答 0