Treeview itemAction: how to set checked based on field in datasource?

4 posts, 0 answers
  1. Scott
    Scott avatar
    28 posts
    Member since:
    May 2015

    Posted 02 Nov 2015 Link to this post

     If I have a treeview with .DataSource(d=>d.Model(....).Read(r=>r.Action(....)))

    Can I use .ItemAction() to set item.checked based on a field provided in the DataSource?  How?

    In the code below, what would I put in place of the bolded comment?

    @( Html.Kendo()
           .TreeView()
           .DataTextField("Name")
           .Name("userPermissionsTree")

           .Checkboxes(cbxConfig => cbxConfig.Enabled(true)
                                             .CheckChildren(true)
                                             .Name("checkedNodes"))
           .DataSource(d => d.Model(m =>
                                    {
                                        m.Field("enabled");
                                        m.Id("permissionId")
                                    })
           .Read(read => read.Action("PermissionsTree_Read",
                                                       "SettingsUsers",
                                                       new {
                                                           userId = Model
                                                       })))
          .ItemAction(item=>item.Checked=    /*******set to true if field enabled is true**********/    )
          .Events(events => events
            .Check("onPrivilegeCheck"))
          )

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 05 Nov 2015 Link to this post

    Hello,

    The ItemAction method can only be used if the treeview is bound on the server. If the data is loaded via Ajax then you should set the checked field to true for the items returned from the server that should be checked.

    Regards,
    Daniel
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Scott
    Scott avatar
    28 posts
    Member since:
    May 2015

    Posted 05 Nov 2015 in reply to Daniel Link to this post

    My question is how to do that, if ItemAction does not work.

     It appears that the Model in the DataSource does not allow for a checked field (see below).  So when/where/how would this be done?

    .DataSource(d => d.Model(m =>
                             {
                                 m.Field("enabled");
                                 m.Id("permissionId");
                                 m.checked("enabled"); // this is not available
                             })

  5. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 06 Nov 2015 Link to this post

    Hello again,

    There isn't a configuration option to set the checked field name. You should include a field named checked in the items. Another option is to set the fields from option via the Custom builder:
    .DataSource(dataSource => dataSource.Custom()
        .Transport(transport => transport
            .Read(read => read
                .Action("Action", "controller")
            )
        )
        .Schema(schema=> schema
            .Model(model =>
            {
                m.Field("checked", typeof(bool)).From("enabled");
                m.Id("permissionId");
            })
        )
    )
    but note that the enabled field will not be available in the items with this approach. 

    Regards,
    Daniel
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready