Is it possible to populate jstree using a loop?

Dumisani

Hi I'd like to know if its possible to populate my jstree using a loop instead of hard-coding each node. Below is my code:

api.call("Get", {
   typeName: "Device"
}, function (result) {
   $('#jstree').jstree({
      'plugins': ["checkbox", "sort"],
      'core': {
         'data':
            [
               { id: result[0].id, text: result[0].name },
               { id: result[1].id, text: result[1].name }
            ]
        }
    });
});

I make an api call then populate my tree with the returned results. How can I format my JSON using a loop?

This is the main part I need to change:

[
   { id: result[0].id, text: result[0].name },
   { id: result[1].id, text: result[1].name }
]

This is what I've tried:

[
    function () {
       for(var i = 0; i < result.length; i++){
          { id: result[i].id, text: result[i].name }
       }
    }
]

Thank you.

shyam

Sure.

api.call("Get", {
   typeName: "Device"
}, function (result) {
   $('#jstree').jstree({
      'plugins': ["checkbox", "sort"],
      'core': {
         'data': (function () {
           var results = [];
           for(var i = 0; i < result.length; i++){
             results.push({ id: result[i].id, text: result[i].name });
           }
           return results;
         })()
        }
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is this loop possible to do using simple LINQ?

From Dev

Is it possible to index using a variable of the for/loop in vhdl?

From Dev

Populate integers to dropdownlist using a loop

From Dev

Using a For loop to populate an array

From Dev

How do I populate an array with palindromes using a loop?

From Dev

Is it possible to populate ListView with List

From Dev

populate jsTree from Sharepoint list

From Dev

jsTree - Populate Tree Dynamically using AJAX/C#Web Method

From Dev

Possible to populate two levels?

From Dev

How to dynamically populate an array using a for in loop

From Dev

Is it possible to populate an array in a foreach loop

From Dev

Is it possible to change the number in an object name by using a loop?

From Dev

How to populate a dropdown in rails using a loop?

From Dev

Is it possible to loop a counter in a property using Stylus?

From Dev

Using Java 8 Stream to replace for loop and populate a Map

From Dev

How to populate a matrix using for loop

From Dev

How to create and populate structure like below by using for loop? ObjectiveC

From Dev

Is it possible to create MVVM properties using for/while loop?

From Dev

Is it possible to index using a variable of the for/loop in vhdl?

From Dev

Populate a table with data from a database using a loop

From Dev

Is it possible to populate jstree using a loop?

From Dev

Using a for loop, how do I populate a lambda where condition?

From Dev

SQL - Using a select statement and loop to populate a stored procedure

From Dev

Populate array with integer sequence using for loop without crashing Chrome

From Dev

Is it possible to populate an array in a foreach loop

From Dev

Using tryCatch to populate a data.frame inside a loop... nicely

From Dev

T-SQL Populate Column Using While Loop

From Dev

Populate text field from JsTree selection

From Dev

Populate list using a for loop

Related Related

  1. 1

    Is this loop possible to do using simple LINQ?

  2. 2

    Is it possible to index using a variable of the for/loop in vhdl?

  3. 3

    Populate integers to dropdownlist using a loop

  4. 4

    Using a For loop to populate an array

  5. 5

    How do I populate an array with palindromes using a loop?

  6. 6

    Is it possible to populate ListView with List

  7. 7

    populate jsTree from Sharepoint list

  8. 8

    jsTree - Populate Tree Dynamically using AJAX/C#Web Method

  9. 9

    Possible to populate two levels?

  10. 10

    How to dynamically populate an array using a for in loop

  11. 11

    Is it possible to populate an array in a foreach loop

  12. 12

    Is it possible to change the number in an object name by using a loop?

  13. 13

    How to populate a dropdown in rails using a loop?

  14. 14

    Is it possible to loop a counter in a property using Stylus?

  15. 15

    Using Java 8 Stream to replace for loop and populate a Map

  16. 16

    How to populate a matrix using for loop

  17. 17

    How to create and populate structure like below by using for loop? ObjectiveC

  18. 18

    Is it possible to create MVVM properties using for/while loop?

  19. 19

    Is it possible to index using a variable of the for/loop in vhdl?

  20. 20

    Populate a table with data from a database using a loop

  21. 21

    Is it possible to populate jstree using a loop?

  22. 22

    Using a for loop, how do I populate a lambda where condition?

  23. 23

    SQL - Using a select statement and loop to populate a stored procedure

  24. 24

    Populate array with integer sequence using for loop without crashing Chrome

  25. 25

    Is it possible to populate an array in a foreach loop

  26. 26

    Using tryCatch to populate a data.frame inside a loop... nicely

  27. 27

    T-SQL Populate Column Using While Loop

  28. 28

    Populate text field from JsTree selection

  29. 29

    Populate list using a for loop

HotTag

Archive