Change AJAX options in jstree AND reload the tree from server

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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?

回答1:

     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 


回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!