如何使用react做一个简易的后台管理系统
程序员文章站
2023-10-15 15:11:00
首先我们要创建一个项目 代码是npx create-react-app之后我们开始写这个项目在写这个项目之前我们因为样式美观度的原因所以使用ant design 安装方法请去ant design官网快速上手当中复制粘贴安装命令之后我们还需要安装echarts,echarts同时也是增加页面美观度的一 ......
首先我们要创建一个项目 代码是npx create-react-app之后我们开始写这个项目在写这个项目之前我们因为样式美观度的原因所以使用ant design 安装方法请去ant design官网快速上手当中复制粘贴安装命令之后我们还需要安装echarts,echarts同时也是增加页面美观度的一种操作详情同上 那么安装这些东西以后我们则正式开始做项目我们要做的时候我们先看我们要实现的效果先创建页面
在主页引用ant desgin同时配置路由
import react, {component} from 'react'; import '../app.css'; import {route,switch,redirect} from 'react-router-dom' import {layout, menu, icon} from 'antd'; import homecomponent from "./home"; import tablecomponent from "./table"; const {header, sider, content} = layout; const submenu = menu.submenu; class app extends component { state = { collapsed: false, }; toggle = () => { this.setstate({ collapsed: !this.state.collapsed, }); } gopage(path){ this.props.history.replace(path) } render() { return ( <layout> <sider trigger={null} collapsible collapsed={this.state.collapsed} style={{height:"100vh"}} > <div classname="logo" > <img src={'http://gaohui628.top:8084/dist/184d0d26d7b4d5e11f4045622edd2b54.jpg'} alt=""/> </div> <menu theme="dark" defaultselectedkeys={['1']} mode="inline"> <submenu key="sub1" title={<span><icon type="user" /><span>学生管理</span></span>} > <menu.item key="1" onclick={this.gopage.bind(this,'/table')}>实训学生</menu.item> <menu.item key="2" onclick={this.gopage.bind(this,'/home')}>专高学生</menu.item> <menu.item key="3">专业学生</menu.item> </submenu> <submenu key="sub2" title={<span><icon type="user" /><span>面试题管理</span></span>} > <menu.item key="4">实训学生</menu.item> <menu.item key="5">专高学生</menu.item> <menu.item key="6">专业学生</menu.item> </submenu> <submenu key="sub3" title={<span><icon type="user" /><span>项目库</span></span>} > <menu.item key="7">实训学生</menu.item> <menu.item key="8">专高学生</menu.item> <menu.item key="9">专业学生</menu.item> </submenu> <submenu key="sub4" title={<span><icon type="user" /><span>snippet快捷键</span></span>} > <menu.item key="10">实训学生</menu.item> <menu.item key="11">专高学生</menu.item> <menu.item key="12">专业学生</menu.item> </submenu> </menu> </sider> <layout> <header style={{background: '#fff', padding: 0}}> <icon classname="trigger" type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'} onclick={this.toggle} style={{fontsize:"2rem",marginleft:"0.5rem"}} /> <span style={{fontsize:"2rem",marginleft:"0.5rem"}}>首页</span> <span classname={'head-span'}></span> </header> <content style={{margin: '24px 16px', padding: 24, background: '#fff', minheight: 280}}> <switch> <route path={"/home"} component={homecomponent}></route> <route path={'/table'} component={tablecomponent}></route> <redirect to={'/home'}/> </switch> </content> </layout> </layout> ); } } export default app;
为了配置我们的二级路由我们还需要一个配置路由的文件夹
以下是路由代码对于路由有问题的朋友们可以找react路由配置相关文章
import react,{component} from 'react' import {hashrouter as router,route,switch} from 'react-router-dom' import app from "../pages/app"; class routercomponent extends component{ render() { return ( <react.fragment> <router> <react.fragment> <switch> <route path='/' component={app}></route> </switch> </react.fragment> </router> </react.fragment> ); } } export default routercomponent
之后我们还需要配置我们的index.js页面的代码它是为了更好的配置路由使路由不报错
import react from 'react'; import reactdom from 'react-dom'; import './index.css'; import registerserviceworker from './registerserviceworker'; import routercomponent from "./router/router"; reactdom.render(<routercomponent />, document.getelementbyid('root')); registerserviceworker();
代码如上图之后就是配置我们的组件首先是我们的首页组件其中这里会用到我们的echarts
import react,{component} from 'react' import '../asstes/css/home.css' import echarts from 'echarts' class homecomponent extends component{ componentdidmount(){ var mychart = echarts.init(document.getelementbyid('main')); // app.title = '坐标轴刻度与标签对齐'; var option = { color: ['#3398db'], tooltip : { trigger: 'axis', axispointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3% containlabel: true }, xaxis : [ { type : 'category', data : ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'], axistick: { alignwithlabel: true } } ], yaxis : [ { type : 'value' } ], series : [ { name:'直接访问', type:'bar', barwidth: '60%', data:[10, 52, 200, 334, 390, 330, 220] } ] }; mychart.setoption(option); var mychart = echarts.init(document.getelementbyid('mains')); var option = { title : { text: '某站点用户访问来源', subtext: '纯属虚构', x:'center' }, tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" }, legend: { orient: 'vertical', left: 'left', data: ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎'] }, series : [ { name: '访问来源', type: 'pie', radius : '55%', center: ['50%', '60%'], data:[ {value:335, name:'直接访问'}, {value:310, name:'邮件营销'}, {value:234, name:'联盟广告'}, {value:135, name:'视频广告'}, {value:1548, name:'搜索引擎'} ], itemstyle: { emphasis: { shadowblur: 10, shadowoffsetx: 0, shadowcolor: 'rgba(0, 0, 0, 0.5)' } } } ] }; mychart.setoption(option); } render() { return ( <div> <div classname={'home'}> <div classname={'home-left'}> <div classname={'h-l-box'}></div> <div classname={'h-l-box'}></div> <div classname={'h-l-box'}> <div id="main" style={{width:"100%",height:"100%"}}></div> </div> </div> <div classname={'home-right'}> <div classname={'h-r-box h-r-flex'} style={{width:"100%",height:"20%"}}> <div></div> <div></div> <div></div> <div></div> </div> <div classname={'h-r-box'} style={{width:"100%",height:"8%"}}></div> <div classname={'h-r-box'}></div> <div classname={'h-r-box h-r-flex'}> <div style={{margin:"0"}}> <div id="mains" style={{width:"80%",height:"100%",border:"0"}}></div> </div> <div style={{margin:"0 0 0 0.5rem"}}></div> </div> </div> </div> {/*<div classname="home-footer"></div>*/} </div> ); } } export default homecomponent
最后一步我们需要配置我们的最后的增删改查页面这里我们也要注意一点我们要使用分页器使用蚂蚁金服当中的分页器解决
import react, {component} from 'react' import '../asstes/css/table.css' import {button,input} from 'antd'; import {pagination} from 'antd'; class tablecomponent extends component { constructor() { super() this.state = { list: [], last_page:'', link:'' } } componentdidmount() { let url = 'url' fetch(url,{ method:"get" }) .then(res => res.json()) .then(json => this.setstate({list: json.data,last_page:json.last_page}, () => { // console.log(this.state.total) })) } link(){ let url = "url"+this.state.link fetch(url) .then(res=>res.json()) .then(json=>this.setstate({list:json.data})) } render() { return ( <div> <div style={{width:"100%"}}><input style={{width:"95%"}} placeholder="basic usage" onchange={e => {this.setstate({link:e.target.value})}}/> <button type='primary' onclick={this.link.bind(this)}>查询</button></div> <table> <thead> <tr classname={'table-tr'}> <th>班级</th> <th>学生</th> <th>项目答辩(实训一)</th> <th>项目答辩(实训二)</th> <th>项目答辩(实训三)</th> <th>面试简历</th> <th>操作</th> </tr> </thead> <tbody> {this.state.list.map((item, index) => { return ( <tr classname={'table-tr'} key={index}> <td>{item.classname}</td> <td>{item.username}</td> <td>{item.d*1 === 1 ? <span style={{backgroundcolor: "green"}}>已答辩</span> : <span style={{backgroundcolor:"orange"}}>未答辩</span>}</td> <td>{item.d*2 === 1 ? <span style={{backgroundcolor: "green"}}>已答辩</span> : <span style={{backgroundcolor:"orange"}}>未答辩</span>}</td> <td>{item.d*3 === 1 ? <span style={{backgroundcolor: "green"}}>已答辩</span> : <span style={{backgroundcolor:"orange"}}>未答辩</span>}</td> <td>{item.jianli_status === 1 ? <span style={{backgroundcolor: "green"}}>已开始</span> : <span style={{backgroundcolor:"orange"}}>未开始</span>}</td> <td> <button type="primary" style={{backgroundcolor: "red", border: "1px solid red"}}>删除</button> <button type="primary" style={{margin: "0 0.5rem 0 0.5rem"}}>编辑</button> <button type="primary" style={{backgroundcolor: "orange", border: "1px solid orange"}}>抽取面试题</button> </td> </tr> ) })} <pagination onchange={page=>{ let url = 'url'+page+'&size=10' fetch(url) .then(res => res.json()) .then(json => this.setstate({list: json.data}, () => { console.log(this.state.list) })) }} defaultcurrent={1} total={(this.state.last_page)*10}/> </tbody> </table> </div> ) } } export default tablecomponent
最后我们的这个简单易懂的后台管理系统就完成了
上一篇: Go处理json数据