mobile easyui兼容实体数据(tree插件为例)
ORM的实体类和数据库的类是一一对应的,如果有多级的嵌套循环json返回到前台为了方便展示可以使用mobile easyui,但是mobile easyui又需要特定的属性才可以,比如id,text,attrubule这样的属性,这时候可以通过前端JS递归去重新赋予jSON数组新属性,例如:
[{ "CC": "", "DeleteFlag": 0, "Id": "1", "Manager": "z", "ParentCode": null, "ParentId": null, "ProCode": "1", "ProID": "1111111", "ProLevel": 1, "ProName": "222222222", "SuspendTime": null, "children": [{ "CC": "", "DeleteFlag": 0, "Id": "33333333", "Manager": "y", "ParentCode": "44444444", "ParentId": "asdsadsad", "ProCode": "asdsad", "ProID": "dfdsfdfdf", "ProLevel": 2, "ProName": "xcvxcvxcv", "SuspendTime": null, "children": [{ "CC": null, "DeleteFlag": 0, "Id": "xcvxcvxcvxcvsewefrwghryhrty", "Manager": "yem", "ParentCode": "cvbcvbcvfbd", "ParentId": "xcvbcvndfgtbdr", "ProCode": "cxvcbdrgher", "ProID": "1541654656", "ProLevel": 3, "ProName": "234234324234", "SuspendTime": null, "children": null }] }] }]
可以转换成:
function GetNode(array) { for (var i = 0; i < array.length; i++) { array[i].id = array[i].Id; array[i].text = array[i].ProName; array[i].attributes = []; //平级属性加入到easyui指定的属性集合 array[i].attributes.push({ "CC": array[i].CC }); array[i].attributes.push({ "DeleteFlag": array[i].DeleteFlag }); array[i].attributes.push({ "Manager": array[i].Manager }); array[i].attributes.push({ "ParentCode": array[i].ParentCode }); array[i].attributes.push({ "ParentId": array[i].ParentId }); array[i].attributes.push({ "ProID": array[i].ProID }); array[i].attributes.push({ "ProLevel": array[i].ProLevel }); array[i].attributes.push({ "ProName": array[i].ProName }); array[i].attributes.push({ "SuspendTime": array[i].SuspendTime }); array[i].attributes.push({ "ProCode": array[i].ProCode }); //删除原有的平级属性 delete array[i].Id; delete array[i].CC; delete array[i].DeleteFlag; delete array[i].Manager; delete array[i].ParentCode; delete array[i].ParentId; delete array[i].ProID; delete array[i].ProLevel; delete array[i].ProName; delete array[i].SuspendTime; delete array[i].ProCode; //继续递归子集合 if (array[i].children&&array[i].children.length > 0) { GetNode(array[i].children, array); } else { continue; } } }
最后使用
$('.tree').tree({
data: 转换后的数组
});
大功告成
积累小的知识,才能成就大的智慧,希望网上少一些复制多一些原创有用的答案