组件介绍

这儿主要介绍一些关键的组件,如果时间允许的情况下,我们会尽可能的介绍所有细节。

PageList

该组件组要分为两个部分:

  • 实例化后会设置分页、默认分页条数、表单所需要的初始值、跳转后获得数据的方法
  • PageList.Search为常用的搜索组件,可以设置默认值及跳转时约束其他参数关联关系,如:
  1. import React, { Component, Fragment } from "react";
  2. import PageHeaderWrapper from "@/components/pageHeaderWrapper";
  3. import { Card, Button, Table, Tag, Divider, Modal } from "antd";
  4. import styles from "./index.css";
  5. import router from "umi/router";
  6. import PageList from "@/components/pageList";
  7. import { connect } from "dva";
  8. // 连接express models(理解为数据池)
  9. @connect(({ express, loading }) => ({
  10. // 为了不报错,express.list.result,请在express models里定义好list :{ result : list:[],total_number:0} 类似这样的对象结构,以免在引入的时候未定义undefinded
  11. expressList: express.list.result,
  12. // express/list model请求完后台的加载状态
  13. expressListLoading: loading.effects["express/list"]
  14. }))
  15. class ExpressList extends Component {
  16. static defaultProps = {
  17. // 定义默认值
  18. expressListLoading: true,
  19. // 定义默认值,为了方便阅读,我们在这儿也定义了list初始值
  20. expressList: {
  21. list: [],
  22. total_number: 0
  23. }
  24. };
  25. // 实例化PageList,为了下文方便调用常用搜索方法,我们在这提前定义好
  26. search = new PageList({
  27. // 跳转、搜索提交的默认路由地址,记得'/'这个是根目录的意思,如果不加就是相对路径
  28. router: "/setting/deliver/express",
  29. // 每次请求的条数
  30. rows: 10,
  31. // 搜索框需要的参数及默认值
  32. param: {
  33. keywords_type: "company_name",
  34. keywords: null
  35. },
  36. // 搜索参数的依赖关系,为了过滤掉不必要不成立的参数,如下,意思为keywords_type依赖于keywords参数,如果keywords不成立那么keywords_type也不成立
  37. rule: [{ key: "keywords_type", rule: ["rely", "keywords"] }],
  38. // 当跳转后刷新的请求方法
  39. refresh: (e) => {
  40. this.initList(e);
  41. }
  42. });
  43. // 当组件加载完毕后,我们初始化数据,如果为了美观您也可以定义在refresh方法里,通过this.search.refresh进行调用,能省一个方法定义
  44. componentDidMount() {
  45. this.initList();
  46. }
  47. // 自定义的初始化列表数据方法
  48. initList = () => {
  49. const { dispatch } = this.props;
  50. dispatch({
  51. type: "express/list",
  52. payload: this.search.filter()
  53. });
  54. };
  55. // 默认的react渲染方法,这儿就不介绍了,react文档都有
  56. render() {
  57. // this.search里的getParam()方法可以获得所有参数的集合,用于下面给PageList.Search组件附初始值
  58. let { keywords_type, keywords } = this.search.getParam();
  59. // connect方法后会自动把express models里的数据池的数据仍给上面定义的键值里
  60. const { expressList, expressListLoading } = this.props;
  61. // 表格的栏目定义
  62. const columns = [
  63. {
  64. title: "物流公司名称",
  65. dataIndex: "company_name",
  66. // key一定要定义,否则渲染性能很慢
  67. key: "company_name"
  68. }, {
  69. title: "是否为常用",
  70. dataIndex: "is_commonly_use",
  71. key: "is_commonly_use",
  72. render: (value) => value === 1 ? <Tag color="red">常用</Tag> : "否"
  73. }, {
  74. title: "操作",
  75. dataIndex: "operating",
  76. key: "operating",
  77. // Fragment这个组件要注意了,react提供的空组件,否则就要用[<a>xx<a>,<a>yyy</a>]这样去解决
  78. render: (value, record) => <Fragment>
  79. {record.is_commonly_use !== 1 ? <Fragment><a
  80. onClick={() => {
  81. Modal.confirm({
  82. title: "确认设为常用?",
  83. okText: "确认",
  84. okType: "danger",
  85. cancelText: "取消",
  86. onOk: () => {
  87. this.props.dispatch({
  88. type: "express/setCommonlyUse",
  89. payload: {
  90. id: record.id
  91. },
  92. callback: () => {
  93. this.search.push();
  94. }
  95. });
  96. }
  97. });
  98. }}
  99. >设为常用</a><Divider type="vertical" /></Fragment> : null}
  100. {!record.is_system ? <Fragment><a
  101. onClick={() => {
  102. router.push({
  103. pathname: `/setting/deliver/express/edit`,
  104. search: `?id=${record.id}`
  105. });
  106. }}
  107. >
  108. 编辑
  109. </a><Divider type="vertical" /></Fragment> : null}
  110. {!record.is_system ? <a
  111. onClick={() => {
  112. Modal.confirm({
  113. title: "确认删除?",
  114. okText: "确认",
  115. okType: "danger",
  116. cancelText: "取消",
  117. onOk: () => {
  118. this.props.dispatch({
  119. type: "express/del",
  120. payload: {
  121. id: record.id
  122. },
  123. callback: () => {
  124. this.search.push();
  125. }
  126. });
  127. }
  128. });
  129. }}
  130. >删除</a> : null}
  131. </Fragment>
  132. }
  133. ];
  134. return (
  135. <PageHeaderWrapper hiddenBreadcrumb={true}>
  136. <Card bordered={false}>
  137. <PageList.Search
  138. loading={expressListLoading}
  139. onSubmit={this.search.submit}
  140. defaultValue={this.search.defaultParam}
  141. onReset={this.search.reset}
  142. // 这儿是封装好的常见搜索框元素,只需用这种数据结构去定义
  143. items={[
  144. {
  145. selectInput: [
  146. {
  147. // 参数名字,后端开发者理解为字段
  148. field: "keywords_type",
  149. // 样式
  150. style: { minWidth: 115 },
  151. // 初始值
  152. initialValue: keywords_type,
  153. // select的数据列表,格式请注意,为:[{key:any,value:any}]:array
  154. data: keywords_type_list
  155. },
  156. {
  157. field: "keywords",
  158. placeholder: "请输入关键词",
  159. initialValue: keywords
  160. }
  161. ]
  162. }
  163. ]} />
  164. <div className={styles.batchView}>
  165. <Button
  166. type='primary'
  167. onClick={() => {
  168. router.push("/setting/deliver/express/add");
  169. }}
  170. >
  171. 新增物流公司
  172. </Button>
  173. </div>
  174. <Table
  175. columns={columns}
  176. dataSource={expressList.list}
  177. loading={expressListLoading}
  178. rowKey={record => record.id}
  179. pagination={{
  180. showSizeChanger: false,
  181. showQuickJumper: false,
  182. // 从search里拿page,方便其他地方的自动维护
  183. current: this.search.page,
  184. // 从search里拿rows,方便其他地方的自动维护
  185. pageSize: this.search.rows,
  186. total: expressList.total_number
  187. }}
  188. onChange={({ current }) => {
  189. // 从search的setPage方法去设置page,并且刷新页面,push里主要是实现了refresh和路由跳转
  190. this.search.setPage(current).push();
  191. }}
  192. />
  193. </Card>
  194. </PageHeaderWrapper>
  195. );
  196. }
  197. }
  198. const keywords_type_list = [
  199. {
  200. name: "公司名称",
  201. value: "company_name"
  202. }
  203. ];
  204. // 注意在这儿导出是新的规范,不这么写,遇到babel7的时候装饰器语法会导致错误
  205. export default ExpressLis

PageList.Search

可使用内组件介绍

input

参数 类型 是否必填 说明
label 字段描述
input.field string 字段名称
input.placeholder string 占位文字
input.onChange function 当input发生改变时触发,闭包参数(value)
input.initialValue any 初始值
  1. const props = {
  2. label:"名称",
  3. input:{
  4. field: null,
  5. placeholder: null,
  6. onChange: (value) => {},
  7. initialValue: null
  8. }
  9. }

select

selectInput

timeRange

treeSelect