1

i am using kendo ui tree view with check box

i want the check box's id when it is getting unchecked

this is kendo ui mine code

// var homogeneous contains data

$("#treeview").kendoTreeView({
    checkboxes: {
        checkChildren: false,
        template:"# if(!item.hasChildren){# <input type='hidden' id='#=item.id#' parent_id='#=item.parent_id#' d_text='#=item.value#'/> <input type='checkbox' id_a='#= item.id #' name='c_#= item.id #' value='true' />#}else{# <div id='#=item.id#' style='display:none;' parent_id='#=item.parent_id#' d_text='#=item.value#'/> #}#",

    },
    dataSource: homogeneous,
    dataBound: ondata,
    dataTextField: "value"
});

function ondata() {
   //alert("databound");
}   


// function that gathers IDs of checked nodes
function checkedNodeIds(nodes, checkedNodes) {
    //console.log(nodes);
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].checked) {
            checkedNodes.push(nodes[i].id);
        }

        if (nodes[i].hasChildren) {
            checkedNodeIds(nodes[i].children.view(), checkedNodes);
        }
    }
}

// show checked node IDs on datasource change
$("#treeview").data("kendoTreeView").dataSource.bind("change", function() {
    var checkedNodes = [],
        treeView = $("#treeview").data("kendoTreeView"),
        message;

    checkedNodeIds(treeView.dataSource.view(), checkedNodes);
    if (checkedNodes.length > 0) {
        message = "IDs of checked nodes: " + checkedNodes.join(",");
    } else {
        message = "No nodes checked.";
    }

    $("#result").html(message);
});

in this code i am not getting checkbox's id when it is unchecked so i have tried this jquery code

$('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
       alert('checked');
    } else {
        alert('not checked');
    }
});

this code is only working in js fiddle but not in my case http://jsfiddle.net/NPUeL/

if i use this code then i can get the number of count but i dont know how to use it

    var treeview = $("[data-role=treeview]").data("kendoTreeView");
    treeview.dataSource.bind("change", function (e) {
        if (e.field == "checked") {
            console.log("Recorded Selected: " + $("[data-role=treeview] :checked").length);
        }
    });

what changed i need to do in data source so i can get id thanks in adavance

4

2 回答 2

1

如果你想得到id你可以这样做:

$('input[type=checkbox]').click(function (e) {
    var li = $(e.target).closest("li");
    var id = $("input:hidden", li).attr("id");
    var node = treeView.dataSource.get(id);
    if (node.checked) {
        console.log('checked');
    } else {
        console.log('not checked');
    }
});

我在事件处理程序中所做的是:

  1. 找到最近的li元素,即已单击的树的节点。
  2. id 在一个 HTMLinput元素中hidden(这是我理解你存储它的方式)。
  3. dataSource从使用dataSource.get方法中获取项目。

在此处查看修改并运行的代码

于 2013-06-17T16:34:06.820 回答
1

我做了一个小小的改变,它现在可以工作了

    function ondata() {
   $('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
       alert('checked');
    } else {
        alert('not checked');
    }
  });
}
于 2013-06-18T05:26:31.360 回答