树形数据,构建相关结构

调用
//递归函数无法设置返回值,需要设置一个新的变量记录,下面变量代表行号
const p = { lineno: 0 };
// 编码,层级,顺序,名称重新赋值
//参数1:树形数据,参数2:开始的编码(1,1.1…),
//参数三:层级level,参数四:开始索引值,
//参数五:相邻父级id,参数六:自身id(唯一标识),
//参数七:行号(循环的数据展示的话属于第几行)
resigncodeandparentid(temlist, “”, 0, 0, 0, p);


 ------------------------------------------------------------------------------------
// 定义:编码重新赋值 在插入或删除后
function resigncodeandparentid(
  tree: any[],
  parentcode: string,
  level: number,
  startindex: number,
  parentid: number,
  param: any
): void {
  for (let i = startindex; i < tree.length; i++) {
    const it = tree[i];
    it.fparentid = parentid;
    param.lineno++;
    it.fsecid = param.lineno;
    let pre = "";
    if (parentcode) {
      pre = parentcode + ".";
    }
    it.fcode = pre + (i + 1).toString();
    it.pid = search.value.pid;
    // 顺序重新赋值
    it.flevel = level + 1;
    it.findex = i + 1;
    if (it.fname) {
      it.taskname = it.fname;
    }
    it.badd_ext = true;
    it.fleaf = true;
    delete it.fhelpcode;
    if (it.children && it.children.length > 0) {
      it.fleaf = false;
      resigncodeandparentid(
        it.children,
        it.fcode,
        it.flevel,
        0,
        it.fsecid,
        param
      );
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_44871531/article/details/129200776