(记录笔记8)——8.1 城市管理

城市管理



Github代码

一 基础页面

  • pages->city->index.js: 对应路由 /admin/city

  • 顶部子组件一:选择表单

  • class FilterForm extends React.Component{
     
        render(){
            const { getFieldDecorator } = this.props.form;
            return (
                <Form layout="inline">
                    <FormItem label="城市">
                        {
                            getFieldDecorator('city_id')(
                                <Select
                                    style={{width:100}}
                                    placeholder="全部"
                                >
                                    <Option value="">全部</Option>
                                    <Option value="1">北京市</Option>
                                    <Option value="2">天津市</Option>
                                    <Option value="3">深圳市</Option>
                                </Select>
                            )
                        }
                    </FormItem>
                    <FormItem label="用车模式">
                        {
                            getFieldDecorator('mode')(
                                <Select
                                    style={{ width: 120 }}
                                    placeholder="全部"
                                >
                                    <Option value="">全部</Option>
                                    <Option value="1">指定停车点模式</Option>
                                    <Option value="2">禁停区模式</Option>
                                </Select>
                            )
                        }
                    </FormItem>
                    <FormItem label="营运模式">
                        {
                            getFieldDecorator('op_mode')(
                                <Select
                                    style={{ width: 80 }}
                                    placeholder="全部"
                                >
                                    <Option value="">全部</Option>
                                    <Option value="1">自营</Option>
                                    <Option value="2">加盟</Option>
                                </Select>
                            )
                        }
                    </FormItem>
                    <FormItem label="加盟商授权状态">
                        {
                            getFieldDecorator('auth_status')(
                                <Select
                                    style={{ width: 100 }}
                                    placeholder="全部"
                                >
                                    <Option value="">全部</Option>
                                    <Option value="1">已授权</Option>
                                    <Option value="2">未授权</Option>
                                </Select>
                            )
                        }
                    </FormItem>
                    <FormItem>
                        <Button type="primary" style={{margin:'0 20px'}}>查询</Button>
                        <Button>重置</Button>
                    </FormItem>
                </Form>
            );
        }
    }
    FilterForm = Form.create({})(FilterForm);
    
  • Easy Mock城市管理的数据接口: /open_city

    {
      "code": 0,
      "msg": "",
      "result": {
        "item_list|10": [{
          "id|+1": 1,
          "name": "@city",
          "mode|1-2": 1,
          "op_mode|1-2": 1,
          "franchisee_id": 77,
          "franchisee_name": "松果自营",
          "city_admins|1-2": [{
            "user_name": "@cname",
            "user_id|+1": 10001
          }],
          "open_time": "@datetime",
          "sys_user_name": "@cname",
          "update_time": 1546580667000
        }]
      },
      page: 1,
      page_size: 10,
      total: 20
    }
    
1. componentDidMount():

调用 this.requestList(),默认请求接口数据

 componentDidMount() {
    this.requestList();
  }

  // 默认请求我们的接口数据
  requestList = ()=>{
    let _this = this;
    axios.ajax({
      url: '/open_city',
      data:{
        params:{
          page:this.params.page
        }
      }
    }).then((res)=>{
      let list = res.result.item_list.map((item, index) => {
        item.key = index;
        return item; // 注:这里要返回item的值
      });
      this.setState({
        list:list,
        pagination:Utils.pagination(res,(current)=>{
          _this.params.page = current;
          _this.requestList();
        })
      })
    })
  }
  
  render(){
      ...
  }

2. 实例代码

注:这里使用 div 容器 嵌套 Table表格的用法

// src/pages/city/index.js
import React from 'react';
import {Card, Table, Form, Modal, Button, message, Input, Icon, Select,} from 'antd';
import axios from './../../axios/index';
import Utils from './../../utils/utils';

const FormItem = Form.Item;
const Option = Select.Option;

export default class City extends React.Component {

  state = {
    list: [],
  };

  params = {
    page: 1
  };

  componentDidMount() {
    this.requestList();
  }

  // 默认请求我们的接口数据
  requestList = ()=>{
    let _this = this;
    axios.ajax({
      url: '/open_city',
      data:{
        params:{
          page:this.params.page
        }
      }
    }).then((res)=>{
      let list = res.result.item_list.map((item, index) => {
        item.key = index;
        return item;
      });
      this.setState({
        list:list,
        pagination:Utils.pagination(res,(current)=>{
          _this.params.page = current;
          _this.requestList();
        })
      })
    })
  }

