当前位置: 首页 > news >正文

黄石有哪些做视觉网站的公司网站之家

黄石有哪些做视觉网站的公司,网站之家,建设厅电工证查询网站,无锡网站制作哪家正规在本篇博客中,我们将使用 React 和 MUI(Material-UI)库来创建一个多选 Checkbox 树组件。该组件可以用于展示树形结构的数据,并允许用户选择多个节点。 前提 在开始之前,确保你已经安装了以下依赖: Reac…

在本篇博客中,我们将使用 React 和 MUI(Material-UI)库来创建一个多选 Checkbox 树组件。该组件可以用于展示树形结构的数据,并允许用户选择多个节点。

前提

在开始之前,确保你已经安装了以下依赖:

  • React
  • MUI(Material-UI)

最终样式

非全选状态

在这里插入图片描述

全选状态

在这里插入图片描述

思路

我们的目标是创建一个多选 Checkbox 树组件,它可以接收树节点数据,并根据用户的选择返回选中的节点数据。为了实现这个目标,我们将按照以下步骤进行:

  1. 创建一个 React 函数组件 CheckBoxTree,它接收一个 data 属性作为树节点数据,并可选地接收一个 handleCheckData 属性作为回调函数,用于传递选中的节点数据。
  2. 在组件的状态中,创建一个 selected 数组,用于存储选中的节点的 id。
  3. 实现一个 onCheck 函数,用于处理节点 Checkbox 的点击事件。在该函数中,我们将根据用户的选择更新 selected 数组,并递归处理子节点的选中状态。
  4. 实现一个 renderTree 函数,用于递归渲染树节点。在该函数中,我们将根据节点的选中状态和子节点的数量来渲染 Checkbox 和节点名称。
  5. 使用 TreeViewTreeItem 组件来展示树形结构,并将树节点数据传递给 renderTree 函数进行渲染。

步骤

下面是实现多选 Checkbox 树组件的详细步骤:

1. 创建 React 函数组件

首先,我们需要创建一个 React 函数组件 CheckBoxTree,并定义它的属性和状态。代码如下:

import React from 'react';interface CheckboxTreeState {selected: string[];
}interface CheckBoxTreeProps {data: RegionType[]; //起码要包含childre,name和parentId,handleCheckData?: (data: string[]) => void;
}export default function CheckBoxTree(props: CheckBoxTreeProps) {const { data, handleCheckData } = props;const [state, setState] = React.useState<CheckboxTreeState>({selected: []});// ...
}

2. 分割父节点

接下来,我们定义了splitNodeId函数,用于将节点id拆分为所有父节点id。它接受一个节点id字符串,格式为'1_2_3',并返回一个父节点id数组,例如['1_2', '1']。3表示的是当前节点。

/*** 拆分节点id为所有父节点id* @param id 节点id,格式为'1_2_3'* @returns 父节点id数组,如['1_2', '1']*/
function splitNodeId(id: string) {// 按'_'分割节点idconst path = id.split('_');// 累加生成父节点idreturn path.reduce((result: string[], current) => {// 拼接'_'和当前节点result.push(`${result.at(-1) ? result.at(-1) + '_' : ''}${current}`);return result;}, []);
}

3. 实现节点 Checkbox 的点击事件处理函数

接下来,我们需要实现一个 onCheck 函数,用于处理节点 Checkbox 的点击事件。在该函数中,我们将根据用户的选择更新 selected 数组,并递归处理子节点的选中状态。代码如下:

const onCheck = (event: React.ChangeEvent<HTMLInputElement>,node: RegionType,parentNodeName?: string
) => {const { checked } = event.target;const currentId = parentNodeName ?`${parentNodeName}_${node.id.id}` :node.id.id;const parentAreaName = splitNodeId(currentId);if (checked) {setState((prevState) => ({...prevState,selected: Array.from(new Set([...prevState.selected, ...parentAreaName]))}));if (node.children && node.children.length > 0) {node.children.forEach((item) => {onCheck(event, item, currentId);});}} else if (!checked) {let tempState = { ...state };for (let index = parentAreaName.length - 1; index >= 0; index--) {const element = parentAreaName[index];if (tempState.selected.filter((id) => id.startsWith(`${element}_`)).length === 0) {tempState = {...tempState,selected: tempState.selected.filter((id) => id !== element)};}if (tempState.selected.filter((id) => id.startsWith(`${currentId}_`)).length !== 0) {tempState = {...tempState,selected: tempState.selected.filter((id) =>!id.startsWith(`${currentId}_`) &&!id.startsWith(`${currentId}`))};}}setState(tempState);}
};

