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

项目实战-角色列表

抄上一次写过的代码:

import React, { useState, useEffect } from "react";
import axios from 'axios';
import { Button, Table, Modal } from 'antd';
import { BarsOutlined, DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons';const { confirm } = Modal;function RoleList() {const [dataSource, setdataSource] = useState([]);useEffect(() => {axios.get('http://localhost:3000/roles').then(res => {setdataSource(res.data);});}, []);const confirmMethod = (record) => {confirm({title: '确定要删除这个角色吗?',icon: <ExclamationCircleOutlined />,onOk() {deleteMethod(record);},onCancel() {console.log('取消删除');},});};const deleteMethod = (record) => {// 先更新本地状态setdataSource(dataSource.filter(item => item.id !== record.id));// 发送删除请求axios.delete(`http://localhost:3000/roles/${record.id}`);};const columns = [{title: 'ID',dataIndex: 'id',render: (id) => <b>{id}</b>,},{title: '角色名称',dataIndex: 'roleName',},{title: "操作",render: (item) => (<div><Button type="primary" shape="circle" icon={<BarsOutlined />} /><Button danger type="primary" shape="circle" icon={<DeleteOutlined />} onClick={() => confirmMethod(item)}/></div>),}];return (<div><Table dataSource={dataSource} columns={columns} rowKey={(item) => item.id} /></div>);
}export default RoleList;

就可以实现一个和后端数据也可以同步的功能了:

我们需要用到树形控件:

还要实现这个弹出框的效果 

import React, { useState, useEffect } from "react";
import axios from 'axios';
import { Button, Table, Modal } from 'antd';
import { BarsOutlined, DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons';const { confirm } = Modal;function RoleList() {const [dataSource, setdataSource] = useState([]);const [isModalVisible, setIsModalVisible] = useState(false);useEffect(() => {axios.get('http://localhost:3000/roles').then(res => {setdataSource(res.data);});}, []);const confirmMethod = (record) => {confirm({title: '确定要删除这个角色吗?',icon: <ExclamationCircleOutlined />,onOk() {deleteMethod(record);},onCancel() {console.log('取消删除');},});};const deleteMethod = (record) => {setdataSource(dataSource.filter(item => item.id !== record.id));axios.delete(`http://localhost:3000/roles/${record.id}`);};const showModal = () => {setIsModalVisible(true);};const handleOk = () => {console.log("点击了确认");setIsModalVisible(false);};const handleCancel = () => {console.log("点击了取消");setIsModalVisible(false);};const columns = [{title: 'ID',dataIndex: 'id',render: (id) => <b>{id}</b>,},{title: '角色名称',dataIndex: 'roleName',},{title: "操作",render: (item) => (<div><Button type="primary" shape="circle" icon={<BarsOutlined />} onClick={showModal} /><Button danger type="primary" shape="circle" icon={<DeleteOutlined />} onClick={() => confirmMethod(item)}/></div>),}];return (<div><Table dataSource={dataSource} columns={columns} rowKey={(item) => item.id} />{/* Modal 组件 */}<Modal title="权限分配" open={isModalVisible} onOk={handleOk} onCancel={handleCancel}><p>这里可以放权限分配的内容</p><p>比如角色权限列表</p><p>或者其他选项</p></Modal></div>);
}export default RoleList;

能显示modal对话框,下一步实现树形结构

tree的使用就是引入一个tree组件,然后数据满足树形结构

这太棒了

