React后台管理系统-品类的增加、修改和查看

1.页面

2.品类列表展示

  1. let listBody = this.state.list.map((category, index) => {
  2.             return (
  3.                 <tr key={index}>
  4.                     <td>{category.id}</td>
  5.                     <td>{category.name}</td>
  6.                     <td>
  7.                         <a className="opear"
  8.                             onClick={(e) => this.onUpdateName(category.id, category.name)}>修改名称</a>
  9.                         {
  10.                             category.parentId === 0
  11.                             ? <Link to={`/product-category/index/${category.id}`}>查看子品类</Link>
  12.                             : null
  13.                         }
  14.                     </td>
  15.                 </tr>
  16.             );
  17.         });
  18.         return (
  19.             <div id="page-wrapper">
  20.                 <PageTitle title="品类列表">
  21.                     <div className="page-header-right">
  22.                         <Link to="/product-category/add" className="btn btn-primary">
  23.                             <i className="fa fa-plus"></i>
  24.                             <span>添加品类</span>
  25.                         </Link>
  26.                     </div>
  27.                 </PageTitle>
  28.                 <div className="row">
  29.                     <div className="col-md-12">
  30.                         <p>父品类ID: {this.state.parentCategoryId}</p>
  31.                     </div>
  32.                 </div>
  33.                 <TableList tableHeads={[‘品类ID‘, ‘品类名称‘, ‘操作‘]}>
  34.                     {listBody}
  35.                 </TableList>
  36.             </div>
  37.         );
  38.     }

3.加载品类列表

  1. // 加载品类列表
  2.     loadCategoryList(){
  3.        _product.getCategoryList(this.state.parentCategoryId).then(res => {
  4.            this.setState({
  5.                list : res
  6.            });
  7.        }, errMsg => {
  8.            this.setState({
  9.                list : []
  10.            });
  11.            _mm.errorTips(errMsg);
  12.        });
  13.    }

4.修改品类名称

  1. // 更新品类的名字
  2.     onUpdateName(categoryId, categoryName){
  3.        let newName = window.prompt(‘请输入新的品类名称‘, categoryName);
  4.        if(newName){
  5.            _product.updateCategoryName({
  6.                categoryId: categoryId,
  7.                categoryName : newName
  8.            }).then(res => {
  9.                _mm.successTips(res);
  10.                this.loadCategoryList();
  11.            }, errMsg => {
  12.                _mm.errorTips(errMsg);
  13.            });
  14.        }
  15.    }

5.添加品类

  1. import React from ‘react‘;
  2. import MUtil from ‘util/mm.jsx‘
  3. import Product from ‘service/product-service.jsx‘
  4.  
  5. import PageTitle from ‘component/page-title/index.jsx‘;
  6.  
  7. const _mm = new MUtil();
  8. const _product = new Product();
  9.  
  10.  
  11. class CategoryAdd extends React.Component{
  12.     constructor(props){
  13.         super(props);
  14.         this.state = {
  15.             categoryList : [],
  16.             parentId : 0,
  17.             categoryName : ‘‘
  18.         };
  19.     }
  20.     componentDidMount(){
  21.         this.loadCategoryList();
  22.     }
  23.     // 加载品类列表,显示父品类列表
  24.     loadCategoryList(){
  25.         _product.getCategoryList().then(res => {
  26.             this.setState({
  27.                 categoryList : res
  28.             });
  29.         }, errMsg => {
  30.             _mm.errorTips(errMsg);
  31.         });
  32.     }
  33.     // 表单的值发生变化
  34.     onValueChange(e){
  35.         let name = e.target.name,
  36.             value = e.target.value;
  37.         this.setState({
  38.             [name] : value
  39.         });
  40.     }
  41.     // 提交
  42.     onSubmit(e){
  43.         let categoryName = this.state.categoryName.trim();
  44.         // 品类名称不为空,提交数据
  45.         if(categoryName){
  46.             _product.saveCategory({
  47.                 parentId : this.state.parentId,
  48.                 categoryName : categoryName
  49.             }).then((res) => {
  50.                 _mm.successTips(res);
  51.                 this.props.history.push(‘/product-category/index‘);
  52.             }, (errMsg) => {
  53.                 _mm.errorTips(errMsg);
  54.             });
  55.         }
  56.         // 否则,提示错误
  57.         else{
  58.             _mm.errorTips(‘请输入品类名称‘);
  59.         }
  60.     }
  61.     render(){
  62.         return (
  63.             <div id="page-wrapper">
  64.                 <PageTitle title="品类列表"/>
  65.                 <div className="row">
  66.                     <div className="col-md-12">
  67.                         <div className="form-horizontal">
  68.                             <div className="form-group">
  69.                                 <label className="col-md-2 control-label">所属品类</label>
  70.                                 <div className="col-md-5">
  71.                                     <select name="parentId"
  72.                                         className="form-control"
  73.                                         onChange={(e) => this.onValueChange(e)}>
  74.                                         <option value="0">根品类/</option>
  75.                                         {
  76.                                             this.state.categoryList.map((category, index) => {
  77.                                                 return <option value={category.id} key={index}>根品类/{category.name}</option>
  78.                                             })
  79.                                         }
  80.                                     </select>
  81.                                 </div>
  82.                             </div>
  83.                             <div className="form-group">
  84.                                 <label className="col-md-2 control-label">品类名称</label>
  85.                                 <div className="col-md-5">
  86.                                     <input type="text" className="form-control"
  87.                                         placeholder="请输入品类名称"
  88.                                         name="categoryName"
  89.                                         value={this.state.name}
  90.                                         onChange={(e) => this.onValueChange(e)}/>
  91.                                 </div>
  92.                             </div>
  93.                             <div className="form-group">
  94.                                 <div className="col-md-offset-2 col-md-10">
  95.                                     <button type="submit" className="btn btn-primary"
  96.                                         onClick={(e) => {this.onSubmit(e)}}>提交</button>
  97.                                 </div>
  98.                             </div>
  99.                         </div>
  100.                     </div>
  101.                 </div>
  102.             </div>
  103.         );
  104.     }
  105. }
  106.  
  107. export
    default CategoryAdd;

