Home > Web Front-end > JS Tutorial > body text

Analysis of the problem of jQuery Easyui asynchronously loading tree

高洛峰
Release: 2016-12-29 13:29:06
Original
1226 people have browsed it

When I want to load json files from the local and dynamically insert them into ul through events, I encountered a small bug

The code in the html is like this

<ul class="easyui-tree" id="tt"></ul>
Copy after login

Code in js

$(".next-menu:nth-child(1) a").click(function() {
var $IDstr = $(this).attr("id"),
$treeIDNum = parseInt($(this).attr("treeID")),
jsonURL = "json/" + $IDstr + ".json",
node;
addAttr2Tree(jsonURL);
changeImgSrc($treeIDNum);
});
});
function changeImgSrc(nodeID){
var node = $("#tt").tree(&#39;find&#39;, nodeID);
if(node){
$("#tt").tree(&#39;select&#39;, node.target);
}
if (node.attributes) {
$("#img-box").attr("src", node.attributes.url);
}
}
function addAttr2Tree(URL){
$("#tt").tree({
url: URL,
method: "get",
animate: true,
lines: true
});
}
Copy after login

At first, I wanted to dynamically load the content of the tree through a button click event. This is the code above. addAttr2Tree is used To add the local json data corresponding to when the button is clicked to the ul tag in the html, changeImgSrc is some selection operations on the tree node and loading of images, but no matter how you debug, an error will always appear

关于 jQuery Easyui异步加载tree的问题解析

Unable to obtain attributes attribute! ! ! I have repeatedly confirmed that the attributes are intact in the json file and this error always occurs when the button is clicked for the first time. The second time and thereafter, this error does not occur.

Later I thought, is it because the speed of dynamic loading of json data is not as fast as the speed of program code execution? !

As expected! Tree in easyui comes with a method onLoadSuccess. When the data is successfully loaded, some operations will be performed
So

$(".next-menu:nth-child(1) a").click(function() {
var $IDstr = $(this).attr("id"),
$treeIDNum = parseInt($(this).attr("treeID")),
jsonURL = "json/" + $IDstr + ".json",
node;
addAttr2Tree(jsonURL);
$("#tt").tree({
onLoadSuccess: function(){
changeImgSrc($treeIDNum);
}
});
});
Copy after login

Just change the code to this .

The above is the analysis of the jQuery Easyui asynchronous loading of tree introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on the problem analysis of jQuery Easyui asynchronous loading of tree, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!