import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { Button, Table, Modal,Tree } from 'antd'
import {BarsOutlined,DeleteOutlined,ExclamationCircleOutlined,
} from '@ant-design/icons'const { confirm } = Modalfunction RoleList() {const [dataSource, setdataSource] = useState([])const [isModalVisible, setIsModalVisible] = useState(false)const [rightList, setRightList] = useState([])useEffect(() => {axios.get('http://localhost:3000/roles').then((res) => {setdataSource(res.data)})}, [])useEffect(() => {axios.get('http://localhost:3000/rights?_embed=children').then((res) => {setRightList(res.data)})}, [])const confirmMethod = (record) => {confirm({title: '确定要删除这个角色吗?',icon: <ExclamationCircleOutlined />,onOk() {deleteMethod(record)},onCancel() {console.log('取消删除')},})}const deleteMethod = (record) => {setdataSource(dataSource.filter((item) => item.id !== record.id))axios.delete(`http://localhost:3000/roles/${record.id}`)}const showModal = () => {setIsModalVisible(true)}const handleOk = () => {console.log('点击了确认')setIsModalVisible(false)}const handleCancel = () => {console.log('点击了取消')setIsModalVisible(false)}const columns = [{title: 'ID',dataIndex: 'id',render: (id) => <b>{id}</b>,},{title: '角色名称',dataIndex: 'roleName',},{title: '操作',render: (item) => (<div><Buttontype="primary"shape="circle"icon={<BarsOutlined />}onClick={showModal}/><Buttondangertype="primary"shape="circle"icon={<DeleteOutlined />}onClick={() => confirmMethod(item)}/></div>),},]return (<div><TabledataSource={dataSource}columns={columns}rowKey={(item) => item.id}/>{/* Modal 组件 */}<Modaltitle="权限分配"open={isModalVisible}onOk={handleOk}onCancel={handleCancel}><Treecheckable treeData={rightList}/></Modal></div>)
}export default RoleList

此后想要实现页面同步和后端的同步:

import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { Button, Table, Modal,Tree } from 'antd'
import {BarsOutlined,DeleteOutlined,ExclamationCircleOutlined,
} from '@ant-design/icons'const { confirm } = Modalfunction RoleList() {const [dataSource, setdataSource] = useState([])const [isModalVisible, setIsModalVisible] = useState(false)const [rightList, setRightList] = useState([])//默认选中const [currentRights, setCurrentRights] = useState([])const [currentId,setCurrentId] = useState(0)useEffect(() => {axios.get('http://localhost:3000/roles').then((res) => {setdataSource(res.data)})}, [])useEffect(() => {axios.get('http://localhost:3000/rights?_embed=children').then((res) => {setRightList(res.data)})}, [])const confirmMethod = (record) => {confirm({title: '确定要删除这个角色吗?',icon: <ExclamationCircleOutlined />,onOk() {deleteMethod(record)},onCancel() {console.log('取消删除')},})}const deleteMethod = (record) => {setdataSource(dataSource.filter((item) => item.id !== record.id))axios.delete(`http://localhost:3000/roles/${record.id}`)}const showModal = () => {setIsModalVisible(true)//dataSource重新映射}const handleOk = () => {console.log('点击了确认')setIsModalVisible(false)setdataSource(dataSource.map(item=>{if(item.id === currentId){return{...item,rights:currentRights}}else{return item}}))//patchaxios.patch(`http://localhost:3000/roles/${currentId}`,{rights:currentRights})}const handleCancel = () => {console.log('点击了取消')setIsModalVisible(false)}// 选中的权限const onCheck = (checkedKeys, checkedInfo) => {setCurrentRights(checkedKeys)}const columns = [{title: 'ID',dataIndex: 'id',render: (id) => <b>{id}</b>,},{title: '角色名称',dataIndex: 'roleName',},{title: '操作',render: (item) => (<div><Buttontype="primary"shape="circle"icon={<BarsOutlined />}onClick={()=>{setIsModalVisible(true)setCurrentRights(item.rights)setCurrentId(item.id)}}/><Buttondangertype="primary"shape="circle"icon={<DeleteOutlined />}onClick={() => confirmMethod(item)}/></div>),},]return (<div><TabledataSource={dataSource}columns={columns}rowKey={(item) => item.id}/>{/* Modal 组件 */}<Modaltitle="权限分配"open={isModalVisible}onOk={handleOk}onCancel={handleCancel}><Treecheckable treeData={rightList}checkedKeys={currentRights}checkStrictly = {true} onCheck={onCheck}/></Modal></div>)
}export default RoleList

