如何在JStree中创建新节点时添加属性?在JSTree中创建新节点时添加属性
当btnadd点击那个时候我正在添加新的节点。 节点创建成功,但现在我想添加属性。
我的HTML
<div id="jstree">
</div>
<button id="btnadd">create node</button>
<div id="CurentNodeName"></div>
<div id="CurentNodeId"></div>
脚本
<script>
var CurrentNode;
$(function() {
var data = [
{ "id": "ajson1", "parent": "#", "text": "Simple root node" },
{ "id": "ajson2", "parent": "#", "text": "Root node 2" },
{ "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
{ "id": "ajson4", "parent": "ajson2", "text": "Child 2" },
];
$("#jstree").jstree({
"core": {
// so that create works
"check_callback": true,
"data": data
}
}).on('create_node.jstree', function (e, data) {
console.log('saved');
});
// listen for event
$('#jstree').on('changed.jstree', function (e, data) {
var i, j, r = [], s = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
CurrentNode = data.selected[0];
console.log(data.selected[0]);
}
$('#CurentNodeName').html('Selected: ' + r.join(', '));
$('#CurentNodeId').html('Selected Id: ' + CurrentNode);
}).jstree();
// create the New Node when Button Click
$("#btnadd").on("click", function() {
$('#jstree').jstree().create_node(CurrentNode, { "id": "ajson5" + CurrentNode, "text": "newly added" }, "last", function() {
});
});
</script>
我也尝试 “ATTR” 是:{title: “如果”,值: “表达”}}但是这不能帮助我。
尝试'数据[I] .attr = {标题:“如果”,值:“表达”}' – manuzi1