【问题标题】:mark all children as checked将所有子项标记为已选中
【发布时间】:2019-06-19 12:24:27
【问题描述】:

我正在尝试制作以下树形视图:一旦检查了类别,我正在努力将整个分支标记为已检查。 我不确定如何让我的 Treeview 选择所有子字段并将其复选框标记为“已选中”,如果父项被选中。 任何指导/有用的教程表示赞赏。 我的 Treeview.js

    $.fn.extend({
    treed: function (o) {

      var openedClass = 'glyphicon-minus-sign';
      var closedClass = 'glyphicon-plus-sign';

      if (typeof o != 'undefined'){
        if (typeof o.openedClass != 'undefined'){
        openedClass = o.openedClass;
        }
        if (typeof o.closedClass != 'undefined'){
        closedClass = o.closedClass;
        }
      };

        /* initialize each of the top levels */
        var tree = $(this);
        tree.addClass("tree");
        tree.find('li').has("ul").each(function () {
            var branch = $(this);
            branch.prepend("");
            branch.addClass('branch');
            branch.on('click', function (e) {
                if (this == e.target) {
                    var icon = $(this).children('i:first');
                    icon.toggleClass(openedClass + " " + closedClass);
                    $(this).children().children().toggle();
                }
            })
            branch.children().children().toggle();

        });

        tree.find('.branch>label').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });

    }
});
/* Initialization of treeviews */
$('#tree1').treed();

这是我的类别视图:

{!! Form::open(['route'=>'submit.category']) !!}
                    <h3>Category List</h3>
                    <ul id="tree1">
                        @foreach($categories as $category)
                            <li>
                                                    {{$category->title}}
                                @if(count($category->childs))
                                    @include('manageChild',['childs' => $category->childs])
                                @endif
                            </li>
                        @endforeach
                    </ul>
                            <div class="form-group">
                                <button class="btn btn-success">Submit</button>
                            </div>

                        {!! Form::close() !!}

这里是managechild.php:

<ul>
@foreach($childs as $child)
    <li>
                  <label><input type="checkbox" value="{{$child->id}}" name="option[]">{{ $child->title }}
</label>
    @if(count($child->childs))
            @include('manageChild',['childs' => $child->childs])
        @endif
    </li>
@endforeach
</ul>

【问题讨论】:

    标签: javascript jstree


    【解决方案1】:

    您能否检查以下链接、其树结构和多个其他示例,如您的要求。

    https://www.jstree.com/plugins/

    【讨论】:

    • 我已经尝试实现它,它需要一些奇怪的数据 JSON 结构,我正在努力使我的实现尽可能简单。
    • 您能分享一下您的 HTML 输出或显示您的刀片文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2014-10-26
    • 2013-01-10
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多