TableSelect.js 10.2 KB
Newer Older
1 2 3 4
import React from 'react';
import { Button, message, Table, Tag } from 'antd';
import FormList from './formList/index';
import { connect } from 'dva';
5
import { getModal } from '@/webPublic/one_stop_public/utils/utils';
钟是志's avatar
钟是志 committed
6
import TableSelectNormal from '@/webPublic/one_stop_public/libs/Split/TableSelectNormal';
钟是志's avatar
钟是志 committed
7
import styles from './Split/styles.less';
8

9
const Modal = getModal();
10

钟是志's avatar
钟是志 committed
11

12 13 14 15 16 17 18 19
@connect(({
            DataColumn,
            SqlManageEntity,
            loading
          }) => ({
  DataColumn,
  SqlManageEntity,
  loading: loading.models.DataColumn || loading.models.SqlManageEntity,
徐立's avatar
徐立 committed
20 21
}))
export default class TableSelect extends React.Component {
22 23 24 25
  constructor(props) {
    super(props);
    const value = props.value || {};
    this.otherProps = {};
钟是志's avatar
钟是志 committed
26 27 28 29
    if (props.json.otherProps && props.json.otherProps.length) {
      try {
        this.otherProps = new Function(props.json.otherProps)();
      } catch (e) {
30
        console.log('TableSelect组件 otherProps 获取失败');
钟是志's avatar
钟是志 committed
31 32
      }
    }
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    this.state = {
      selects: value.selects || {},
      visiable: value.visiable,
      isAll: value.isAll != null ? value.isAll : false,
      querys: value.querys || [],
      sql: value.sql,
    };
  }

  triggerChange = (changedValue) => {
    // Should provide an event to pass value to Form.
    const onChange = this.props.onChange;
    if (onChange) {
      // console.log('changedValue', changedValue);
      onChange(Object.assign({}, this.state, changedValue));
    }
  };
50

51 52 53 54
  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
      const value = nextProps.value;
55

56 57 58 59 60 61
      if (value != null && value instanceof Object) {
        this.setState(value);
      }
      //
    }
  }
62

63 64 65 66 67 68 69 70
  remove = (rowKey) => {
    const { selects } = this.state;
    delete selects[rowKey];
    if (!('value' in this.props)) {
      this.setState({ selects });
    }
    this.triggerChange({ selects });
  };
71

72 73 74 75 76 77 78 79 80
  select = (obj) => {
    var { selects } = this.state;
    const {
      json: {
        isMultiple,
        optionType
      },
    } = this.props;
    let valueName = this.props.json.valueName;
81

82 83 84
    if (optionType == 'reference' && this.props.dataColumn.referenceObjId) {
      valueName = this.props.dataColumn.referenceCodeName;
    }
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 121 122 123 124 125 126 127 128 129 130 131 132 133
    const kk = obj[valueName];
    if (kk == null) {
      message.error('指定的取值字段不存在或者值为空');
      return;
    }
    if (!isMultiple) {
      selects = {
        [kk]: {
          ...obj,
          selectKey: kk
        }
      };
    } else {
      selects[kk] = obj;
      selects[kk].selectKey = kk;
    }
    if (!('value' in this.props)) {
      this.setState({ selects });
    }
    this.triggerChange({ selects });
  };

  selectAll = () => {
    let { selects } = this.state;
    const {
      dataSource,
      json
    } = this.props;
    let {
      valueName,
      optionType
    } = json;
    const { list } = dataSource;
    if (optionType == 'reference' && this.props.dataColumn.referenceObjId) {
      valueName = this.props.dataColumn.referenceCodeName;
    }
    if (list && Array.isArray(list)) {
      for (let item of list) {
        let kk = item[valueName];
        selects[kk] = item;
        selects[kk].selectKey = kk;
      }
    }
    if (!('value' in this.props)) {
      this.setState({ selects });
    }
    this.triggerChange({ selects });
  };
134

135 136 137 138 139 140 141
  cancelAll = () => {
    let selects = {};
    if (!('value' in this.props)) {
      this.setState({ selects });
    }
    this.triggerChange({ selects });
  };
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  close = (i) => {
    var {
      values,
      labels
    } = this.state;
    values.splice(i, 1);
    labels.splice(i, 1);
    if (!('value' in this.props)) {
      this.setState({
        values,
        labels
      });
    }
    this.triggerChange({
      values,
      labels
    });
  };
161

162 163 164 165 166 167
  open = () => {
    if (!('value' in this.props)) {
      this.setState({ visiable: true });
    }
    this.triggerChange({ visiable: true });
  };
168

169 170 171 172 173 174 175 176
  closeModal = () => {
    if (!('value' in this.props)) {
      this.setState({ visiable: false });
    }
    const { resetSearchText } = this.props;
    resetSearchText && resetSearchText();
    this.triggerChange({ visiable: false });
  };
177