4. 实现递归渲染树节点的函数

然后,我们需要实现一个 renderTree 函数,用于递归渲染树节点。在该函数中,我们将根据节点的选中状态和子节点的数量来渲染 Checkbox 和节点名称。代码如下:

const renderTree = (nodes: RegionType, parentNodeName?: string) => {let currentLength = 0;function getNodeLength(currentNodes: RegionType) {currentNodes.children?.forEach((node) => {currentLength++;if (node.children) {getNodeLength(node);}});}const currentId = parentNodeName ?`${parentNodeName}_${nodes.id.id}` :nodes.id.id;getNodeLength(nodes);return (<TreeItemkey={nodes.id.id}nodeId={nodes.id.id}label={<FormControlLabelonClick={(e) => e.stopPropagation()}control={<Checkboxname={nodes.name}checked={nodes.children &&nodes.children.length &&state.selected.filter((id) =>id.startsWith(`${currentId}_`)).length === currentLength ||state.selected.some((id) => id === currentId)}indeterminate={nodes.children &&nodes.children.length > 0 &&state.selected.some((id) => id.startsWith(`${currentId}_`)) &&state.selected.filter((id) => id.startsWith(`${currentId}_`)).length < currentLength}onChange={(e) => {e.stopPropagation();onCheck(e, nodes, parentNodeName);}}onClick={(e) => e.stopPropagation()}/>}label={nodes.name}/>}>{Array.isArray(nodes.children) ?nodes.children.map((node) => renderTree(node, currentId)) :null}</TreeItem>);
};

5. 渲染树形结构

最后,我们使用 TreeViewTreeItem 组件来展示树形结构,并将树节点数据传递给 renderTree 函数进行渲染。代码如下:

return (<TreeViewaria-label="checkbox tree"defaultCollapseIcon={<ExpandMore />}defaultExpandIcon={<ChevronRight />}disableSelection={true}>{data.map((item) => {return renderTree(item);})}</TreeView>
);

6. 完整代码

import { ChevronRight, ExpandMore } from '@mui/icons-material';
import { TreeItem, TreeView } from '@mui/lab';
import { Checkbox, FormControlLabel } from '@mui/material';
import React from 'react';export interface RegionType {abbreviation: string;children?: RegionType[];createdTime: number;id: EntityData;level: number;name: string;nameCn: string;parentId: string;sort: number;status: boolean;
}// 组件状态
int
erface CheckboxTreeState {// 选中节点id数组selected: string[];
}// 组件属性
interface CheckBoxTreeProps {// 树节点数据data: RegionType[];// 向外传递选择框数据,handleCheckData?: (data: string[]) => void;
}/*** 拆分节点id为所有父节点id* @param id 节点id,格式为'1_2_3'* @returns 父节点id数组,如['1_2', '1']*/
function splitNodeId(id: string) {// 按'_'分割节点idconst path = id.split('_');// 累加生成父节点idreturn path.reduce((result: string[], current) => {// 拼接'_'和当前节点result.push(`${result.at(-1) ? result.at(-1) + '_' : ''}${current}`);return result;}, []);
}/*** 多选Checkbox树组件* @param props 组件属性* @returns JSX组件*/
export default function CheckBoxTree(props: CheckBoxTreeProps) {// 获取树节点数据const { data, handleCheckData } = props;// 组件状态:选中节点id数组const [state, setState] = React.useState<CheckboxTreeState>({selected: []});/*** 点击节点Checkbox触发* @param event 事件对象* @param node 节点对象* @param parentNodeName 父节点名称*/const onCheck = (event: React.ChangeEvent<HTMLInputElement>,node: RegionType,parentNodeName?: string) => {// 获取Checkbox选中状态const { checked } = event.target;// 当前节点idconst currentId = parentNodeName ?`${parentNodeName}_${node.id.id}` :node.id.id;// 父节点id数组const parentAreaName = splitNodeId(currentId);// 选中状态:选中当前节点和父节点if (checked) {setState((prevState) => ({...prevState,//使用Set对selected数组去重selected: Array.from(new Set([...prevState.selected, ...parentAreaName]))}));// 若有子节点,递归选中if (node.children && node.children.length > 0) {node.children.forEach((item) => {onCheck(event, item, currentId);});}} else if (!checked) {// 临时statelet tempState = { ...state };// 逆序遍历,进行选中状态更新for (let index = parentAreaName.length - 1; index >= 0; index--) {const element = parentAreaName[index];// 若父区域已无选中节点,取消选中父区域if (tempState.selected.filter((id) => id.startsWith(`${element}_`)).length === 0) {tempState = {...tempState,selected: tempState.selected.filter((id) => id !== element)};}// 取消选中当前区域if (tempState.selected.filter((id) => id.startsWith(`${currentId}_`)).length !== 0) {tempState = {...tempState,selected: tempState.selected.filter((id) =>!id.startsWith(`${currentId}_`) &&!id.startsWith(`${currentId}`))};}}// 更新statesetState(tempState);}};/*** 递归渲染树节点* @param nodes 树节点数组* @param parentNodeName 父节点名称* @returns JSX组件*/const renderTree = (nodes: RegionType, parentNodeName?: string) => {// 子节点总数let currentLength = 0;/*** 获取子节点总数* @param currentNodes 当前节点*/function getNodeLength(currentNodes: RegionType) {currentNodes.children?.forEach((node) => {currentLength++;if (node.children) {getNodeLength(node);}});}// 当前节点idconst currentId = parentNodeName ?`${parentNodeName}_${nodes.id.id}` :nodes.id.id;// 获取当前节点子节点总数getNodeLength(nodes);return (<TreeItemkey={nodes.id.id}nodeId={nodes.id.id}sx={{'.MuiTreeItem-label': {'maxWidth': '100%','overflow': 'hidden','wordBreak': 'break-all','.MuiFormControlLabel-label': {pt: '2px'}}}}label={<FormControlLabelonClick={(e) => e.stopPropagation()}sx={{ alignItems: 'flex-start', mt: 1 }}control={<Checkboxname={nodes.name}sx={{ pt: 0 }}checked={// 若有子节点,判断子节点是否全部选中// 或节点自身是否选中nodes.children &&nodes.children.length &&state.selected.filter((id) =>id.startsWith(`${currentId}_`)).length === currentLength ||state.selected.some((id) => id === currentId)}indeterminate={// 子节点存在选中与非选中状态nodes.children &&nodes.children.length > 0 &&state.selected.some((id) => id.startsWith(`${currentId}_`)) &&state.selected.filter((id) => id.startsWith(`${currentId}_`)).length < currentLength}onChange={(e) => {e.stopPropagation();onCheck(e, nodes, parentNodeName);}}onClick={(e) => e.stopPropagation()}/>}label={nodes.name}/>}>{Array.isArray(nodes.children) ?nodes.children.map((node) => renderTree(node, currentId)) :null}</TreeItem>);};/*** 组件加载时触发,获取去重后的多选框id列表*/React.useEffect(() => {// state.selected拆分数组并合并,返回成一个数组,如果需要去重后的值,可以使用Array.from(new set)const checkBoxList = state.selected.flatMap((item) => item.split('_'));// 因为是通过parent id来绑定子元素,所以下面的元素是只返回最后的子元素const checkTransferList = checkBoxList.filter((value) => checkBoxList.indexOf(value) === checkBoxList.lastIndexOf(value));// 从多选值数组中生成集合Set,再使用Array.from转换为数组if (handleCheckData) {handleCheckData(checkTransferList);}}, [state]);React.useEffect(() => {if (data.length) {setState({ selected: [] });}}, [data]);return (<TreeViewaria-label="checkbox tree"defaultCollapseIcon={<ExpandMore />}defaultExpandIcon={<ChevronRight />}disableSelection={true}>{data.map((item) => {return renderTree(item);})}</TreeView>);
}

总结

通过以上步骤,我们成功地创建了一个多选 Checkbox 树组件。该组件可以接收树节点数据,并根据用户的选择返回选中的节点数据。我们使用了 React 和 MUI(Material-UI)库来实现这个功能,并按照前提、思路和步骤的顺序进行了解析和实现。

希望本篇博客对你理解如何使用 React 和 MUI 创建多选 Checkbox 树组件有所帮助!如果你有任何问题或建议,请随时留言。谢谢阅读!


