UNPKG

3.23 kBPlain TextView Raw
1import { TreeNode } from 'primeng/primeng';
2import { RefBean, Selectable, RefBeanTreeNode } from './models';
3
4export class PrimengUtils {
5 /** 把PrimeNg TreeNode数组转换成RefBean数组
6 * @param treeNodes PrimeNg TreeNode数组,有data,label两个属性
7 * @return RefBean数组, 有code 和 name两个属性
8 */
9 public static treeNodesToRefBeans(treeNodes: TreeNode[]): RefBean[] {
10 let refBeans: RefBean[] = new Array<RefBean>();
11 for (let tnode of treeNodes) {
12 refBeans.push(new RefBean(tnode.data, tnode.label));
13 }
14 return refBeans;
15 }
16 /** 根据已选的数据,把相应的PrimeNg TreeNode节点打上已选标记
17 * @param treeNodes PrimeNg TreeNode数组,有data,label两个属性
18 * @return RefBean数组, 有code 和 name两个属性
19 */
20 public static getSelectedTreeNodeByDatas(tree: TreeNode[], datas: string[]): TreeNode[] {
21 let seletedTreeNodes: TreeNode[] = new Array<TreeNode>();
22 PrimengUtils.getSelectedTreeNodeByDatas1(tree, datas, seletedTreeNodes);
23 return seletedTreeNodes;
24 }
25 private static getSelectedTreeNodeByDatas1(treeNodes: TreeNode[], datas: string[], seletedTreeNodes: TreeNode[]) {
26 for (let tnode of treeNodes) {
27 for (let data of datas) {
28 if (data == tnode.data) {
29 seletedTreeNodes.push(tnode);
30 break;
31 }
32 }
33 if (tnode.children) {
34 PrimengUtils.getSelectedTreeNodeByDatas1(tnode.children, datas, seletedTreeNodes);
35 }
36 }
37
38 }
39 /** 从tree中找到与refBeans数组中ID相同的节点集。
40 * @param tree 目标树,Primeng TreeNode树
41 * @param refBeans 对应的refBeans,有code,name两个属性
42 * @return PrimeNg TreeNode树中,和refBeans中有相同ID的节点集
43 */
44 public static getSelectedTreeNodes(tree: TreeNode[], refBeans: RefBean[]): TreeNode[] {
45 let seletedTreeNodes: TreeNode[] = new Array<TreeNode>();
46 PrimengUtils.getSelectedNodes(tree, refBeans, seletedTreeNodes);
47 return seletedTreeNodes;
48 }
49 private static getSelectedNodes(treeNodes: TreeNode[], refBeans: RefBean[], seletedTreeNodes: TreeNode[]) {
50 for (let tnode of treeNodes) {
51 for (let rbean of refBeans) {
52 if (rbean.code == tnode.data) {
53 seletedTreeNodes.push(tnode);
54 break;
55 }
56 }
57 if (tnode.children) {
58 PrimengUtils.getSelectedNodes(tnode.children, refBeans, seletedTreeNodes);
59 }
60 }
61
62 }
63 /** 把从服务器来的RefBeanTreeNode树变成PrimeNg TreeNode树,实际只是简单的把RefBean的code和name属性复制到data,label属性。
64 * 这样简单处理后,需要返回到服务器端时,可以把PrimeNg TreeNode树直接当作RefBeanTreeNode树返回,无需变换。
65 * @param rNodes 从服务器来的RefBean树,有code,name两个属性
66 * @return PrimeNg TreeNode树, 有data 和 label两个属性
67 */
68 static refBeanTreeNodesToTreeNodes(rNodes: RefBeanTreeNode[]): TreeNode[] {
69 //let tnodes = new Array<TreeNode>();
70 for (let rnode of rNodes) {
71 let tnode: TreeNode = rnode;
72 tnode.data = rnode.code;
73 tnode.label = rnode.name;
74 //tnodes.push(tnode);
75 if (rnode.children) {
76 tnode.children = PrimengUtils.refBeanTreeNodesToTreeNodes(rnode.children);
77 }
78 }
79 return rNodes;
80 }
81}
\No newline at end of file