相关文章:

项目实战-角色列表

抄上一次写过的代码&#xff1a; import React, { useState, useEffect } from "react"; import axios from axios; import { Button, Table, Modal } from antd; import { BarsOutlined, DeleteOutlined, ExclamationCircleOutlined } from ant-design/icons;const…...

fetch`的语法规则及常见用法

fetch() 是 JavaScript 用于发送 HTTP 请求的内置 API&#xff0c;功能强大&#xff0c;语法简洁。以下是 fetch 的语法规则及常见用法。 1. fetch 基本语法 fetch(url, options).then(response > response.json()) // 解析 JSON 响应体.then(data > console.log(data))…...

如何排查java程序的宕机和oom?如何解决宕机和oom?

排查oom 用jmap生成我们的堆空间的快照Heap Dump&#xff08;堆转储文件&#xff09;&#xff0c;来分析我们的内存占用 用可视化工具&#xff0c;例如java中的jhat分析Heap Dump文件 &#xff0c;它分析完会通过一个浏览器打开一个可视化页面展示分析结果 根据oom的类型来调…...

26_ajax

目录 了解 接口 前后端交互 一、安装服务器环境 nodejs ajax发起请求 渲染响应结果 get方式传递参数 post方式传递参数 封装ajax_上 封装ajax下 了解 清楚前后端交互就可以写一些后端代码了。小项目 现在写项目开发的时候都是前后端分离 之前都没有前端这个东西&a…...

代理模式(Proxy Pattern)实现与对比

代理模式&#xff08;Proxy Pattern&#xff09;实现与对比 1. 虚拟代理&#xff08;Virtual Proxy&#xff09; 定义&#xff1a;延迟加载对象&#xff0c;避免资源浪费。 适用场景&#xff1a;大文件或资源的加载&#xff08;如图片、数据库连接&#xff09;。 代码示例 /…...

MySQL - 数据库基础操作

SQL语句 结构化查询语言(Structured Query Language)&#xff0c;在关系型数据库上执行数据操作、数据检索以及数据维护的标准语言。 分类 DDL 数据定义语言(Data Definition Language)&#xff0c;定义对数据库对象(库、表、列、索引)的操作。 DML 数据操作语言(Data Manip…...

​​​​​​Spring Boot热部署插件

在实际开发中&#xff0c;我们修改某些代码或页面都需要重启应用后才能生效&#xff0c;如果每次都手动重启&#xff0c;会降低了开发效率&#xff1b;热部署是指当我们修改代码后&#xff0c;服务能自动重启加载新修改的内容&#xff0c;这样大大提高了我们开发的效率&#xf…...

pip install cryptacular卡住,卡在downloading阶段

笔者安装pip install cryptacular卡在downloading阶段&#xff0c;但不知道为何 Collecting cryptacularCreated temporary directory: /tmp/pip-unpack-qfbl8f08http://10.170.22.41:8082 "GET http://repo.huaweicloud.com/repository/pypi/packages/42/69/34d478310d6…...

AI大模型从0到1记录学习 day09

第 8 章 面向对象之类和对象 8.1 面向过程和面向对象 面向过程编程&#xff08;Procedural Programming&#xff09;和面向对象编程&#xff08;OOP&#xff09;是两种不同的编程范式&#xff0c;它们在软件开发中都有广泛的应用。 Python是一种混合型的语言&#xff0c;既支持…...

【FW】ADB指令分类速查清单

1. 设备管理 指令核心作用adb devices列出已连接设备adb reboot重启设备adb reboot bootloader进入Bootloader模式adb reboot recovery进入Recovery模式adb root获取Root权限&#xff08;需设备支持&#xff09;adb remount挂载系统分区为可读写 2. 应用管理 指令核心作用adb…...

Kafka中的消息是如何存储的?

大家好&#xff0c;我是锋哥。今天分享关于【Kafka中的消息是如何存储的&#xff1f;】面试题。希望对大家有帮助&#xff1b; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 在 Kafka 中&#xff0c;消息是通过 日志&#xff08;Log&#xff09; 的方式进行存储的。…...

Altium Designer——同时更改多个元素的属性(名称、网络标签、字符串标识)

右键要更改的其中一个对象&#xff0c;选择查找相似… 进入到筛选界面&#xff0c;就是选择你要多选的对象的共同特点&#xff08;名字、大小等等&#xff09;&#xff0c;我这里要更改的是网络标签&#xff0c;所以我选择Text设置为一样。 点击应用就是应用该筛选调节&#…...

当模板方法模式遇上工厂模式:一道优雅的烹饪架构设计

当模板方法模式遇上工厂模式&#xff1a;一道优雅的烹饪架构设计 模式交响曲的实现模板方法模式搭建烹饪骨架&#xff08;抽象类&#xff09;具体菜品&#xff08;子类&#xff09; 工厂模式 模式协作的优势呈现扩展性演示运行时流程控制 完整代码 如果在学习 设计模式的过程中…...

c++位运算总结

在C中&#xff0c;位运算是对二进制位进行操作的运算&#xff0c;主要有以下几种&#xff1a; 1. 按位与&#xff08; & &#xff09;&#xff1a;两个操作数对应位都为1时&#xff0c;结果位才为1&#xff0c;否则为0。例如 3 & 5 &#xff0c; 3 二进制是 0000 0011…...

企业级知识库建设:自建与开源产品集成的全景解析 —— 产品经理、CTO 与 CDO 的深度对话

文章目录 一、引言二、主流产品与方案对比表三、自建方案 vs. 开源产品集成&#xff1a;技术路径对比3.1 自建方案3.2 开源产品集成方案 四、结论与个人观点 一、引言 在当今数据驱动的商业环境中&#xff0c;构建高质量的知识库已成为企业数字化转型的关键一环。本博客分别从…...

Python小练习系列 Vol.6:单词搜索(网格回溯)

&#x1f9e0; Python小练习系列 Vol.6&#xff1a;单词搜索&#xff08;网格回溯&#xff09; &#x1f50d; 本期我们来挑战一道 LeetCode 上经典的网格型回溯题 —— 单词搜索&#xff0c;考察对 DFS 状态恢复的掌握&#xff01; &#x1f9e9; 一、题目描述 给定一个 m x…...

shell脚本--MySQL简单调用

实现功能 增 数据库的创建&#xff0c;数据表的创建已经实现 创建用户 删 删除数据库&#xff0c; 删除库下的某个表&#xff0c; 删除某个用户 改 暂无 查 查看所有的数据库&#xff0c; 查看某个库下的所有数据表&#xff0c; 查看某个表的结构&#xff0c; 查…...

vue3项目配置别名

vue3项目配置别名 src别名的配置TypeScript 编译配置如果出现/别名引入报找不到的问题 src别名的配置 在开发项目的时候文件与文件关系可能很复杂&#xff0c;因此我们需要给src文件夹配置一个别名&#xff01;&#xff01;&#xff01; // vite.config.ts import {defineCon…...

Rust 面向对象

Rust 面向对象 引言 Rust 是一种系统编程语言,以其高性能、内存安全和并发支持而受到关注。Rust 的面向对象特性是其强大功能之一,它允许开发者以面向对象的方式构建复杂的应用程序。本文将深入探讨 Rust 的面向对象编程(OOP)特性,包括类的定义、继承、封装和多态等概念…...

[ C语言 ] | 从0到1?

目录 认识计算机语言 C语言 工欲善其事必先利其器 第一个C语言代码 这一些列 [ C语言 ] &#xff0c;就来分享一下 C语言 相关的知识点~ 认识计算机语言 我们说到计算机语言&#xff0c;语言&#xff0c;就是用来沟通的工具&#xff0c;计算机语言呢&#xff1f;就是我们…...

[Mac]利用Hexo+Github Pages搭建个人博客

由于我这台Mac基本没啥环境&#xff0c;因此需要从零开始配置&#xff0c;供各位参考。 注意⚠️&#xff1a;MacBook (M4)使用/bin/zsh作为默认Shell&#xff0c;其对应的配置文件为~/.zshrc 参考文档&#xff1a; HEXO系列教程 | 使用GitHub部署静态博客HEXO | 小白向教程 文…...

pycharm与python版本

python 3.6-3.9 pycharm 2021版本搭配最好 python 3.8 pycharm 2019版本搭配最好 pycharm各版本下载...

Qt在IMX6ULL嵌入式系统中图片加载问题排查与解决

Qt在IMX6ULL嵌入式系统中图片加载问题排查与解决&#xff08;保姆级教学&#xff01;&#xff09; 在使用Qt开发IMX6ULL嵌入式系统的过程中&#xff0c;我遇到了图片加载的常见问题。本文将分享问题排查的详细过程和解决方案&#xff0c;希望能帮助遇到类似困难的开发者。 问题…...

界面控件Telerik和Kendo UI 2025 Q1亮点——AI集成与数据可视化

Telerik DevCraft包含一个完整的产品栈来构建您下一个Web、移动和桌面应用程序。它使用HTML和每个.NET平台的UI库&#xff0c;加快开发速度。Telerik DevCraft提供完整的工具箱&#xff0c;用于构建现代和面向未来的业务应用程序&#xff0c;目前提供UI for ASP.NET MVC、Kendo…...

pycharm终端操作远程服务器

pycharm项目已经连接了远程服务器&#xff0c;但是打开终端&#xff0c;却依旧显示的是本地的那个环境&#xff0c;也就是说没有操作远程的那个环境。只能再使用Xshell去操作远程环境&#xff0c;很麻烦&#xff0c;找了下教程。 来源&#xff1a;https://blog.csdn.net/maolim…...

接口测试中数据库验证,怎么解决?

在接口测试中&#xff0c;通常需要在接口调用前后查询数据库&#xff0c;以验证接口操作是否正确影响了数据库状态。​这可以通过数据库断言来实现&#xff0c;PyMySQL库常用于连接和操作MySQL数据库。​通过该库&#xff0c;可以在测试中执行SQL语句&#xff0c;查询或修改数据…...

Playwright从入门到实战:比Selenium更快的数据爬取案例实战

摘要 Playwright 是微软开源的下一代浏览器自动化工具&#xff0c;凭借其高性能、跨浏览器支持和现代化设计&#xff0c;迅速成为 Web 自动化领域的热门选择。本文将从 安装配置 开始&#xff0c;通过 实战演练 展示其核心功能&#xff0c;并与 Selenium 深度对比&#xff0c;…...

defconfig配置宏的规则

defconfig配置宏的规则 CONFIG_INETnCONFIG_INETy defconfig里这样配置&#xff0c;CONFIG_INET宏有效吗 在 defconfig 文件中&#xff0c;如果出现了 相同的配置项被定义多次&#xff0c;最终生效的是最后一次出现的值。 &#x1f539; 你的配置 bash复制编辑CONFIG_INE…...

day1_Flink基础

文章目录 Flink基础今日课程内容目标为什么要学Flink技术更新迭代市场需求 流式计算批量计算概念特点 批量计算的优势和弊端流式计算生活中流场景流式计算的概念 Flink简介Flink历史Flink介绍 Flink架构体系已学过的框架技术Flink架构 Flink集群搭建Flink的集群模式Standalone模…...

ctf-web: 不统一的解析 + sql注入要求输入与输出相等 -- tpctf supersqli

# 从 django.shortcuts 模块导入 render 函数&#xff0c;用于渲染模板 from django.shortcuts import render # 从 django.db 模块导入 connection 对象&#xff0c;用于数据库连接 from django.db import connection# 此模块用于创建视图函数 # 从 django.http 模块导入 Http…...