前提・実現したいこと
jsTreeで最下層(葉っぱ)だけがcheck可能で、それ以外(親)のcheckboxをdisableにする方法が知りたいです。
ツリー生成時に子をもたないノードのチェックボックスをDisableにしたいです。
該当のソースコード
<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.5/jstree.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.5/themes/default/style.min.css" /> <script> $(function(){ var data1 = [{ "id": "W", "text": "World", "state": { "opened": true }, "children": [{"text": "Asia","disable_node":"true"}, {"text": "Africa"}, {"text": "Europe", "state": { "opened": false }, "children": [ "France","Germany","UK" ] }] }]; $('#Tree').jstree({ core: { data: data1, check_callback: false }, checkbox: { three_state : false, // to avoid that fact that checking a node also check others whole_node : false, // to avoid checking the box just clicking the node tie_selection : false // for checking without selecting and selecting without checking }, plugins: ['checkbox'] }) .on( "ready.jstree", function( e, data ) { $( "#Tree" ).jstree( "disable_node", "Europe" ); }) .on("check_node.jstree uncheck_node.jstree", function(e, data) { <!-- var parent_node = $("#Tree").jstree().get_node(data.node).parents; --> var parent_node = $("#Tree").jstree().get_node(data.node).children; if(parent_node.length>0) { data.node.state.checked=false; data.node.state.disabled=true; <!-- $("#Tree").jstree().disable_node(node); --> return false; } alert(data.node.id + ' ' + data.node.text + (data.node.state.checked ? ' CHECKED': ' NOT CHECKED')) }).bind('before.jstree', function(event, data){ switch(data.plugin){ case 'ui': if(data.inst.is_leaf(data.args[0])){ return false; } break; default: break; } }).on( "ready.jstree", function( e, data ) { var parent_node = $("#Tree").jstree().get_node(data.node).children; if(parent_node.length>0) { data.node.state.checked=false; data.node.state.disabled=true; <!-- $("#Tree").jstree().disable_node(node); --> return false; } } ); }); </script> </head> <body> <div id="Tree"> </div> </body> </html>
回答1件
あなたの回答
tips
プレビュー