原文地址:https://www.cnblogs.com/wangyawei/p/9233442.html

时间: 2024-10-05 04:10:00

React后台管理系统-品类的增加、修改和查看的相关文章

React后台管理系统-品类选择器二级联动组件

1.页面大致是这个样子 2.页面结构 <div className="col-md-10">            <select name="" onChange={(e) => this.onFirstCategoryChange(e)} className="form-control cate-select">                 <option value="">请

【共享单车】—— React后台管理系统开发手记:项目准备

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.项目概述       React全家桶 React基础知识.生命周期 Router 4.0 语法讲解 Redux集成开发      AnD UI组件 最实用基础组件 AntD栅格系统 ETable组件封装 BaseForm组件封装 表格内嵌单选.复选封装      公共机制封装 Axios请求

【共享单车】—— React后台管理系统开发手记:主页面架构设计

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.页面结构定义 左侧导航栏,右侧页面结构 右侧显示内容分别分为上Header.中Content和下Footer部分 二.目录结构定义 src->admin.js:项目主结构代码(index.js中替换App.js挂载到根节点) src->common.js:项目公共结构代码(类似admin.j

【共享单车】—— React后台管理系统开发手记:UI菜单各个组件使用(Andt UI组件)

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.按钮Button pages->ui->button.js:对应路由/admin/ui/buttons import React from 'react'; import {Card, Button, Radio} from 'antd' import './ui.less' class B

【共享单车】—— React后台管理系统开发手记:城市管理和订单管理

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.城市管理 pages->city->index.js:对应路由/admin/city 顶部子组件一:选择表单 class FilterForm extends React.Component{ render(){ const { getFieldDecorator } = this.prop

React后台管理系统-file-uploader组件

1.React文件上传组件github地址: https://github.com/SoAanyip/React-FileUpload 2.Util里边新建file-uploader文件夹,里边新建index.jsx import React from 'react'; import FileUpload from './react-fileupload.jsx';   class FileUploader extends React.Component{     render(){      

React后台管理系统-rich-editor组件

1.Simditor组件的github地址:https://github.com/mycolorway/simditor 网址:http://simditor.tower.im/ 2.在util里边新建rich-editor文件夹,里边新建index.jsx import React from 'react'; import Simditor from 'simditor'; import 'simditor/styles/simditor.scss'; import './index.scss

【共享单车】—— React后台管理系统开发手记:AntD Form基础组件

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.使用Form组件开发登录页面 pages->form->login.js:对应路由/admin/form/login import React from 'react' import {Card, Form, Input, Button, message, Icon, Checkbox} f

【共享单车】—— React后台管理系统开发手记:Router 4.0路由实战演练

前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. 一.React Router 4.0核心概念 4.0版本中已不需要路由配置,一切皆组件 react-router:基础路由包 提供了一些router的核心api,包括Router,Route,Switch等 react-router-dom:基于浏览器的路由(包含react-router) 提供了