Kendo Treeview with checkboxes, pre-select items on load and pass selected items on button click

3 posts, 1 answers
  1. Dayana
    Dayana avatar
    2 posts
    Member since:
    Feb 2016

    Posted 22 Apr Link to this post

    Hi,

     

    I am using kendo treeview with checkboxes in ASP .Net MVC. The datasource for the treeview is a list of item and want some of them to be selected on loading the treeview and then I want to pass the newly selected items to the controller. I can achieve each one of the two separately(with a line of code for each functionality), but they seem to be mutually exclusive.

     

    This is my code for the view:

    @(Html.Kendo().TreeView()
                            .Name("PrescriptionProductInstructions")
                            .DataTextField("Name")
                            .Checkboxes(x => x.CheckChildren(true))
                            .CheckboxTemplate("<input type='checkbox' name='checkedFiles[#= item.id #]' value='#= item.id #' #if(item.IsChecked){# checked #}#/>")
                            .Events(events => events.DataBound("onDataBound"))
                            .Checkboxes(x => x.Name("PrescriptionProductInstructions"))                        
                            .DataSource(source => source.Read(read => read.Action("GetInstructions", "Note", new { productId = Model.ProductId, personId = Model.PersonId }))))

        <script>
             function onDataBound() {
                $("#PrescriptionProductInstructions").data("kendoTreeView").expand(".k-item");
            }
        </script>                         

     

    The first bold line of code helps me pre-select some item when loading the tree and the second line of code helps me pass the list of ids of the selected items.

     

    My question is whether I can have both functionalities as the same time? If yes, how?

     

    Thanks in advance.

     

    Regards,

     

    Dayana Hadzhipetrova

  2. Answer
    Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 26 Apr Link to this post

    Hello,

    If you need to use template and set specific name to the input then you should set the name in the template:
    .CheckboxTemplate("<input type='checkbox' name='PrescriptionProductInstructions' value='#= item.id #' #if(item.IsChecked){# checked #}#/>")


    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Dayana
    Dayana avatar
    2 posts
    Member since:
    Feb 2016

    Posted 26 Apr Link to this post

    Thanks for the reply. It all works now.

     

    Regards,

     

    Dayana

Back to Top