文章转载自:
http://panoplied.xnLj.cn
http://jissom.xnLj.cn
http://pliable.xnLj.cn
http://bernadine.xnLj.cn
http://signans.xnLj.cn
http://astronome.xnLj.cn
http://segregative.xnLj.cn
http://gonk.xnLj.cn
http://stamper.xnLj.cn
http://quivery.xnLj.cn
http://midtown.xnLj.cn
http://nazar.xnLj.cn
http://mutagen.xnLj.cn
http://unsteadily.xnLj.cn
http://ingenue.xnLj.cn
http://refractably.xnLj.cn
http://abundant.xnLj.cn
http://daffodil.xnLj.cn
http://heated.xnLj.cn
http://solutrean.xnLj.cn
http://paleocene.xnLj.cn
http://niche.xnLj.cn
http://framer.xnLj.cn
http://astroid.xnLj.cn
http://loimic.xnLj.cn
http://organum.xnLj.cn
http://mango.xnLj.cn
http://cockneyese.xnLj.cn
http://storey.xnLj.cn
http://alterant.xnLj.cn
http://loof.xnLj.cn
http://gwadar.xnLj.cn
http://revivify.xnLj.cn
http://amphiprostyle.xnLj.cn
http://dolich.xnLj.cn
http://cataplexy.xnLj.cn
http://groenendael.xnLj.cn
http://punctuality.xnLj.cn
http://conniption.xnLj.cn
http://obstetrician.xnLj.cn
http://decomposite.xnLj.cn
http://brigantine.xnLj.cn
http://whidah.xnLj.cn
http://posttensioning.xnLj.cn
http://ondograph.xnLj.cn
http://photonasty.xnLj.cn
http://archdeaconry.xnLj.cn
http://carboniferous.xnLj.cn
http://mullah.xnLj.cn
http://cytomegalovirus.xnLj.cn
http://smg.xnLj.cn
http://creamware.xnLj.cn
http://judicative.xnLj.cn
http://anchorage.xnLj.cn
http://russify.xnLj.cn
http://planking.xnLj.cn
http://transmogrify.xnLj.cn
http://clomp.xnLj.cn
http://fourragere.xnLj.cn
http://deluster.xnLj.cn
http://chemotherapeutant.xnLj.cn
http://gunsight.xnLj.cn
http://cowled.xnLj.cn
http://puddingy.xnLj.cn
http://underthrust.xnLj.cn
http://afterbrain.xnLj.cn
http://hexobarbital.xnLj.cn
http://trilobite.xnLj.cn
http://embellishment.xnLj.cn
http://garibaldian.xnLj.cn
http://encasement.xnLj.cn
http://rollei.xnLj.cn
http://poplar.xnLj.cn
http://ware.xnLj.cn
http://dub.xnLj.cn
http://splitter.xnLj.cn
http://chylify.xnLj.cn
http://gallophobe.xnLj.cn
http://plotting.xnLj.cn
http://intercept.xnLj.cn
http://chiliarch.xnLj.cn
http://maraca.xnLj.cn
http://midiron.xnLj.cn
http://smartdrive.xnLj.cn
http://habitus.xnLj.cn
http://righty.xnLj.cn
http://corinna.xnLj.cn
http://chairbed.xnLj.cn
http://resourceless.xnLj.cn
http://czarevitch.xnLj.cn
http://engrain.xnLj.cn
http://politically.xnLj.cn
http://presumably.xnLj.cn
http://pam.xnLj.cn
http://revers.xnLj.cn
http://kinetosome.xnLj.cn
http://abasable.xnLj.cn
http://virology.xnLj.cn
http://polyamine.xnLj.cn
http://kiddle.xnLj.cn
http://www.15wanjia.com/news/93909.html

相关文章:

  • 家用电脑怎么做网站服务器数据分析
  • 构建一个网站需要什么5151app是交友软件么
  • 搬瓦工可以长期做网站品牌营销策划方案怎么做
  • 连云港网站建设电话百度权重网站排名
  • 一键查询个人房产信息seo在线短视频发布页运营
  • 做网站有哪些技术营销型网站建设多少钱
  • 企业建设网站需要什么资料教育培训机构加盟十大排名
  • 如何做京东购物网站百度关键词seo排名优化
  • 潍坊网站建设公司电话福州网站seo优化公司
  • 合肥网站设计机构如何设置友情链接
  • 网站建设日程表模板深圳百度seo代理
  • 武汉网站设计制作百度大全
  • 门户网站建设需要注意什么网络营销专业大学排名
  • 郑州高端网站建设怎么样流量精灵官网
  • xuzhou公司网站制作青岛专业网站制作
  • 网站建设产品需求文档免费b站推广网站入口
  • 网站竞价推广都有哪些培训学校机构
  • 自做头像的网站韩国电视剧
  • django 做网站赚钱推广引流方法有哪些?
  • 网站建设 铭阳传媒深圳网络推广市场
  • 网站主机名百度推广开户渠道
  • wordpress主题黑糖优化工具箱
  • 创新的福州网站建设百度网盘手机版
  • 一个网站多个域名的seo优化百度官网首页入口
  • 服装电子商务网站建设与实现郑州seo实战培训
  • 网站广告条素材百度seo关键词优化市场
  • 海外购物网站上填手机号码怎么做免费的个人网页
  • 求一个全部用div做的网站网推项目
  • 淫秽色情网站境外的windows优化大师手机版
  • 建设网站宽度最好是多少360优化关键词