react新手入门教程03-纯前端渲染table+弹窗

此处输入图片的描述
此处输入图片的描述

需要antd中两个组件:
1.Modal
2.Table

样式引入

  • 表格
  • 弹窗

表格

整个组件放在'/all'这个路由下
所以在pages文件夹下新建all文件夹, 在all文件夹中新建index.js

并引入antd中Table组件

//app/public/src/pages/all/index.js

import React, { Component } from 'react';
import { Table } from 'antd'

class All extends Component {
  constructor(props) {
    super(props);
    this.state = {

    }
  }


  render() {
    return (
      <div className="content-inner">
        <Table/>
      </div>
    );
  }
}
export default All;

在router.js中定义'/all'路由与all组件的映射

// 子组件
import Welcome from './pages/welcome';
+import All from './pages/all';



export default () => (
  <Router>
    <div>
      <Header/>
      <div
        className='main-contains'
        style={{
          minHeight: document.body.clientHeight,
        }}
      >
          <Breadcrumb/>
          <Switch>
            {/* welcome */}
            <Route exact path="/" component={Welcome} />
+           <Route exact path="/all" component={All} />
          </Switch>
      </div>
    </div>
  </Router>
);

点击'/all'路由可以看到效果如下
表示all组件已经渲染成功

此处输入图片的描述
此处输入图片的描述

不过问题是并没有表格头
所有下一步先定义表格头


  render() {
    //columns为Table组件自带方法
    return (
      <div className="content-inner">
        <Table
+       columns={this.columns}
        />
      </div>
    );
  }

  //定义表格
+  columns = [{
+    title: '姓名',
+   dataIndex: 'name',
+    key: 'name',
+  }, {
+    title: '年龄',
+    dataIndex: 'age',
+    key: 'age',
+  }, {
+    title: '住址',
+    dataIndex: 'address',
+    key: 'address',
+  }];
}
export default All;

弹窗

弹窗属于编辑操作,所以把它作为all组件的子组件
在all文件夹中新建edit文件夹,并在edit中新建index.js

编辑edit/index.js文件, 引入antd中的Modal组件
设置visible = true 默认显示弹窗

//app/public/src/pages/all/edit/index.js


import React, { Component } from 'react';
import { Modal } from 'antd'


class EditModel extends Component {
  constructor(props) {
    super(props);
    this.state = {
    }
  }

  render() {
    //visible为Modal组件自带方法
    return (
      <Modal
      visible = { true }
      >
      </Modal>
    );
  }
}

export default EditModel;

效果


此处输入图片的描述
此处输入图片的描述

不过目前弹窗还不可以控制,
所以下一步, 给visible的值设个变量,来控制弹窗的显示和关闭
并在all/index.js 中加入一个Button,通过onClick来改变visible的值

  • 点击button显示弹窗
//app/public/src/pages/all/index.js


import React, { Component } from 'react';
+import { Button, Table } from 'antd'

//子组件
import EditModal from './edit'

class All extends Component {
  constructor(props) {
    super(props);
    this.state = {
+      editVisiable:false,
    }
  }

  //显示弹窗
+  addDataSource = () =>{
+    this.setState({
+      editVisiable:true,
+    })
+  }

  render() {
+    const { editVisiable } = this.state

    return (
      <div className="content-inner">
+        <Button type ='primary' onClick={ this.addDataSource }> 新建数据</Button>
        <Table
        columns={this.columns}
        />
        <EditModal
 +       editVisiable={ editVisiable }
        />
      </div>
    );
  }
//app/public/src/pages/all/edit/index.js



  render() {
    const { editVisiable } = this.props
    return (
      <Modal
+       visible = { editVisiable }
      >
      </Modal>
    );
  }


export default EditModel;

  • 取消弹窗
    这里值得注意的一点是:
    基于react单向数据流的特点,
    子组件改变父组件中state的值时,
    通常的做法是,通过父组件传递改变state的方法给子组件,子组件调用这个方法实现:
//app/public/src/pages/all/index.js


class All extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editVisiable:false,
    }
  }

  //显示弹窗
  addDataSource = () =>{
    this.setState({
      editVisiable:true,
    })
  }

+  //取消弹窗
+  onModelCancel = () =>{
+    this.setState({
+      editVisiable:false,
+    })
+  }

  render() {
    const { editVisiable } = this.state

    return (
      <div className="content-inner">
        <Button type ='primary' onClick={ this.addDataSource }> 新建数据</Button>
        <Table
        columns={this.columns}
        />
        <EditModal
         editVisiable={ editVisiable }
 +       onModelCancel={ this.onModelCancel}
        />
      </div>
    );
  }
}