178 179 180 181 182 183 184 185 186
  cancelOne = (record) => {
    var { selects } = this.state;
    const {
      json: {
        isMultiple,
        optionType
      },
    } = this.props;
    let valueName = this.props.json.valueName;
187

188 189 190
    if (optionType == 'reference' && this.props.dataColumn.referenceObjId) {
      valueName = this.props.dataColumn.referenceCodeName;
    }
191

192 193 194 195 196 197 198 199 200 201 202 203 204
    const kk = record[valueName];
    if (kk == null) {
      message.error('指定的取值字段不存在或者值为空');
      return;
    }
    if (selects[kk]) {
      delete selects[kk];
    }
    if (!('value' in this.props)) {
      this.setState({ selects });
    }
    this.triggerChange({ selects });
  };
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219
  render() {
    const {
      json: {
        isMultiple,
        sql,
        optionType,
        showTable,
        isShowQuanXuan
      },
      dataColumn: { referenceObjId },
      sqlModel,
      columns,
      dataSource,
    } = this.props;
220
    let disabled = this.props.disabled || this.props.json.disabled; // 禅道 28713
221
    const { cardSelectModalProps = {} } = this.otherProps;
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
    let {
      valueName,
      labelName
    } = this.props.json;
    if (optionType == 'reference' && referenceObjId) {
      valueName = this.props.dataColumn.referenceCodeName;
      labelName = this.props.dataColumn.referenceNameName;
    }
    if (valueName == null || valueName == '') {
      return <>请指定取值的列名</>;
    }
    const {
      selects,
      visiable,
      labels
    } = this.state;
    const keys = [];
    for (let i in selects) {
      keys.push(selects[i][valueName]);
    }
    const columns2 = [
      ...columns,
    ];
    if (!disabled) {
      columns2.push({
        title: '操作',
        render: (val, record) => <a onClick={this.remove.bind(this, record.selectKey)}>移除</a>,
      },);
    }
    const columns3 = [
      ...columns,
    ];
    if (!disabled) {
      columns3.push( {
        title: '操作',
        render: (val, record) =>
          keys.includes(record[valueName]) ? (
            <a
              onClick={() => {
                this.cancelOne(record);
              }}>
              已选择
            </a>
          ) : (
            <a onClick={this.select.bind(this, record)}>选择</a>
          ),
      },);
    }
271

272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    const custom = {
      isAll: this.state.isAll,
      querys: this.state.querys,
      sql: this.state.sql,
    };
    let allWidth = 0;
    if (columns3) {
      columns3.map((item, index) => {
        if (index === columns3.length - 1) {
          item.fixed = 'right';
          item.width = 90;
          allWidth += 90;
          return item;
        }
        if (index === columns3.length - 2) {
          allWidth += 150;
        } else {
          if (item.width) {
            allWidth += item.width;
          } else {
            item.width = 180;
            allWidth += 180;
          }
        }
        return item;
      });
    }
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    return (
      <div>
        {Object.keys(selects).length > 0 ? ( // 是否显示表格
          showTable ? (
            <Table
              rowKey={valueName}
              size="small"
              dataSource={Object.values(selects)}
              columns={columns2}
              pagination={false}
            />
          ) : (
            Object.keys(selects)
              .map((k) => (
                <Tag key={k} closable={!disabled} onClose={this.remove.bind(this, k)}>
                  {selects[k][labelName]}
                </Tag>
              ))
          )
        ) : (
          ''
        )}
        {!disabled && (
          <Button type="primary" onClick={this.open}>
            选择
          </Button>
        )}
328

329 330
        {visiable && (
          <Modal
钟是志's avatar
钟是志 committed
331
            className={styles.ModalDiy}
332 333 334
            width={this.props.get === 'mobile' ? '100%' : '60%'}
            maskClosable={false}
            title={`请选择`}
335 336 337 338 339
            visible={true}
            bodyStyle={{
              maxHeight: '60vh',
              overflowY: 'auto',
            }}
钟是志's avatar
钟是志 committed
340 341 342 343 344 345 346 347
            // footer={[
            //   <Button key="back"
            //           onClick={this.closeModal}
            //           type={'primary'}
            //   >
            //     确定
            //   </Button>,
            // ]}
348 349
            onOk={this.closeModal}
            onCancel={this.closeModal}
钟是志's avatar
钟是志 committed
350
            {...cardSelectModalProps}
钟是志's avatar
钟是志 committed
351
          >
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
            {optionType === 'reference' ? (
              referenceObjId ? (
                <FormList
                  rights={this.props.rights || []}
                  notShowBack={true}
                  value={custom}
                  objId={referenceObjId}
                  isFormCom={true}
                  isSelect={true}
                  selects={keys}
                  valueName={valueName}
                  callback={this.select}
                  sql={sql}
                />
              ) : (
                '未关联数据对象'
              )
            ) : sqlModel.dataObjId ? (
              <FormList
                rights={this.props.rights || []}
                notShowBack={true}
                value={custom}
                objId={sqlModel.dataObjId}
                isFormCom={true}
                isSelect={true}
                selects={keys}
                valueName={valueName}
                callback={this.select}
                sql={sql}
              />
            ) : (
              <TableSelectNormal isMultiple={isMultiple}
钟是志's avatar
钟是志 committed
384 385 386 387 388 389 390
                                 allWidth={allWidth}
                                 cancelAll={this.cancelAll}
                                 selectAll={this.selectAll}
                                 isShowQuanXuan={isShowQuanXuan}
                                 valueName={valueName}
                                 dataSource={dataSource}
                                 columns3={columns3}
钟是志's avatar
钟是志 committed
391
                                 otherProps={this.otherProps}
钟是志's avatar
钟是志 committed
392 393 394
                                 get={this.props.get}
                                 selects={this.state.selects}
                                 callback={this.select}
395

钟是志's avatar
钟是志 committed
396
              />
397 398 399 400 401 402
            )}
          </Modal>
        )}
      </div>
    );
  }
403
}