渲染树形文件夹列表

案例 hdsp-数据质量-标准规则

一、通用方法

获取当前节点的父节点

list 数据结构转换为 treeList结构

/**

* 获取 Tree 中的 parentKey

* @param {String} key key

* @param {Array} tree tree

* @param {String} primaryKey primaryKey

*/

functiongetParentKeyOfTree(key,tree,primaryKey) {

letparentKey;

for(leti=0;i<tree.length;i++) {

constnode=tree[i];

if(node.children) {

if(node.children.some(item=>item[primaryKey]===key)) {

parentKey=node[primaryKey];

}elseif(getParentKeyOfTree(key,node.children,primaryKey)) {

parentKey=getParentKeyOfTree(key,node.children,primaryKey);

     }

   }

  }

returnparentKey;

}

/**

* List结构转换成TreeList结构

* @param {Array} dataList dataList

*/

functionlistToTreeList(dataList) {

constcloneDataList=cloneDeep(dataList);

constmap={};

cloneDataList.forEach((item)=>{

map[item.groupId===undefined?-1:item.groupId]=item;

  });

constval=[];

cloneDataList.forEach((item)=>{

constparent=map[item.parentGroupId];

if(parent) {

(parent.children||(parent.children=[])).push(item);

}else{

val.push(item);

   }

  });

returnval;

}

二、DataSet配置

primaryKey:'groupId',

autoQuery:true,

paging:false,

selection:'single',

parentField:'parentGroupId',

idField:'groupId',

三、渲染树节点

渲染树

循环渲染树节点

/**

  * 循环渲染Tree节点

  * @param {Object} data data

  */

@Bind()

loopTreeList(data) {

const{selectedGroupId}=this.props;

const{searchValue}=this.state;

returndata.map((item)=>{

const{groupId,groupName='',children}=item;

constindex=groupName.indexOf(searchValue);

constbeforeStr=groupName.substr(0,index);

constafterStr=groupName.substr(index+searchValue.length);

// 根节点

constrootFlag=groupId===0;

constmenu=(

<MenuclassName={styles['rule-group-menu']}onClick={({key})=>this.handleRuleGroupMenuClick(key,item)}>

<Menu.ItemonClick={this.handleMenuItemStopPropagation}key={RULE_GROUP_ACTION.EDIT}disabled={rootFlag}>

{HZERO_COMMON_LANG.BUTTON_EDIT}

</Menu.Item>

</Menu>

     );

constmenuDropdown=selectedGroupId===groupId&&(

<Dropdownoverlay={menu}>

<IcononClick={this.handleIconStopPropagation}className={styles['rule-group-icon']}type="more_vert"/>

</Dropdown>

     );

consttitle=index>-1?(

<>

{beforeStr}

<spanstyle={{color:'#f50'}}>{searchValue}</span>

{afterStr}

{menuDropdown}

</>

     ) : (

<>

<span>{groupName}</span>

{menuDropdown}

</>

     );

leticon;

if(rootFlag) {

icon=ICON_TYPE.ROOT_FOLDER;

}else{

icon=ICON_TYPE.FOLDER;

     }

if(children) {

return(

<TreeNodekey={groupId}icon={<Icontype={icon}/>}title={title}>

{this.loopTreeList(children)}

</TreeNode>

       );

     }

return<TreeNodeicon={<Icontype={icon}/>}key={groupId}title={title}/>;

   });

  }



render() {

const{ruleGroupDS,selectedGroupId}=this.props;

const{expandedKeys,autoExpandParent}=this.state;

constdataList=ruleGroupDS.toData();

consttreeDataList=listToTreeList(dataList);

return(

<divstyle={{padding:10}}>

<Rowgutter={4}style={{marginBottom:8}}>

<Colspan={18}>

<Search

placeholder={HZERO_COMMON_LANG.BUTTON_SEARCH}

onSearch={(e)=>this.handleSearchChange(e,treeDataList)}

/>

</Col>

</Row>

<Tree

className={styles['rule-group-tree']}

showIcon

onSelect={this.handleTreeSelect}

onExpand={this.handleTreeExpand}

expandedKeys={expandedKeys.map(x=>String(x))}

selectedKeys={[String(selectedGroupId)]}

autoExpandParent={autoExpandParent}

>

{this.loopTreeList(treeDataList)}

</Tree>

</div>

   );

  }

###

四、查询树节点

/**

  * 搜索字符

  * @param {Object} value value

  * @param {Array} treeDataList treeDataList

  */

@Bind()

handleSearchChange(value,treeDataList) {

const{ruleGroupDS}=this.props;

constdataList=ruleGroupDS.toData();

constexpandedKeys=dataList.map(item=>{

const{groupName=''}=item;

if(groupName.indexOf(value)>-1) {

returngetParentKeyOfTree(item.groupId,treeDataList,'groupId');

     }

returnnull;

}).filter((item,i,self)=>item&&self.indexOf(item)===i);

this.setState({

expandedKeys:expandedKeys.concat('0'),

searchValue:value,

autoExpandParent:true,

   });

  }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容

  • 实战 7:树形控件——Tree 本小节基于 Vue.js 的递归组件知识,来开发一个常见的树形控件—Tree。 T...
    中午吃啥_f330阅读 1,178评论 0 1
  • 1.Typescript中的&理解 参数中的 & 表示props对象同时拥有了TagManagementState...
    Lethe35阅读 7,611评论 0 1
  • 1 页面发布 1.1 技术方案 技术方案说明: 1、平台包括多个站点,页面归属不同的站点。 2、发布一个页面应将该...
    lww文阅读 440评论 0 0
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,720评论 0 5
  • 昨天,在回家的路上,坐在车里悠哉悠哉地看着三毛的《撒哈拉沙漠的故事》,我被里面的内容深深吸引住了,尽管上学时...
    夜阑晓语阅读 3,785评论 2 9