渲染树形文件夹列表

案例 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,

   });

  }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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