2013-03-14 41 views
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是沒有得到。我怎樣才能得到所選孩子的父母身份。任何幫助表示讚賞。

+0

發佈你的html .. – sasi 2013-03-14 05:10:20

+0

我編輯了我的問題 – Jonathan 2013-03-14 05:15:19

+0

@Naidu,檢查一個基本問題:當你計算'date'時,什麼是'treeview'?它是'treeview'還是'treeview-left' ?. – OnaBai 2013-03-14 09:29:37

回答

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); 
}); 

Here's a demo。一定要打開控制檯。

+0

我應該在哪裏使用複選框更改事件和什麼是元素和parentNode – Jonathan 2013-03-14 05:16:30

+0

@Naidu更新更完整的解決方案。 – 2013-03-14 05:21:58

+0

@Naidu也增加了工作演示。 – 2013-03-14 05:31:45

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。

+0

@Naidu是否爲你工作 – PSR 2013-03-14 05:19:42

+0

不,它正在變得不確定。 – Jonathan 2013-03-14 05:20:39

+0

可以在修改後顯示代碼 – PSR 2013-03-14 05:21:08