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. Git简单上传和下载

    本文参考 git-简易指南 编写 上传本地代码到gitHub仓库 第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的所有文件添加到仓库中 git a ...

  2. Mybatis学习笔记1 - Hello World

    1.pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  3. [转]25个HTML5和JavaScript游戏引擎库

    本文转自:http://www.open-open.com/news/view/27c6ed 1. The GMP JavaScript Game Engine GMP是一个基于精灵2-D游戏,它可以 ...

  4. 工作采坑札记:2. Hadoop中MultipleInputs的使用陷阱

    1. 背景 近日在一个Hadoop项目中使用MultipleInputs增加多输入文件时,发现相同路径仅会加载一次,导致后续的统计任务严重失真.本博文旨在记录异常的排查及解决方案. 2. 情景重现 ( ...

  5. 【转】Android实现伸缩弹力分布菜单效果

    本文介绍下在Android中实现伸缩弹力分布菜单效果.关于这种菜单效果在IPhone中比较常见,效果比较酷.那么在Android中实现只是一种简单的模仿. 这两天无意间看到一园友的博文实现Path2. ...

  6. Redis学习1

    Redis 学习记录 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zs ...

  7. the wait queue

    using System; using System.Collections.Concurrent; using System.Threading; namespace Base { public c ...

  8. intellijidea课程 intellijidea神器使用技巧 6-1 Spring的关联

    待学完spring之后再来看 Spring的关联位置:菜单->File->Project Structure->Facets功能:帮助管理Spring容器.还提供了很多其他的管理,比 ...

  9. 账户密码提示 jq简单事件

    $(".username").focus(function(){ if($(this).val()=="请输入用户名"){ $(this).val(" ...

  10. #include stdio.h(4)

    #include <stdio.h> int main() { //****************1.数组*************** //什么是数组:专门用来存放数据的 /* 格式 ...