ant-design 實現 搜索功能


1.邏輯代碼

list.js

/**
 * 用戶列表頁
 */
import React,{ PureComponent } from 'react'
import {connect} from 'react-redux'
import {history} from '../../store'
import styles from './index.less'
import { Row, Col, Card, Form, Input, Select, Button } from 'antd';

const FormItem = Form.Item;
const { Option } = Select;
const statusMap = ['success', 'error'];
const status = ['啟用', '禁用',];

/**
 *  用戶列表
 */
@Form.create()
class UserList extends PureComponent{
  state = {
    //
  };

  renderAdvancedForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch.bind(this)} layout="inline">
        {/*搜索條件*/}
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="賬號">
              {getFieldDecorator('username')(
                <Input placeholder="請輸入" />
              )}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="姓名">
              {getFieldDecorator('name')(
                <Input placeholder="請輸入" />
              )}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="狀態">
              {getFieldDecorator('status')(
                <Select placeholder="請選擇">
                  <Option value="0">啟用</Option>
                  <Option value="1">禁用</Option>
                </Select>
              )}
            </FormItem>
          </Col>
        </Row>
        {/*按鈕*/}
        <div style={{ marginBottom: 10 }}>
          <span>
            <Button 
              icon="plus"
              type="primary"
              onClick={()=>(
                history.push("/syster/user/add")
              )}
            >
              新建
            </Button>
          </span>
          <span style={{ float: 'right' }}>
            <Button
              icon="search"
              type="primary"
              htmlType="submit"
            >查詢</Button>
            <Button
              icon="sync"
              style={{ marginLeft: 8 }}
              onClick={this.handleFormReset.bind(this)}
            >重置</Button>
          </span>
        </div>
      </Form>
    );
  }

  // 查詢
  handleSearch(e){
    // 禁止默認行為
    e.preventDefault();
    // 獲取 form 表單的值
    console.log(this.props.form.getFieldsValue());
  }

  // 重置
  handleFormReset(){
    this.props.form.resetFields();
  }


  render(){

    return(
      <Card bordered={false}>
        <div className={styles.tableList}>
          <div className={styles.tableListForm}>
            {this.renderAdvancedForm()}
          </div>
        </div>
      </Card>
    )
  }
}

export default connect (({ user })=>(
  user
))(UserList)

2.樣式

@import "~antd/lib/style/themes/default.less";
@import "../../utils/utils.less";

.tableList {
  .tableListOperator {
    margin-bottom: 16px;
    button {
      margin-right: 8px;
    }
  }
}

.tableListForm {
  :global {
    .ant-form-item {
      margin-bottom: 24px !important;
      margin-right: 0;
      display: flex;
      > .ant-form-item-label {
        width: auto;
        line-height: 32px;
        padding-right: 8px;
      }
      .ant-form-item-control {
        line-height: 32px;
      }
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
  .submitButtons {
    white-space: nowrap;
    margin-bottom: 24px;
  }
}

@media screen and (max-width: @screen-lg) {
  .tableListForm :global(.ant-form-item) {
    margin-right: 24px;
  }
}

@media screen and (max-width: @screen-md) {
  .tableListForm :global(.ant-form-item) {
    margin-right: 8px;
  }
}

3.效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM