user9955311
user9955311

Reputation:

How to send data to dynamic jstree with alternative json format from django

I tried the following process but query set has totally changed its format.

$('#jstree').jstree({
        'core' : {
             'data' :{{data}}  # here main issue
               },
        plugins : ["wholerow","unique","dnd","sort","state","contextmenu","contextmenubtn","themes"]
    }).on("ready.jstree", function (e, data) { data.instance.open_all(); });

Query set I sent in response:

data = Buildkb4.objects.values("id","text","parent")
return render(request,"registration/basetest.html", 
     {"data": data})

Output:

No table structure and no data, on page source code I got following output for the query:

 data= <QuerySet [{'text': 'PKM', 'parent': 0, 'id': 1}, {'text': 'develop', 'parent': 1, 'id': 2}, {'text': 'social', 'parent': 1, 'id': 3}]>

Upvotes: 1

Views: 545

Answers (1)

Pranay reddy
Pranay reddy

Reputation: 160

I have some basic experience with jstree and django, this solution may help you

Here your are directly passing the query set to data in template which is not acceptable. It accepts json data type.

Design a view which will return json data, you required in jstree.

from django.http import JsonResponse
def table(request):
   data = table.objects.values("column1","column2")
   data_list = list(data)
   return JsonResponse(data_list,safe=False) 

now in your template add:

var arraycollection =(function() {
    var json = null;
    $.ajax({
        'async': false,
        'global': false,
        'url': "####### url to acces data"
        'dataType': "json",
        'success': function (data) {
            json = data;
        }
    });
    return json;
})();

Now assign arraycollection to data in jstree

Upvotes: 1

Related Questions