onkami
onkami

Reputation: 9411

Change AJAX options in jstree AND reload the tree from server

I am loading XML flat tree in my jsTree using ajax, so the declaration looks like this (it works fine):

  $("#jstree").jstree({
        "xml_data": {
            //  "data": $xmlFlatData,
            "ajax": {
                type: "POST",
                async: true,
                "url": loc + "/AjaxReturnTree",
                data: '{"longnames":"'+flag+'"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (msg) { 
                        var mm = eval('(' + msg + ')'); ; // create object to get rid of json
                        return mm.d;
                },
                error: function () {
                    // TODO process error
                }
            },
            "xsl": "flat",
            "override_ui": "true",
            "real_checkboxes": "true"
        },
        "plugins": ["xml_data", "themes", "checkbox", "ui"]
    });

Now I need to reload the tree AND change the "longnames" part to another flag (it's either 0/1), but keep other options unchanged.

I am trying to use something like this:

           $("#jstree").jstree({
                "xml_data": {
                    "ajax": {
                     cache: false,
                        data: '{"longnames":"' + flag + '"}'
                    }
                }
            });
            $("#jstree").jstree("refresh");

But it does not trigger new AJAX request, only refreshes the tree on screen without reload.

How I can get tree to reload from server?

Also, how I can be sure that I change properties of old ajax setup, not creating a completely new tree object?

Upvotes: 3

Views: 9682

Answers (2)

Crow Soup
Crow Soup

Reputation: 5

should remove parentheses of "data:getData" Or, jQuery will execute getData() and keep only the result from first execution. Without parentheses, jQuery will trade the value of "data" as a function and run it every time. BTW, beware the "cache: false", jQuery will post "_" cacheBurster.

Upvotes: 0

blissfulignant
blissfulignant

Reputation: 46

    flag = 0;

    function getData() {
        return '{"longnames":"' + flag + '"}';
    }

    $("#jstree").jstree({
        "xml_data": {
            "ajax": {
                cache: false,
                data: getData()
            }
        }
    });

    $("#jstree").jstree("refresh"); //refresh with flag = 0

    flag = 1;
    $("#jstree").jstree("refresh"); //refresh with flag = 1

Upvotes: 3

Related Questions