1.页面大致是这个样子

2.页面结构

  1. <div className="col-md-10">
  2.            <select name="" onChange={(e) => this.onFirstCategoryChange(e)} className="form-control cate-select">
  3.                 <option value="">请选择一级分类</option>
  4.                 {
  5.                  //箭头函数=>右边,加上了{}就需要return,不加就不需要return
  6.                   this.state.firstCategoryList.map(
  7.                       (category, index) => <option value={category.id} key={index}>{category.name}</option>)
  8.                 }
  9.            </select>
  10.            { this.state.secondCategoryList.length ?
  11.            <select name="" onChange={(e) => this.onSecondCategoryChange(e)} className="form-control cate-select">
  12.                 <option value="">请选择二级分类</option>
  13.                 {
  14.                          this.state.secondCategoryList.map(
  15.                              (category, index)=> <option value={category.id} key={index}>{category.name}</option>
  16.                          )
  17.               }
  18.            </select> : null
  19.            }
  20.         </div>

3.定义state里边的数据

  1. this.state = {
  2.            firstCategoryList : [],
  3.            firstCategoryId : 0,
  4.            secondCategoryList : [],
  5.            secondCategoryId : 0,
  6.        }

监听select选择框,当一级品类和二级品类改变的时候, 更新state里边firstCategoryId和secondCategoryId的值

  1. //一级品类改变事件
  2.     onFirstCategoryChange(e){
  3.         //取一级品类的值,没有的话为0
  4.         let newValue=e.target.value || 0;
  5.         this.setState({
  6.  
  7.             firstCategoryId : newValue,
  8.             //当一级品类改变时清空二级品类
  9.             secondCategoryList : [],
  10.             secondCategoryId : 0,
  11.         },() => {
  12.             //加载二级分类
  13.             this.loadSecondCategory()
  14.         })
  15.     }
  16.     //二级品类改变事件
  17.     onSecondCategoryChange(e){
  18.            //取一级品类的值,没有的话为0
  19.            let newValue=e.target.value || 0;
  20.            this.setState({
  21.               secondCategoryId : newValue,
  22.            },() => {
  23.                //加载二级分类
  24.                this.onPropsCategoryChange();
  25.            })
  26.     }

加载一级分类

  1. //加载一级分类
  2.     loadFirstCategory(){
  3.         _product.getCategoryList().then(res => {
  4.             this.setState({
  5.                 firstCategoryList : res
  6.             });
  7.         }, errMsg => {
  8.             _mm.errorTips(errMsg);
  9.         });
  10.     }

加载二级分类

  1. //加载二级分类
  2.    // 加载二级分类
  3.    loadSecondCategory(){
  4.        _product.getCategoryList(this.state.firstCategoryId).then(res => {
  5.            this.setState({
  6.                secondCategoryList : res
  7.            });
  8.        }, errMsg => {
  9.            _mm.errorTips(errMsg);
  10.        });
  11.    }

4.把最新的firstCategoryId和secondCategoryId的值传入父组件,更新父组件里边一级品类和二级品类

  1. // 传给父组件选中的结果
  2.    onPropsCategoryChange(){
  3.        // 判断props里的回调函数存在
  4.        let categoryChangable = typeof this.props.onCategoryChange === 'function';
  5.        // 如果是有二级品类
  6.        if(this.state.secondCategoryId){
  7.            categoryChangable && this.props.onCategoryChange(this.state.secondCategoryId, this.state.firstCategoryId);
  8.        }
  9.        // 如果只有一级品类
  10.        else{
  11.            categoryChangable && this.props.onCategoryChange(this.state.firstCategoryId, 0);
  12.        }
  13.    }

父组件使用CategorySelector组件

  1. <div className="form-group">
  2.                        <label className="col-md-2 control-label">所属分类</label>
  3.                        <CategorySelector
  4.                         categoryId={this.state.categoryId}
  5.                         parentCategoryId={this.state.parentCategoryId}
  6.                         onCategoryChange={ (categoryId,parentCategoryId) => this.onCategoryChange(categoryId,parentCategoryId)} />

更新父组件state里边一级品类和二级品类的值

  1. //品类改变事件
  2.    onCategoryChange(categoryId,parentCategoryId){
  3.        this.setState({
  4.            categoryId : categoryId,
  5.            parentCategoryId : parentCategoryId
  6.        });
  7.    }

React后台管理系统-品类选择器二级联动组件的更多相关文章

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

    1.页面 2.品类列表展示 let listBody = this.state.list.map((category, index) => {             return (      ...

  2. React后台管理系统-添加商品组件

    引入了CategorySelector 二级联动组件.FileUploader图片上传组件和RichEditor富文本编辑组件 import React from 'react'; import MU ...

  3. 《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件

    实战 上接,笔记:https://blog.csdn.net/u010132177/article/details/104150177 https://gitee.com/pasaulis/react ...

  4. react后台管理系统路由方案及react-router原理解析

        最近做了一个后台管理系统主体框架是基于React进行开发的,因此系统的路由管理,选用了react-router(4.3.1)插件进行路由页面的管理配置. 实现原理剖析 1.hash的方式   ...

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

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  6. Vue + Element-ui实现后台管理系统(4)---封装一个ECharts组件的一点思路

    封装一个ECharts组件的一点思路 有关后台管理系统之前写过三遍博客,看这篇之前最好先看下这三篇博客.另外这里只展示关键部分代码,项目代码放在github上: mall-manage-system ...

  7. 《React后台管理系统实战 :二》antd左导航:cmd批量创建子/目录、用antd进行页面布局、分离左导航为单独组件、子路由、动态写左导航、css样式相对陷阱

    一.admin页面布局及路由创建 0)cmd批量创建目录及子目录 //创建各个目录,及charts和子目录bar md home category product role user charts\b ...

  8. 《React后台管理系统实战 :三》header组件:页面排版、天气请求接口及页面调用、时间格式化及使用定时器、退出函数

    一.布局及排版 1.布局src/pages/admin/header/index.jsx import React,{Component} from 'react' import './header. ...

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

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

随机推荐

  1. Silverlight FullScreen 全屏

    <UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...

  2. Maven导入jar包

    可在该网址查找:http://search.maven.org/#search%7Cga%7C1%7Cjunit

  3. Spring集成Quartz的3种方式

    1.使用xml配置方式 Maven依赖 <properties> <!-- spring版本号 --> <spring.version>4.2.2.RELEASE& ...

  4. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  5. PHP函数的引用传递(地址传递)

    PHP中的引用: 在PHP中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字.在PHP中引用意味着用不同的名字访问同一个变量的内容. 比如:$a = 'hello world'; $b = ...

  6. [转]png图片压缩大小但是不改变透明部分

    降低PNG图片存储大小方法,图片压缩方法,如何降低PNG图片存储大小?前提是分辨率和尺寸大小不变,图形的透明部分不变.请看如下办法,亲测可用. 1. 将PNG图片用PS打开. 2. 图像-模式-8位/ ...

  7. js对象动态赋值

    <view class="movies-template"> <template is="movieListTemplate" data=&q ...

  8. webpack打包将配置文件单独抽离不压缩打包

    webpack.config.js: plugins: [ //提取公共模块 new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', ch ...

  9. 错误Cannot find module 'stylus'

    vue项目中使用stylus预处理器写css语法,老是出现 Cannot find module ‘stylus’ 的错误,鼓捣了很久,包括webstorm中配置stylus的支持,安装依赖. 终于找 ...

  10. [转]C# 单例模式

    最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuable Object-Orient ...