Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Fix the problem with set_id function in jsTree

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...

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!