lundi 6 juillet 2015

Tree View Checkbox Solution

I have treeview checkbox . i solve every problem but in 1 area i can't solve my problem . That is when child element if not any value checked then parent unchecked . please give me advice how if all child uncheck then parent unchecked

demo work

<ul class="someclass">
<li>
    <input type="checkbox"/>Manager 1
    <ul>
        <li>
            <input type="checkbox"/>Assistant Manager 1
        </li>
        <li>
            <input type="checkbox"/>Assistant Manager 2
        </li>
        <li>
            <input type="checkbox"/>Assistant Manager 3
        </li>
    </ul>
</li>
<li>
    <input type="checkbox"/>Vice President
    <ul>
        <li>
            <input type="checkbox"/>Manager 4
        </li>
        <li>
            <input type="checkbox"/>Manager 5
        </li>
        <li>
            <input type="checkbox"/>Manager 6
        </li>
    </ul>
</li>

$('li :checkbox').on('click', function () {
    var $chk = $(this),
        $li = $chk.closest('li'),
        $ul, $parent;
    if ($li.has('ul')) {
        $li.find(':checkbox').not(this).prop('checked', this.checked)
    }
    do {
        $ul = $li.parent();
        $parent = $ul.siblings(':checkbox');
        if ($chk.is(':checked')) {
            $parent.prop('checked', true)

        } else {
          $parent.prop('checked', $li.has(':checked').length == 0)
           // $parent.prop('checked', false)
        }
        $chk = $parent;
        $li = $chk.closest('li');
    } while ($ul.is(':not(.someclass)'));
});




Aucun commentaire:

Enregistrer un commentaire