0

我有一个带有复选框模板的剑道树视图。

我的html代码

<div id="treeview-left" style=" height: 375px; position: absolute;top: 30px;"></div>

我已将树视图定义为:

var treeview1 = $('#treeview-left').kendoTreeView({
            select: onSelect,
            checkboxTemplate: kendo.template($("#treeview-checkbox-template").html()),            
            dragAndDrop: true,
            dataSource: parent, 
            dataTextField: ["parentName", "childName"]
        }).data("kendoTreeView");

复选框模板的定义如下:

<script id="treeview-checkbox-template" type="text/kendo-ui-template">
            <input type="checkbox" />
</script>

数据源将是

var parent = new kendo.data.HierarchicalDataSource({
            type: "POST",
            dataType: "json",
            transport: {
                read: {
                    url: "/Projects/leftdisplay"
                }
            },
            schema: {
                model: {
                    id: "parentid",
                    hasChildren: "childName",
                    children: child
                }
            }
        });

现在父母的孩子会像:

   var child = {
            type: "POST",
            dataType: "json",
            transport: {
                read: {
                    url: function (options) {
                        return kendo.format("/projects/modulesleftdisplay?parentid=" + options.parentid + "&roleid=" + rolevalue + "&projectid=" + projectid + "");
                    }
                }
            },
            schema: {
                model: {
                    id: "childid",
                    hasChildren: function () {
                        return false;
                    }
                }
            }
        };

现在,当我单击子复选框时,我想获取所选子项的父 ID。我怎么能得到那个。

我写过类似的代码

$("#treeview-left [type=checkbox]").live('change', function (e) {      
    var chkBox = $(this)
    var parentid = chkBox.parent();
    var date = $('#treeview-left').data('kendoTreeView').dataItem(parent_id);
    var parid = date.id;
    var childid = $('#treeview-left').data('kendoTreeView').dataItem(parentid);
});

但parid没有得到。如何获取所选孩子的父 ID。任何帮助表示赞赏。

4

2 回答 2

0

尝试这个:

// Find all the checkboxes:
var checkboxes = Array.prototype.slice.call(document.querySelectorAll('#yourFormsID input[type=checkbox]'));

checkboxes.forEach(function(checkbox) {                //<== loop through checkboxes
    checkbox.addEventListener('change', function() {   //<== When clicked:
        console.log(this.parentNode.id);               //<== Log the id of the checkbox's parentNode in the console.
    }, false);
});

这是一个演示。一定要打开控制台。

于 2013-03-14T05:12:33.320 回答
0

你可以用这个

   var parentId = $(this).parent().closest(parent element).attr('id')

例子:

 var parentId  = $(this).parent().closest('li').attr('id')

代替

 var chkBox = $(this)

 var parentid = chkBox.parent();

获取父 ID。

于 2013-03-14T05:15:12.800 回答