带有复选框的Kendo TreeList

时间:2014-12-18 14:46:46

标签: kendo-ui kendo-asp.net-mvc kendo-treeview

我正在使用Kendo UI TreeList控件,并且需要每个父节点都是一个复选框。我已经搜索了如何使用TreeList中的复选框,但还没有找到任何示例。有谁知道怎么做或者甚至可能?

4 个答案:

答案 0 :(得分:2)

我在treelist kendo UI中工作,我绑定了父ID,并创建了另一个列名称" manualparentid"这也是parentid ..可能我做错了但你可以把条件放在模板上

//Created Separate Class
public class TreePermission
{
   public int menuid { get; set; }
   public int parentmenuid { get; set; }
   public string menuname { get; set; }
   public bool isadd { get; set; } 
   public int manualparentid { get; set; }
}


 Html.Kendo().TreeList<TSBBAL.BAL.TreePermission>()
.Name("GridRolePermissions")
.Columns(colums =>
{
    colums.Add().Field(e => e.menuname).Width(150).Title("Menu Name");
    colums.Add().Field(e => e.isadd).Width(100).Title("Add").TemplateId("addtemplate"); 
}).DataSource(data => data.AutoSync(false)
    .ServerOperation(true)
    .Model(m =>
    {
        m.Id(f => f.menuid);
        m.ParentId(f => f.parentmenuid);
        m.Field(f => f.menuname);
        m.Field(f => f.isadd);  
        m.Expanded(true);
    }).Read(read => read.Action("MenuList", "User"))
)
  • 创建模板addtemplate

     <script id="addtemplate" type="text/x-kendo-template">  
    <div class="col-md-6">       
    
    # if(manualparentid !=0) {# 
        <input type="checkbox" onclick="CheckThePermission(#=menuid #,#=manualparentid #,'add')" id="chkadd_#=menuid #" mid="#=menuid #"  pid="add_#=manualparentid #"   name="chkadd" #= isadd ? checked='checked' : '' # /> 
    #} else {#   
        <input type="checkbox" onclick="CheckChildPermission(#=menuid #,'add')" id="chkadd_#=menuid #" mid="#=menuid #"  pid="add_#=manualparentid #"   name="chkadd" #= isadd ? checked='checked' : '' # /> 
    
    #}#
    </div>  
    

     <script>
             function CheckThePermission(id,pid,obj) {
        //you will get the parent id 
        }
    </script>
    

希望这会有所帮助......

答案 1 :(得分:0)

您可以使用CSS替换箭头(精灵背景)和复选框。

.k-i-expand, .k-plus, .k-plus-disabled {
   background: url(http://i.stack.imgur.com/zQtSf.png) top left !important;
}

.k-i-collapse, .k-minus, .k-minus-disabled {
   background: url(http://i.stack.imgur.com/5tDmY.png) top left !important;
}

enter image description here enter image description here

enter image description here

答案 2 :(得分:0)

只有父母的复选框很容易通过模板完成。这是一个示例模板:

<script id="treelist-checkbox-template" type="text/x-kendo-template">
    # if ( hasChildren ) { #
        <input type='checkbox' name='checkedFiles[#= id #]' value='true' />
    # } #
</script>

然后使用这样的模板:

$("#treelist").kendoTreeList({
    dataSource: dataSource,
    height: 540,
    columns: [
        { template: kendo.template($("#treelist-checkbox-template").html()), width: 32 },
        { field: "Position", expandable: true },
        { field: "Name" },
        { field: "Phone" }
    ],
    // Other options here
});

以下是一个有效的Dojo示例:http://dojo.telerik.com/@mrtaunus/eXiVO

答案 3 :(得分:0)

我正在使用KendoTreeList政策管理的复选框请看我希望有用的示例

政策模型

  public class PolicyModel
{
    public PolicyModel(){}
    public string PolicyId { get; set; }
    public string PolicyName { get; set; }
    public string ParentID{ get; set; }
 }

PolicyTreeListModel

 public class PolicyTreeListModel
{
    public PolicyTreeListModel(){}
    public string id { get; set; }
    public string text { get; set; }
    public List<PolicyTreeListModel> items { get; set; }
    public bool expanded { get; set; }
 }

政策webAPI控制器

    public class PoliciesController : ApiController
    {
        public IEnumerable<PolicyTreeListModel> GetPoliciesTree()
        {
            List<PolicyTreeListModel> ToReturen = new List<PolicyTreeListModel>();

            List<PolicyModel> DataFromDB = new List<PolicyModel>();
            using (var db = DBManager.CreateNewContext())
            {
                var data = db.Policies.ToList();
                foreach (var item in data)
                {
                    DataFromDB.Add(new PolicyModel() { PolicyId = item.PolicyId, PID = item.PID });
                }
            }
            foreach (var item in DataFromDB.Where(e => e.PID == null))
            {
                toreturen.Add(new PolicyTreeListModel()
                {
                    id = item.PolicyId,
                    text = item.PolicyName,
                    expanded = true,
                    items = GetSubItems(DataFromDB.ToList(), new List<PolicyTreeListModel>(), item.PolicyId)
                });
            }

            return ToReturen.OrderByDescending(e => e.text).ToList();
        }

     public List<PolicyTreeListModel> GetSubItems(List<PolicyModel> OrginalData,List<PolicyTreeListModel> SubItems,string PID)
        {
                foreach (var item in OrginalData.Where(e=>e.PID==PID))
                {
                    if (!SubItems.Any(e => e.id == item.PolicyId))
                        SubItems.Add(new PolicyTreeListModel() { id = item.PolicyId, text = item.PolicyName, expanded = true, items = GetItems(OrginalData, new List<Models.PolicyTreeListModel>(),item.PolicyId)});
                }
                return SubItems;
        }
    }

HTML代码

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" href="styles/kendo.common.min.css" />
        <link rel="stylesheet" href="styles/kendo.default.min.css" />
        <link rel="stylesheet" href="styles/kendo.default.mobile.min.css" />
        <link rel="stylesheet" href="styles/bootstrap.min.css" />
        <script src="js/jquery.min.js"></script>
        <script src="js/kendo.all.min.js"></script>
    </head>
    <body>
        <div class="col-lg-12">
            <div class="form-group" style="width:100%">
                <div class="form-inline" style="background-color: #428bca;">
                    <input class="k-textbox" placeholder="Search..." style="font-size:smaller;width:230px;margin-left:2px" id="searchTree" />
                    <label style="font-size:smaller;padding:2px;margin:2px;color:white"><input class="checkbox" style="font-size:smaller;padding:2px;margin:2px" id="chbAll" type="checkbox" value="" /><b>Select All</b></label>
                    <label style="font-size:smaller;padding:2px;margin:2px;color:white"><input class="checkbox" style="font-size:smaller;padding:2px;margin:2px" id="expandAll" type="checkbox" value="" /><b>Expand All</b></label>
                    <button class="btn btn-success" id="savePolicies" style="padding:5px;margin:2px;width:100px;font-family:sans-serif"><i class="fa fa-floppy-o" style="font-size:medium"> Save</i></button>
                    <button class="btn btn-default" id="clearPolicoies" style="padding:5px;margin:2px;width:100px;font-family:sans-serif"><i class="fa fa-eraser" style="font-size:medium;"> Clear</i></button>
                </div>
                <div id="policiestree" style="height:520px;width:100%;background-color: gainsboro;font-size:small;text-align:left;font-weight:bold"></div>
            </div>
        </div>
 <script>
            $(document).ready(function() {
     $("#policiestree").kendoTreeView({
                checkboxes: { checkChildren: true },
                dataSource: [],
            });
            $("#searchTree").on("input", function () {
                var query = this.value.toLowerCase();
                var dataSource = $("#policiestree").data("kendoTreeView").dataSource;
                filter(dataSource, query);
            });
            $("#chbAll").click(function () {
                chbAllOnChange();
            });
            $("#expandAll").attr("checked", true);
            $("#expandAll").click(function () {
                var treeView = $("#policiestree").data("kendoTreeView");
                if (this.checked) { treeView.expand(".k-item"); }
                else { treeView.collapse(".k-item"); }
            });
            function filter(dataSource, query) {
                var hasVisibleChildren = false;
                var data = dataSource instanceof kendo.data.HierarchicalDataSource && dataSource.data();
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    var text = item.text.toLowerCase();
                    var itemVisible =
                        query === true // parent already matches
                        || query === "" // query is empty
                        || text.indexOf(query) >= 0; // item text matches query
                    var anyVisibleChildren = filter(item.children, itemVisible || query); // pass true if parent matches
                    hasVisibleChildren = hasVisibleChildren || anyVisibleChildren || itemVisible;
                    item.hidden = !itemVisible && !anyVisibleChildren;
                }
                if (data) {
                    // re-apply filter on children
                    dataSource.filter({ field: "hidden", operator: "neq", value: true });
                }
                return hasVisibleChildren;
            }
            function checkUncheckAllNodes(nodes, checked) {
                for (var i = 0; i < nodes.length; i++) {
                    nodes[i].set("checked", checked);

                    if (nodes[i].hasChildren) {
                        checkUncheckAllNodes(nodes[i].children.view(), checked);
                    }
                }
            }
            function chbAllOnChange() {
                var checkedNodes = [];
                var treeView = $("#policiestree").data("kendoTreeView");
                var isAllChecked = $('#chbAll').prop("checked");
                checkUncheckAllNodes(treeView.dataSource.view(), isAllChecked)
            }
            function GetPoliciesTree() {
                $.ajax({
                    url: "/api/Policies/GetPoliciesTree",
                    success: function (result) {
                        $("#policiestree").data("kendoTreeView").dataSource.data(result);
                    }
                });
            }
});
 </script>
    </body>
    </html>