//app/public/src/pages/all/edit/index.js


class EditModel extends Component {
  constructor(props) {
    super(props);
    this.state = {
    }
  }



  render() {
     //onCancel为Modal组件自带方法
+    const { editVisiable, onModelCancel } = this.props
     return (
       <Modal
       visible = { editVisiable }
+      onCancel= { onModelCancel }
       >
       </Modal>
    );
  }

}

export default EditModel;

数据交互

需要的组件基本完成,就差弹窗中数据输入的表单组件,

表单

添加表单组件, 并引入相关逻辑,

//app/public/src/pages/all/edit/index.js


import React, { Component } from 'react';
+import { Modal, Form, Input } from 'antd'

+const FormItem = Form.Item;

+// 样式
+const formLayout = {
+  labelCol: {
+    xs: { span: 6 },
+    sm: { span: 6 },
+  },
+  wrapperCol: {
+    xs: { span: 6 },
+    sm: { span: 15 },
+  },
+};


class EditModel extends Component {
  constructor(props) {
    super(props);
    this.state = {
+      key:0,
    }
  }


+  onOk = () => {
+   const { onModelCancel, saveData} = this.props
+    //getFieldsValue() 获取表单中输入的值
+    const { getFieldsValue, resetFields } = this.props.form
+    const values = getFieldsValue()
+    //antd table需要加一个key字段
+    const key = this.state.key + 1
+    this.setState({
+      key:key,
+    })
+    values.key = key
+
+    //重置表单 (坑点)
+    resetFields()

+    saveData(values)
+    onModelCancel()
+  }


  render() {
    const { editVisiable, onModelCancel } = this.props
+    // getFieldDecorator用于定义表单中的数据
+    const { getFieldDecorator } = this.props.form
     return (
       <Modal
       visible = { editVisiable }
       onCancel = { onModelCancel }
 +     onOk = { this.onOk }
       >
 +       <Form>
 +         <FormItem
 +           label="姓名"
 +           {...formLayout}
 +         >
 +           {getFieldDecorator('name', {
 +             rules: [{
 +               required: true, message: '姓名必填',
 +             }],
 +           })(
 +             <Input />
 +           )}
 +         </FormItem>
 +         <FormItem
 +           label="年龄"
 +           {...formLayout}
 +         >
 +           {getFieldDecorator('age', {
 +             rules: [{
 +               required: true, message: '姓名必填',
 +             }],
 +           })(
 +             <Input />
 +           )}
 +         </FormItem>
 +         <FormItem
 +           label="住址"
 +           {...formLayout}
 +         >
 +           {getFieldDecorator('address', {
 +             rules: [{
 +               required: true, message: '住址必填',
 +             }],
 +           })(
 +             <Input />
 +           )}
 +         </FormItem>
 +       </Form>
      </Modal>
    );
  }

}
//Form.create()传入表单的方法给EditModel
+ export default Form.create()(EditModel);

//app/public/src/pages/all/index.js


import React, { Component } from 'react';
import { Button, Table } from 'antd'

//子组件
import EditModal from './edit'

class All extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editVisiable:false,
+     dataSource:[],
    }
  }

  //显示弹窗
  addDataSource = () =>{
    this.setState({
      editVisiable:true,
    })
  }

  //取消弹窗
  onModelCancel = () =>{
    this.setState({
      editVisiable:false,
    })
  }

+  //储存数据
+  saveData = (updateData) => {

+    const { dataSource } = this.state
+    dataSource.push(updateData)

+    this.setState({
+    dataSource:dataSource,
+    })

+  }

  render() {
+    // editVisiable控制弹窗显示, dataSource为tabale渲染的数据
+    const { editVisiable, dataSource } = this.state

    return (
      <div className="content-inner">
        <Button type ='primary' onClick={ this.addDataSource }> 新建数据</Button>
        <Table
        columns = {this.columns}
        dataSource={dataSource}
        />
        <EditModal
        editVisiable={ editVisiable }
        onModelCancel={ this.onModelCancel}
 +       saveData = { this.saveData }
        />
      </div>
    );
  }

  //定义表格
  columns = [{
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  }, {
    title: '年龄',
    dataIndex: 'age',
    key: 'age',
  }, {
    title: '住址',
    dataIndex: 'address',
    key: 'address',
  }];
}
export default All;

这里都写了备注,就不详细说明了

参考: https://ant.design/components/table-cn/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容