React后台管理系统-品类选择器二级联动组件
1.页面大致是这个样子

2.页面结构
- <div className="col-md-10">
- <select name="" onChange={(e) => this.onFirstCategoryChange(e)} className="form-control cate-select">
- <option value="">请选择一级分类</option>
- {
- //箭头函数=>右边,加上了{}就需要return,不加就不需要return
- this.state.firstCategoryList.map(
- (category, index) => <option value={category.id} key={index}>{category.name}</option>)
- }
- </select>
- { this.state.secondCategoryList.length ?
- <select name="" onChange={(e) => this.onSecondCategoryChange(e)} className="form-control cate-select">
- <option value="">请选择二级分类</option>
- {
- this.state.secondCategoryList.map(
- (category, index)=> <option value={category.id} key={index}>{category.name}</option>
- )
- }
- </select> : null
- }
- </div>
3.定义state里边的数据
- this.state = {
- firstCategoryList : [],
- firstCategoryId : 0,
- secondCategoryList : [],
- secondCategoryId : 0,
- }
监听select选择框,当一级品类和二级品类改变的时候, 更新state里边firstCategoryId和secondCategoryId的值
- //一级品类改变事件
- onFirstCategoryChange(e){
- //取一级品类的值,没有的话为0
- let newValue=e.target.value || 0;
- this.setState({
- firstCategoryId : newValue,
- //当一级品类改变时清空二级品类
- secondCategoryList : [],
- secondCategoryId : 0,
- },() => {
- //加载二级分类
- this.loadSecondCategory()
- })
- }
- //二级品类改变事件
- onSecondCategoryChange(e){
- //取一级品类的值,没有的话为0
- let newValue=e.target.value || 0;
- this.setState({
- secondCategoryId : newValue,
- },() => {
- //加载二级分类
- this.onPropsCategoryChange();
- })
- }
加载一级分类
- //加载一级分类
- loadFirstCategory(){
- _product.getCategoryList().then(res => {
- this.setState({
- firstCategoryList : res
- });
- }, errMsg => {
- _mm.errorTips(errMsg);
- });
- }
加载二级分类
- //加载二级分类
- // 加载二级分类
- loadSecondCategory(){
- _product.getCategoryList(this.state.firstCategoryId).then(res => {
- this.setState({
- secondCategoryList : res
- });
- }, errMsg => {
- _mm.errorTips(errMsg);
- });
- }
4.把最新的firstCategoryId和secondCategoryId的值传入父组件,更新父组件里边一级品类和二级品类
- // 传给父组件选中的结果
- onPropsCategoryChange(){
- // 判断props里的回调函数存在
- let categoryChangable = typeof this.props.onCategoryChange === 'function';
- // 如果是有二级品类
- if(this.state.secondCategoryId){
- categoryChangable && this.props.onCategoryChange(this.state.secondCategoryId, this.state.firstCategoryId);
- }
- // 如果只有一级品类
- else{
- categoryChangable && this.props.onCategoryChange(this.state.firstCategoryId, 0);
- }
- }
父组件使用CategorySelector组件
- <div className="form-group">
- <label className="col-md-2 control-label">所属分类</label>
- <CategorySelector
- categoryId={this.state.categoryId}
- parentCategoryId={this.state.parentCategoryId}
- onCategoryChange={ (categoryId,parentCategoryId) => this.onCategoryChange(categoryId,parentCategoryId)} />
更新父组件state里边一级品类和二级品类的值
- //品类改变事件
- onCategoryChange(categoryId,parentCategoryId){
- this.setState({
- categoryId : categoryId,
- parentCategoryId : parentCategoryId
- });
- }
React后台管理系统-品类选择器二级联动组件的更多相关文章
- React后台管理系统-品类的增加、修改和查看
1.页面 2.品类列表展示 let listBody = this.state.list.map((category, index) => { return ( ...
- React后台管理系统-添加商品组件
引入了CategorySelector 二级联动组件.FileUploader图片上传组件和RichEditor富文本编辑组件 import React from 'react'; import MU ...
- 《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件
实战 上接,笔记:https://blog.csdn.net/u010132177/article/details/104150177 https://gitee.com/pasaulis/react ...
- react后台管理系统路由方案及react-router原理解析
最近做了一个后台管理系统主体框架是基于React进行开发的,因此系统的路由管理,选用了react-router(4.3.1)插件进行路由页面的管理配置. 实现原理剖析 1.hash的方式 ...
- 【共享单车】—— React后台管理系统开发手记:主页面架构设计
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- Vue + Element-ui实现后台管理系统(4)---封装一个ECharts组件的一点思路
封装一个ECharts组件的一点思路 有关后台管理系统之前写过三遍博客,看这篇之前最好先看下这三篇博客.另外这里只展示关键部分代码,项目代码放在github上: mall-manage-system ...
- 《React后台管理系统实战 :二》antd左导航:cmd批量创建子/目录、用antd进行页面布局、分离左导航为单独组件、子路由、动态写左导航、css样式相对陷阱
一.admin页面布局及路由创建 0)cmd批量创建目录及子目录 //创建各个目录,及charts和子目录bar md home category product role user charts\b ...
- 《React后台管理系统实战 :三》header组件:页面排版、天气请求接口及页面调用、时间格式化及使用定时器、退出函数
一.布局及排版 1.布局src/pages/admin/header/index.jsx import React,{Component} from 'react' import './header. ...
- 【共享单车】—— React后台管理系统开发手记:UI菜单各个组件使用(Andt UI组件)
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
随机推荐
- Silverlight FullScreen 全屏
<UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...
- Maven导入jar包
可在该网址查找:http://search.maven.org/#search%7Cga%7C1%7Cjunit
- Spring集成Quartz的3种方式
1.使用xml配置方式 Maven依赖 <properties> <!-- spring版本号 --> <spring.version>4.2.2.RELEASE& ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- PHP函数的引用传递(地址传递)
PHP中的引用: 在PHP中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字.在PHP中引用意味着用不同的名字访问同一个变量的内容. 比如:$a = 'hello world'; $b = ...
- [转]png图片压缩大小但是不改变透明部分
降低PNG图片存储大小方法,图片压缩方法,如何降低PNG图片存储大小?前提是分辨率和尺寸大小不变,图形的透明部分不变.请看如下办法,亲测可用. 1. 将PNG图片用PS打开. 2. 图像-模式-8位/ ...
- js对象动态赋值
<view class="movies-template"> <template is="movieListTemplate" data=&q ...
- webpack打包将配置文件单独抽离不压缩打包
webpack.config.js: plugins: [ //提取公共模块 new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', ch ...
- 错误Cannot find module 'stylus'
vue项目中使用stylus预处理器写css语法,老是出现 Cannot find module ‘stylus’ 的错误,鼓捣了很久,包括webstorm中配置stylus的支持,安装依赖. 终于找 ...
- [转]C# 单例模式
最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuable Object-Orient ...