This is a migrated thread and some comments may be shown as answers.

Get Partial Checked item

14 Answers 650 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kuangyi
Top achievements
Rank 1
Kuangyi asked on 02 Oct 2012, 08:46 PM
Hi, I need to send back checked items in a hierarchical tree view and the state they're in.  The state has to have 3 values, checked, unchecked and the "partial" check where only some children are checked (and the checkbox itself is "filled").

How do I get this state?  I see no class, or anything that tells me it's partial (short of looking through it's child tree and looking for checked items, which I don't want to do).

14 Answers, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 05 Oct 2012, 03:12 PM
Hello Kuangyi,

The indeterminate checkboxes are not checked and they are not send to the server. To send them you can attach a handler to the submit event of the form and check them programatically.

Here is an example:
<script type="text/javascript">
    $(function () {
        $('form').submit(function () {
            $('#treeview :checkbox').each(function () {
                if (this.indeterminate) { this.checked = true; }
            });
        })
    })
</script>



All the best,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 01 Oct 2013, 02:04 AM
I have a need to send via JSON the $("#treeView").data("kendoTreeView").dataSource.view() back to the server for processing.

I have tried  $('#treeView :checkbox').each(function () {                if (this.indeterminate) { this.checked = true; }            });
prior to the type POST as suggested in the blogs above this one, but the checkboxes which are indeterminate  are being translated via JSON as false.
(The ones that have the full checkmark are being relayed correctly as true)

I have also tried:
$("#treeView :checkbox").each(function () {
                        if ($(this).attr("indeterminate")) {
                                currentDataItem = $("#treeView").data("kendoTreeView").dataItem($(this).closest("li"));
                                currentDataItem.set("checked", true);
                            }
                    });
which gives the same result.
My current v is Kendo UI Web Q1 2013

Any suggestions that will allow this to work would be welcome.
0
Petur Subev
Telerik team
answered on 03 Oct 2013, 06:25 AM
Hello Alan,

How did you configure your TreeView and how you send it to the server? Share your implementation so we can search for potential issues, also if possible try to demonstrate your case with a JsBin example we will be able to advise you more precisely.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 03 Oct 2013, 10:50 AM
Petur 

I would be happy to generate all the things you have asked for but before I do, I would like to make sure that I am going down the right path, can you point me to the Kendo docs that describes more about the treeview's checkbox indeterminate state property?

Thank you
0
Petur Subev
Telerik team
answered on 08 Oct 2013, 08:54 AM
Hello Alan,

Basically the 'indeterminate' state of the treeview with checkboxes is enabled automatically when checkChildren option is set to true.

http://docs.kendoui.com/api/web/treeview#configuration-checkboxes.checkChildren


Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 08 Oct 2013, 05:34 PM
Sorry, I must have been unclear before.

After the checkChildren option is set to true in a treeview what is the best way to access the "indeterminate" property on a given checkbox via javasript?

Thank you
0
Alex Gyoshev
Telerik team
answered on 09 Oct 2013, 07:46 AM
Hello Alan,

The indeterminate property is not persisted in the datasource, because it is a feature of the browser checkboxes. If you want to post the indeterminate nodes as checked, you need to loop through the checkboxes, and if they are indeterminate, set the checked field in the datasource:

var tree = $("#treeview").data("kendoTreeView");
$("#treeview :checkbox").each(function() {
    if (this.indeterminate) {
         tree.dataItem(this).checked = true;
    }
});

var data = tree.dataSource.view().toJSON();
console.log(data);

Note that the above code does not call the set("checked", true) method, because this would trigger the checkChildren behavior of the treeview and check all children.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 18 Nov 2013, 01:20 AM
Alex~

Is there a way to allow  or enable a tri-state checkbox on a kendo Tree view without having any children?
i.e. I have the following javascript in place:

$("#treeView").kendoTreeView({
                checkboxes: {
                    checkChildren: true
                },


but the datasource for this example will never have any children.

Thanks
Alan Painter
0
Alex Gyoshev
Telerik team
answered on 19 Nov 2013, 08:31 AM
Hello Alan,

What is the expected result of that? A TreeView without children will not show any items, and checkboxes are associated with items, so there will be no checkboxes shown. Still, you can enable the tri-state checkboxes and add new items to the tree afterwards.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 19 Nov 2013, 11:38 AM
Sorry I was not clear in asking my question.

using the following code:

$("#treeView").kendoTreeView({
                checkboxes:  true,
                dataSource: [{
                      text: "1st", 
                      text: "2nd",
                      text: "3rd"
                 ] }
                }

What would be the best way to make the checkboxes tri-state?

This example does not have any children but for my needs the checkboxes need to be tri-state.

Thank you
0
Alex Gyoshev
Telerik team
answered on 19 Nov 2013, 02:01 PM
Hello Alan,

I'm afraid that this functionality is not supported. If you need to enable the tri-state of the checkboxes, you may do so with a custom script, as shown in this demo from CSS-tricks. Note that this approach isn't related to the TreeView and can be used standalone.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alan
Top achievements
Rank 2
answered on 19 Nov 2013, 08:47 PM
Alex ~

Many thanks for the article. Using the code from the section called "Casual Trash" (yes that is what the article called him/her) suggests the following pattern
may work:

$("#treeView").kendoTreeView({
                checkboxes: {
                    template: "<input type='checkbox' id='cb1' onclick='ts(this)' />"
                },

function ts(cb) {
            if (cb.readOnly) cb.checked = cb.readOnly = false;
            else if (!cb.checked) cb.readOnly = cb.indeterminate = true;
        };

However the onclick event does not seem to fire within the kendo Treeview. Any suggestions on how to make this work?
Thank you

0
Alan
Top achievements
Rank 2
answered on 20 Nov 2013, 08:08 PM
I have went ahead and submitted this as Kendo ticket 760686

Thank you
0
Alex Gyoshev
Telerik team
answered on 21 Nov 2013, 08:47 AM
Hello Alan,

This code will not work along with the checkbox handling in the TreeView. Since you do not have a hierarchy, you don't actually need a TreeView -- you can use the same markup for the items, and use simple HTML. Here's a sample jsBin that shows this.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
TreeView
Asked by
Kuangyi
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Alan
Top achievements
Rank 2
Alex Gyoshev
Telerik team
Share this question
or