Search code examples
checkboxkendo-uitreeviewkendo-asp.net-mvckendo-treeview

Set all checkbox in Kendo Dropdowntree checked when load the page


I want to create kendo dropdowntree where when i load the page, all the checkbox is checked. Here is my code.

    @(Html.Kendo().DropDownTree()
                          .AutoWidth(true)
                          .Name("dropdowntree")
                          .DataTextField("Name")
                          .DataValueField("Id")
                          .CheckAll(true)
                          .HtmlAttributes(new { style = "width: 100%" })
                          .Events(events => events.Change("onChange"))
                          .Filter(FilterType.Contains)
                          .AutoClose(false)
                          .Checkboxes(checkboxes => checkboxes
                              .Name("checkedFiles")
                              .CheckChildren(true)
                          )
                          .DataSource(dataSource => dataSource
                            .Read(read => read
                            .Action("GetName", "CheckBox")
                        )
                        )
    )

I already doing some research and try the solution, but none if them work. For example, what i have try:

$(document).ready(function () {
   $("#dropdowntree input.k-checkbox").prop("checked", true);
})

This one also not work:

$(document).ready(function () {
    $("#dropdowntree").attr('checked', 'checked');
})

This one is work, but i need to set the value. What i need is it checked all by default, no need to set the value.

 $(document).ready(function () {
 var dropdowntree = $("#dropdowntree").data("kendoDropDownTree");
 dropdowntree.value(["1","2","3","4","5","6","7"]); 
 })

Other than all of these, i also try the solution in this link jquery set all checkbox checked and others solution. But still not work. I really need some advice. Thank you.


Solution

  • I already found the answer if there is someone need it.

    First in controller i get all of the list for id, then i change the list into string in json.

    CONTROLLER CODE:

        public IActionResult CheckBoxDB()
        {
            List<string> parts = new List<string>();
            GetId(parts);
            ViewBag.All = parts;
            var json = JsonConvert.SerializeObject(parts);
            ViewBag.change = json;
            return View();
        }
    
        private void GetId(List<string> parts)
        {
            List<DdlcheckBox> ddlcheckBoxes = new List<DdlcheckBox>();
            ddlcheckBoxes = _context.DdlcheckBox.ToList();
            foreach (var data in ddlcheckBoxes)
            {
                string id = data.Id.ToString();
                parts.Add(id);
            }
        }
    

    Then, in the Views, i get the ViewBag value at the script.

     <script>
    $(document).ready(function () {
        var dropdowntree = $("#dropdowntree").data("kendoDropDownTree");
        dropdowntree.value('@ViewBag.change');
    });
     </script>
    

    Thank you.