简体   繁体   English

在 AJAX 调用上重新加载数据时丢失格式

[英]Losing formatting on reload of data on AJAX call

I am having an issue when I reload the jstree with data from my AJAX call.当我使用来自 AJAX 调用的数据重新加载 jstree 时遇到问题。 So on load it works fine, but adding data, and getting a response back the tree loses its structure所以在加载它工作正常,但添加数据,并得到响应树失去了它的结构

Initial html is an empty div, data is updated on button click using ajax初始 html 是一个空 div,使用 ajax 在按钮单击时更新数据

<div id="pnlTree">                  

  <div id="jstree" style="text-align: left">

  </div>
</div>

success: function (data) 
        {
            if( data )
            {
                    var output = '<ul class="appointmentlist">';
                    $.each(data, function(key, val){

                        output += "<li id='"+data[key].data1+"' name='dtpurchase'"+counter+"' value='" + data[key].data2 + "'>" +dtdata + "</li>";
                        //output += '<a id="dtappointment" name="dtpurchase" value="' + data[key].data3 + '"'+ '>'+data[key].purchase_date + '</a>';
                         //output += '</li>';
                         counter++;

                    });
                    output += '</ul>';
                $('#jstree').html(output);
            }

        },

I have tried refreshing, reloading but nothing seems to fix the issue.我尝试过刷新,重新加载,但似乎没有任何解决问题的方法。

Soooo what you need to do is to refresh() the tree after you updated the data. Soooo 您需要做的是在更新数据后refresh()树。 Now this works only if you append the date to the element with $("#jstree").jstree(true).settings.core.data = output .现在这仅适用于 append 元素的日期$("#jstree").jstree(true).settings.core.data = output If you use $('#jstree').html(output) then it doesn't work如果你使用$('#jstree').html(output)那么它不起作用

Run the snippets to see it in action:运行代码片段以查看它的实际效果:

Working:在职的:

 $('#jstree').jstree() function update() { var output = `<ul><li>New Item</li></ul>` $("#jstree").jstree(true).settings.core.data = output $('#jstree').jstree(true).refresh(); }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> <div id="jstree"> <ul> <li>Item</li> <li>Item</li> <li>Item</li> </ul> </div> <button onclick="update()">Update data</button>

Okay, so this second one is working with AJAX.好的,所以第二个正在使用 AJAX。 It doesn't work in the Stack Overflow sandbox, but it does work on a normal HTML page (see screenshots).它在 Stack Overflow 沙箱中不起作用,但它在正常的 HTML 页面上起作用(参见屏幕截图)。 It's the exact same concept, you just need to apply it in the right order:这是完全相同的概念,您只需要以正确的顺序应用它:

 $('#jstree').jstree() function update() { $.ajax({ url: "https://swapi.dev/api/people/1", success: function(data){ var output = `<ul> <li>${data.name}</li> <li>${data.eye_color}</li> <li>${data.height}</li> </ul>` $("#jstree").jstree(true).settings.core.data = output $('#jstree').jstree(true).refresh(); } }); }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> <div id="jstree"> <ul> <li>Item</li> <li>Item</li> <li>Item</li> </ul> </div> <button onclick="update()">Update data</button>

在 AJAX 调用之前

AJAX 调用后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM