问题描述
想要实现的功能是点击一个treegrid的父节点,获取此父节点下的子节点的数据,并使得此父节点展开。
代码实现
$('#tt').treegrid({
onBeforeExpand: function (row) {
console.log("onBeforeExpand:"+JSON.stringify(row));
var parentRow = $('#tt').treegrid("getParent", row['_childrenId']);
if (parentRow != null){
console.log("tt:"+JSON.stringify(parentRow));
$.ajax({
type: "POST",
url: "../getItem",
async: false,
data:{"parentID":row._childrenId},
dataType:'json',
success:function(data1){
console.log("data:"+JSON.stringify(data1.data));
//当点击父节点时,判断节点是否有children属性,
//如果没有的话,则将通过ajax获取的数据append父节点下面
//如果已经有的话,则什么都不执行
if (row.children){
}else{
$('#tt').treegrid('append',{
parent: row._childrenId, // the node has a 'id' value that defined through 'idField' property
data: data1.data
});
}
return true;
},
});
}
},
})
一开始想的就是通过ajax获取数据,然后查看easyui的文档发现,可以通过append函数将获得子节点数据添加到父节点下面,
但是发现,数据需要在第二次点击父节点时才会显示出来,然后查看代码发现,ajax默认的请求方式时异步的需要按照上面的代码将ajax的请求方式改成同步的之后,即在代码中添加async: false,父节点下面的数据就会在ajax请求成功之后将数据显示出来。