lundi 30 novembre 2015

Parent node checked but child node is not checked when want to retrieve saved data from database TreeView checkbox


I have did the checkbox treeview. It is successfully save to my database as well. But it just did not checked it back the checkbox. It only checked the parent node, but the child node is still remaining unchecked.

Here is the javascript for the checkbox.

<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);
            }
        }
    });
})

behind code to save:

foreach (TreeNode tn in tvPermission.CheckedNodes)
{
    Page p = new Page();
    p.Name = tn.Text;
    p.Url = tn.Value;
    validation.AddRange(PageManager.Save(p));
}

this is the behind code to display it back:

foreach (TreeNode tn in tvPermission.Nodes)
    tn.Checked = (role.AccessPages.Where(p => p.Url == tn.Value).ToList().Count() > 0);




Aucun commentaire:

Enregistrer un commentaire