So i'm trying to make jsTree from sql database via php. I have seen a lot of examples, and what i have got at this moment. This is my php code:
case 'create_node':
$node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
$nodeText = isset($_GET['text']) && $_GET['text'] !== '' ? $_GET['text'] : '';
$statement = $pdo->prepare("INSERT INTO table(id, name, text, parent_id) VALUES (DEFAULT, ?, ?, ?)");
$statement->execute(array($nodeText, $nodeText, $node));
$last_id = $statement->fetchColumn(); //use because PostgreSQL
$result = array('id' => $last_id);
break;
echo json_encode($result); // output is {id:23} etc
And this is part of JS code:
$(document).ready(function () {
$('#tree-container').jstree({
'plugins': ['contextmenu', 'dnd', 'wholerow'],
'core': {
'data': {
'url': 'response.php?operation=get_node',
'data': function (node) {
return {
'id': node.id
};
},
"dataType" : "json"
},
'check_callback': true,
'themes': {
'responsive': false
}
}
}).on('create_node.jstree', function (e, data) {
$.get('response.php?operation=create_node', {
'id': data.node.parent,
'position': data.position,
'text': data.node.text
})
.done(function (d) {
data.instance.set_id(data.node, d.id);
})
.fail(function () {
data.instance.refresh();
});
So in the end of operation i will have this error message: TypeError: id is undefined (in set_id function parameter in jstree.js)
My tree is working but after adding new node i need to refresh page to edit text in this node (i want create and rename with one operation like in example on library site). Thanks for helping...