mtable.jsx 8.28 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
import React, { PureComponent } from "react";
import ProTable from "@ant-design/pro-table";
import request from "umi-request";
import Cascader from "./Cascader";
import { Tooltip } from "antd";

let defaultsize = localStorage.getItem("size"); //设置缓存

class Mtable extends PureComponent {
  state = {
    total: 0,
    size: defaultsize ? defaultsize : "small",
    valueEnum: {},
    columns: [],
  };

  actionRefs = React.createRef();
  formRefs = React.createRef();

  componentWillReceiveProps(nextprops) {
    if (
      JSON.stringify(this.props.extraparams) !=
        JSON.stringify(nextprops.extraparams) ||
      JSON.stringify(this.props.path) != JSON.stringify(nextprops.path)
    ) {
      if (this.props.actionRef) {
        this.props.actionRef?.current?.reload();
      } else {
        this.actionRefs?.current?.reload();
      }
    }
  }

  render() {
    let {
        pageSize,
        x,
        y,
        style,
        dataSource,
        onSearchChange,
        defaultPageSize,
        dispatch, //dispatch
        columns, //表头
        pagination, //分页
        rowKey, //行key
        savekey, //保存查询条件key
        actionRef, //操作ref
        formRef, //查询表单ref
        path, //路径
        extraparams = {}, //拓展查询参数
        rowSelection, //行选择
        tableRender, //表格布局自定义
        getDefaultSelected, //获取默认选中的数据
        rowClassNameFn, //选中后行样式
        showQuickJumper, //false字符串 不显示
        expandable, //是否可展开
        onExpand,
      } = this.props,
      { total, size } = this.state;

    let scroll = {};
    if (y) {
      scroll = {
        ...scroll,
        y,
      };
    }
    if (x) {
      scroll = {
        ...scroll,
        x,
      };
    }
    let tabledataProps =
      dataSource && Array.isArray(dataSource)
        ? {
            dataSource,
            pagination:
              pagination == "false"
                ? false
                : {
                    showTotal: (total, range) => <span>{total}</span>,
                    showQuickJumper: false,
                    defaultPageSize: defaultPageSize ? defaultPageSize : 15,
                    pageSizeOptions: [10, 15, 30, 50, 100, 200],
                    total: dataSource.length,
                  },
            search: {
              filterType: "light", //轻量模式
            },
            toolBarRender: false,
          }
        : {
            request: async (params = {}) => {
              //表格数据获取 只需要传入url自动获得数据
              if (!path) {
                return;
              }
              //设置token
              let token = localStorage.getItem("TOKENY")
                ? localStorage.getItem("TOKENY")
                : "b60ef0c40b7f1b7e63c2b24b127988d4";
              let headers = {
                "Content-Type": "application/json",
                token: token ? token : "",
              };
              //处理传参 extraparams为除列筛选外的自定义传参
              let newparams = {
                ...params,
                ...extraparams, //父组件传参
                pageIndex: params.current,
                pageSize: params.pageSize || pageSize,
              };
              delete newparams.current;
              if (pagination == "false") {
                delete newparams.pageIndex;
                delete newparams.pageSize;
              }

wuhao's avatar
wuhao committed
121
              let res = await request("/wms" + path, {
wuhao's avatar
wuhao committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
                body: JSON.stringify(newparams ? newparams : {}),
                headers,
                method: "POST",
              });
              if (res.code == "0000") {
                return {
                  data: res.data,
                  total: res?.data?.page?.total || res?.data?.dataList?.length,
                };
              } else {
                return {
                  data:
                    pagination == "false"
                      ? {
                          dataList: [],
                        }
                      : {
                          page: {
                            list: [],
                          },
                        },
                  total: 0,
                };
              }
            },
            pagination: {
              showTotal: (total, range) => <span>{total}</span>,
              showQuickJumper: !showQuickJumper ? true : false,
              pageSizeOptions: [5, 10, 15, 30, 50, 100, 200],
              defaultPageSize: pageSize || 15,
              total,
            },
            search: {
              filterType: "light", //轻量模式
            },
          };
    let columnes = columns.map((it) => {
      if (it.valueType == "cascader") {
        it.renderFormItem = (
          item,
          { type, defaultRender, fieldProps, ...rest },
          form
        ) => {
          let newoption = it.options || it.belinked.options;
          if (Array.isArray(newoption)) {
            newoption = { option: newoption };
          } else {
            let newparams = {};
            Object.keys(newoption.params).map((it) => {
              newparams[it] = form.getFieldValue(it);
            });
            newoption = {
              option: {
                ...newoption,
                params: newparams,
              },
            };
          }
          let reset = columns.filter((item) => {
            let newop = item?.options || item?.belinked?.options;
            return (
              newop?.params && newop.params[it?.formItemProps?.name] == "linked"
            );
          });
          let resetkey = reset.map((it) => it?.formItemProps?.name);
          return <Cascader {...newoption} form={form} resetkey={resetkey} />;
        };
      }
      if (it.valueType == "select" || it.valueType == "checkbox") {
        if (Array.isArray(it.options)) {
          // it.request = async function () {
          //   return it.options
          // }
          it.fieldProps = {
            options: [...it.options],
          };
        } else {
          it.request = async function () {
            let list = await it.options.database(it.options.params);
            return list.data.dataList;
          };
        }
      }
      return {
        ...it,
        ellipsis: true,
        render: it.render
          ? it.render
          : (text, row) => {
              return (
                <Tooltip title={row[it.dataIndex]} placement="topLeft">
                  <span>{row[it.dataIndex] ?? "-"}</span>
                </Tooltip>
              );
            },
      };
    });
    return (
      <ProTable
        size={size}
        onSubmit={(params) => {
          onSearchChange && onSearchChange(params);
        }}
        onSizeChange={(size) => {
          localStorage.setItem("size", size); //设置全局表格规格缓存
          this.setState({
            size,
          });
        }}
        columns={columnes}
        scroll={scroll}
        style={style || {}}
        actionRef={actionRef ? actionRef : this.actionRefs}
        formRef={formRef ? formRef : this.formRefs}
        rowKey={rowKey ? rowKey : "id"} //表格每行数据的key
        dateFormatter="string"
        postData={(data) => {
          if (data.page) {
            this.setState({
              total: data.page.total,
            });
          } else {
            this.setState({
              total: data.dataList.length,
            });
          }
          //分页or不分页获取数据
          getDefaultSelected && getDefaultSelected(data); //存在默认选中向上返回选中值
          let defaultval =
            pagination == "false"
              ? data.dataList
              : data.page
              ? data.page.list
              : []; //分页或不分页的接口返回数据
          return defaultval;
        }}
        rowSelection={rowSelection ? rowSelection : false}
        tableAlertRender={false} //取消顶部筛选操作
        tableRender={
          tableRender ? (_, dom) => tableRender(_, dom) : (_, dom) => dom
        } //自定义表格主体
        rowClassName={
          rowClassNameFn ? (record, index) => rowClassNameFn(record, index) : ""
        } //自定义行高亮
        {...tabledataProps}
        {...expandable}
        onExpand={onExpand ?? null}
      />
    );
  }
}

export default Mtable;