  render() {
    const columns = [
      {
        title: '城市ID',
        dataIndex: 'id'
      }, {
        title: '城市名称',
        dataIndex: 'name'
      }, {
        title: '用车模式',
        dataIndex: 'mode',
        render(mode) {
          return mode === 1 ? '停车点' : '禁停区';
        }
      }, {
        title: '营运模式',
        dataIndex: 'op_mode',
        render(op_mode) {
          return op_mode === 1 ? '自营' : '加盟';
        }
      }, {
        title: '授权加盟商',
        dataIndex: 'franchisee_name'
      }, {
        title: '城市管理员',
        dataIndex: 'city_admins',
        render(arr) {  //处理数组类型
          return arr.map((item) => {
            return item.user_name;
          }).join(',');
        }
      }, {
        title: '城市开通时间',
        dataIndex: 'open_time'
      }, {
        title: '操作时间',
        dataIndex: 'update_time',
        render: Utils.formateDate //格式化时间戳
      }, {
        title: '操作人',
        dataIndex: 'sys_user_name'
      }
    ];
    return (
      <div>
        <Card>
          <FilterForm/>
        </Card>
        <Card style={{marginTop: 10}}>
          <Button type="primary">开通城市</Button>
        </Card>

        <div className="content-wrap">
          <Table
            bordered
            columns={columns}
            dataSource={this.state.list}
          />
        </div>
      </div>
    );
  }

}


class FilterForm extends React.Component {

  render() {
    const {getFieldDecorator} = this.props.form;
    return (
        <Form layout="inline">
          <FormItem label="城市">
            {
              getFieldDecorator('city_id')(
                <Select
                  style={{width: 100}}
                  placeholder="全部"
                >
                  <Option value="">全部</Option>
                  <Option value="1">北京市</Option>
                  <Option value="2">天津市</Option>
                  <Option value="3">深圳市</Option>
                </Select>
              )
            }
          </FormItem>
          <FormItem label="用车模式">
            {
              getFieldDecorator('mode')(
                <Select
                  style={{width: 120}}
                  placeholder="全部"
                >
                  <Option value="">全部</Option>
                  <Option value="1">指定停车点模式</Option>
                  <Option value="2">禁停区模式</Option>
                </Select>
              )
            }
          </FormItem>
          <FormItem label="营运模式">
            {
              getFieldDecorator('op_mode')(
                <Select
                  style={{width: 80}}
                  placeholder="全部"
                >
                  <Option value="">全部</Option>
                  <Option value="1">自营</Option>
                  <Option value="2">加盟</Option>
                </Select>
              )
            }
          </FormItem>
          <FormItem label="加盟商授权状态">
            {
              getFieldDecorator('auth_status')(
                <Select
                  style={{width: 100}}
                  placeholder="全部"
                >
                  <Option value="">全部</Option>
                  <Option value="1">已授权</Option>
                  <Option value="2">未授权</Option>
                </Select>
              )
            }
          </FormItem>

          <FormItem>
            <Button type="primary" style={{margin: '0 20px'}}>查询</Button>
            <Button>重置</Button>
          </FormItem>
        </Form>
    );
  }
}

FilterForm = Form.create({})(FilterForm);

二 弹窗功能

为以上组件,增加点击按钮 弹窗(Modal)功能

  1. [开通城市]按钮:监听 onClick 事件,调用this.handleOpenCity()显示弹框

    state = {
        list:[],
    +   isShowOpenCity:false      // 默认不可见
    }
    
    // 开通城市
    + handleOpenCity = ()=>{
    +    this.setState({
    +            isShowOpenCity:true
    +    })
    + }
    
  2. Modal 部分默认隐藏,触发事件进行显示

    onOk:点击确定回调,参数为关闭函数,

    onCancel:取消回调,参数为关闭函数

    <Modal
           title="开通城市"
           visible={this.state.isShowOpenCity}
           onCancel={()=>{
               this.setState({
                    isShowOpenCity:false
               })
           }}
           onOk={this.handleSubmit}
    >
           <OpenCityForm wrappedComponentRef={(inst)=>{this.cityForm = inst;}}/>
    </Modal>
    

    wrappedComponentRef属性:

    拿到表单中的信息对象inst,通过this.cityForm存到state中

  3. 城市开通提交

    // 城市开通提交
    handleSubmit = ()=>{
            let cityInfo = this.cityForm.props.form.getFieldsValue();
            console.log(cityInfo);
            axios.ajax({
                url:'/city/open',
                data:{
                    params:cityInfo
                }
            }).then((res)=>{
                if(res.code === 0){
                    message.success('开通成功');
                    this.setState({
                        isShowOpenCity:false
                    })
                    this.requestList();
                }
            })
    }
    
  • 弹框子组件二:开通城市表单

  • class OpenCityForm extends React.Component {
      render() {
        const formItemLayout = {
          labelCol: {
            span: 5
          },
          wrapperCol: {
            span: 19
          }
        };
        const {getFieldDecorator} = this.props.form;
        return (
          <Form layout="horizontal">
            <FormItem label="选择城市"  {...formItemLayout}>
              {
                getFieldDecorator('city_id', {
                  initialValue: '1'
                })(
                  <Select style={{width: 100}}>
                    <Option value="">全部</Option>
                    <Option value="1">北京市</Option>
                    <Option value="2">天津市</Option>
                  </Select>
                )
              }
            </FormItem>
            <FormItem label="营运模式"  {...formItemLayout}>
              {
                getFieldDecorator('op_mode', {
                  initialValue: '1'
                })(
                  <Select style={{width: 100}}>
                    <Option value="1">自营</Option>
                    <Option value="2">加盟</Option>
                  </Select>
                )
              }
    
            </FormItem>
            <FormItem label="用车模式"  {...formItemLayout}>
              {
                getFieldDecorator('use_mode', {
                  initialValue: '1'
                })(
                  <Select style={{width: 100}}>
                    <Option value="1">指定停车点</Option>
                    <Option value="2">禁停区</Option>
                  </Select>
                )
              }
            </FormItem>
          </Form>
        );
      }
    }
    
    OpenCityForm = Form.create({})(OpenCityForm);
    
    • Easy Mock城市管理的数据接口: /open_city

      {
        "code": 0,
        "list": "开通成功"
      }
      
实例代码
// src/pages/city/index.js
import React from 'react';
import {Card, Table, Form, Modal, Button, message, Select,} from 'antd';
import axios from './../../axios/index';
import Utils from './../../utils/utils';

const FormItem = Form.Item;
const Option = Select.Option;

export default class City extends React.Component {

state = {
 list: [],
 isShowOpenCity: false
};

params = {
 page: 1
};

componentDidMount() {
 this.requestList();
}

// 默认请求我们的接口数据
requestList = () => {
 let _this = this;
 axios.ajax({
   url: '/open_city',
   data: {
     params: {
       page: this.params.page
     }
   }
 }).then((res) => {
   let list = res.result.item_list.map((item, index) => {
     item.key = index;
     return item;
   });
   this.setState({
     list: list,
     pagination: Utils.pagination(res, (current) => {
       _this.params.page = current;
       _this.requestList();
     })
   });
 });
};

// 开通城市
handleOpenCity = () => {
 this.setState({
   isShowOpenCity: true
 });
};

// 城市开通提交
handleSubmit = () => {
 let cityInfo = this.cityForm.props.form.getFieldsValue();
 console.log(cityInfo);
 axios.ajax({
   url: '/city/open',
   data: {
     params: cityInfo
   }
 }).then((res) => {
   if (res.code == 0) {
     message.success('开通成功');
     this.setState({
       isShowOpenCity:false
     });

     this.requestList();
   }
 });
};

render() {
 const columns = [
   {
     title: '城市ID',
     dataIndex: 'id'
   }, {
     title: '城市名称',
     dataIndex: 'name'
   }, {
     title: '用车模式',
     dataIndex: 'mode',
     render(mode) {
       return mode === 1 ? '停车点' : '禁停区';
     }
   }, {
     title: '营运模式',
     dataIndex: 'op_mode',
     render(op_mode) {
       return op_mode === 1 ? '自营' : '加盟';
     }
   }, {
     title: '授权加盟商',
     dataIndex: 'franchisee_name'
   }, {
     title: '城市管理员',
     dataIndex: 'city_admins',
     render(arr) {  //处理数组类型
       return arr.map((item) => {
         return item.user_name;
       }).join(',');
     }
   }, {
     title: '城市开通时间',
     dataIndex: 'open_time',
     render:Utils.formateDate
   }, {
     title: '操作时间',
     dataIndex: 'update_time',
     render: Utils.formateDate //格式化时间戳
   }, {
     title: '操作人',
     dataIndex: 'sys_user_name'
   }
 ];
 return (
   <div>
     <Card>
       <FilterForm/>
     </Card>
     <Card style={{marginTop: 10}}>
       <Button type="primary" onClick={this.handleOpenCity}>开通城市</Button>
     </Card>

     <div className="content-wrap">
       <Table
         bordered
         columns={columns}
         dataSource={this.state.list}
       />
     </div>
     <Modal
       title="开通城市"
       visible={this.state.isShowOpenCity}
       onCancel={() => {
         this.setState({
           isShowOpenCity: false
         });
       }}

       onOk={this.handleSubmit}
     >
       <OpenCityForm wrappedComponentRef={(inst) => {
         this.cityForm = inst;
       }}/>
     </Modal>
   </div>
 );
}

}

//子组件一:选择表单
class FilterForm extends React.Component {

render() {
 const {getFieldDecorator} = this.props.form;
 return (
     <Form layout="inline">
       <FormItem label="城市">
         {
           getFieldDecorator('city_id')(
             <Select
               style={{width: 100}}
               placeholder="全部"
             >
               <Option value="">全部</Option>
               <Option value="1">北京市</Option>
               <Option value="2">天津市</Option>
               <Option value="3">深圳市</Option>
             </Select>
           )
         }
       </FormItem>
       <FormItem label="用车模式">
         {
           getFieldDecorator('mode')(
             <Select
               style={{width: 120}}
               placeholder="全部"
             >
               <Option value="">全部</Option>
               <Option value="1">指定停车点模式</Option>
               <Option value="2">禁停区模式</Option>
             </Select>
           )
         }
       </FormItem>
       <FormItem label="营运模式">
         {
           getFieldDecorator('op_mode')(
             <Select
               style={{width: 80}}
               placeholder="全部"
             >
               <Option value="">全部</Option>
               <Option value="1">自营</Option>
               <Option value="2">加盟</Option>
             </Select>
           )
         }
       </FormItem>
       <FormItem label="加盟商授权状态">
         {
           getFieldDecorator('auth_status')(
             <Select
               style={{width: 100}}
               placeholder="全部"
             >
               <Option value="">全部</Option>
               <Option value="1">已授权</Option>
               <Option value="2">未授权</Option>
             </Select>
           )
         }
       </FormItem>

       <FormItem>
         <Button type="primary" style={{margin: '0 20px'}}>查询</Button>
         <Button>重置</Button>
       </FormItem>
     </Form>
 );
}
}

FilterForm = Form.create({})(FilterForm);

//子组件二:开通城市
class OpenCityForm extends React.Component {
render() {
 const formItemLayout = {
   labelCol: {
     span: 5
   },
   wrapperCol: {
     span: 19
   }
 };
 const {getFieldDecorator} = this.props.form;
 return (
   <Form layout="horizontal">
     <FormItem label="选择城市"  {...formItemLayout}>
       {
         getFieldDecorator('city_id', {
           initialValue: '1'
         })(
           <Select style={{width: 100}}>
             <Option value="">全部</Option>
             <Option value="1">北京市</Option>
             <Option value="2">天津市</Option>
           </Select>
         )
       }
     </FormItem>
     <FormItem label="营运模式"  {...formItemLayout}>
       {
         getFieldDecorator('op_mode', {
           initialValue: '1'
         })(
           <Select style={{width: 100}}>
             <Option value="1">自营</Option>
             <Option value="2">加盟</Option>
           </Select>
         )
       }

     </FormItem>
     <FormItem label="用车模式"  {...formItemLayout}>
       {
         getFieldDecorator('use_mode', {
           initialValue: '1'
         })(
           <Select style={{width: 100}}>
             <Option value="1">指定停车点</Option>
             <Option value="2">禁停区</Option>
           </Select>
         )
       }
     </FormItem>
   </Form>
 );
}
}

OpenCityForm = Form.create({})(OpenCityForm);

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值