List.js 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
import React, { Component, Fragment } from 'react';
import SearchDom from '@/highOrderComponent/SearchDom';
import ButtonListDom from '../ButtonListDom';
import Shell from '@/baseComponent/Shell';
import StandardTable from '@/components/StandardTable';
import PropTypes from 'prop-types';
import { deepCopy } from '@/baseComponent/utils';
import { handleAudit } from '../publicApiService';
import { handleColumns } from '@/webPublic/FormInsertDiy/AffairPage/destruction';
王绍森's avatar
王绍森 committed
10 11

export default class List extends Component {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	constructor(props) {
		super(props);
		this.state = {
			list: [],
			selectRows: [],
			formValues: this.giveDefaultValue(props),
			loading: true,
			sortGetPageFields: {},
			pagination: {
				current: 1,
				total: 1,
				pageSize: this.props.listConfig.pageSize || 10,
				pageSizeOptions: ['10', '20', '50', '100', '500'],
				showQuickJumper: true,
				onShowSizeChange: (current, size) => {
					this.pageChange(current, size);
				},
				onChange: (current, size) => {
					this.pageChange(current, size);
				},
			},
		};
	}
王绍森's avatar
王绍森 committed
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	giveDefaultValue = (props) => {
		// 如果设置了 giveFieldsToFormValues 则将默认值赋值给formValues
		let formValues = {};
		if (
			props.pageSearch &&
			props.pageSearch.search &&
			props.pageSearch.search.giveFieldsToFormValues
		) {
			for (let x in props.pageSearch.search.field) {
				if (props.pageSearch.search.field[x].required === true) {
					formValues[x] = props.pageSearch.search.field[x].defaultValue;
				}
			}
		}
		return formValues;
	};
王绍森's avatar
王绍森 committed
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
	giveGetPageFields = () => {
		const { listConfig, pageSearch, beforeGetPage } = this.props;
		const { pagination, formValues } = this.state;
		const search = pageSearch.search;
		let data = deepCopy(formValues);
		/** 如果配置了默认值 则 加进去 **/
		for (let item in search.field) {
			let one = search.field[item];
			if (one.required && typeof data[item] === 'undefined') {
				/**
				 * 存在必填项 且当前必填项没有值时 则传入. 如果必填项有值则不需要修改
				 * */
				data[item] = one.defaultValue;
			}
		}
		if (listConfig.paging) {
			// 是否分页
			data.pageSize = pagination.pageSize;
			data.pageNo = pagination.current;
		}
		if (search.beforeGetPage) {
			data = search.beforeGetPage(data);
		}
		return data;
	};
王绍森's avatar
王绍森 committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	getPage = () => {
		const { listConfig, pageSearch, workId } = this.props;
		const { pagination, sortGetPageFields } = this.state;
		const { search } = pageSearch;
		let data = this.giveGetPageFields();
		if (!data) {
			return false;
		}
		if (search.handleSort) {
			data = {
				...data,
				...sortGetPageFields, // 增加排序的搜索条件
			};
		}
		this.setState({
			list: [],
			loading: true,
		});
王绍森's avatar
王绍森 committed
97

98 99 100
		if (search.beforeSearchData && typeof search.beforeSearchData === 'function') {
			data = search.beforeSearchData(data, { ...this.props });
		}
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
		search.getPageService({ ...data, appId: workId }).then((response) => {
			this.setState({
				loading: false,
			});
			if (
				!response ||
				(search.paging && typeof response.total === 'undefined') ||
				response.errMsg
			) {
				return false;
			}
			if (listConfig.paging) {
				pagination.total = Number(response.total);
			}
			if (typeof search.responseCallBack !== 'undefined') {
				response = search.responseCallBack(response);
			}
			this.setState({
				list: response.rows || response,
				selectRows: [],
				pagination: pagination,
			});
		});
	};
王绍森's avatar
王绍森 committed
126

127 128 129 130 131
	stateChange = (key, value) => {
		this.setState({
			[key]: value,
		});
	};
王绍森's avatar
王绍森 committed
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146
	pageChange = (current, size) => {
		this.setState(
			{
				pagination: {
					...this.state.pagination,
					current: current,
					pageSize: size,
				},
			},
			() => {
				this.getPage();
			},
		);
	};
王绍森's avatar
王绍森 committed
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
	formStateChange = (value, key) => {
		const { search } = this.props.pageSearch;
		const { condition } = search;
		let oldValue = this.state.formValues;
		oldValue[key] = value;
		/**
		 * 如果设置了beforeChange回调函数 则调用此回调函数改变另外的字段的值
		 * */
		const index = condition.findIndex((item) => {
			return item.key === key;
		});
		if (index !== -1) {
			const thisConfig = condition[index];
			if (thisConfig.beforeChange) {
				oldValue = thisConfig.beforeChange(oldValue, thisConfig);
			}
		}
		this.setState(
			{
				formValues: oldValue,
			},
			() => {
				if (search.afterFormValuesChange) {
					search.afterFormValuesChange(key, oldValue, this.getPage);
				}
			},
		);
	};
王绍森's avatar
王绍森 committed
176

177 178 179 180 181
	handleSelectRows = (rows) => {
		this.setState({
			selectRows: rows,
		});
	};
王绍森's avatar
王绍森 committed
182

183 184 185 186 187 188
	componentDidMount() {
		const { search } = this.props.pageSearch;
		if (!search.noNeedInitData) {
			this.getPage();
		}
	}
王绍森's avatar
王绍森 committed
189

190
	componentWillMount() {}
王绍森's avatar
王绍森 committed
191

192
	componentWillUnmount() {}
王绍森's avatar
王绍森 committed
193

194 195 196 197 198 199 200 201 202 203 204 205
	resetFormValues = () => {
		const { beforeResetFormValues } = this.props.pageSearch.search;
		let { formValues } = this.state;
		if (beforeResetFormValues) {
			formValues = beforeResetFormValues(formValues);
		} else {
			formValues = {};
		}
		this.setState({
			formValues,
		});
	};
王绍森's avatar
王绍森 committed
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	/**
	 * 处理排序
	 * */
	handleOnChange = (pagination, filters, sorter) => {
		const { pageSearch } = this.props;
		if (sorter) {
			const sortGetPageFields = pageSearch.search.handleSort(sorter);
			this.setState(
				{
					sortGetPageFields,
				},
				() => {
					this.getPage();
				},
			);
		}
	};
王绍森's avatar
王绍森 committed
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
	pageButton = () => {
		return [];
		const { list } = this.state;
		let re = {};
		let buttonConfig = [];
		for (let item of list) {
			if (item.btns && Array.isArray(item.btns)) {
				for (let x of item.btns) {
					if (typeof re[x.name] === 'undefined') {
						re[x.name] = x;
					}
				}
			}
		}
		for (let z in re) {
			let info = re[z];
			buttonConfig.push({
				type: z,
				name: z,
				component: 'ModalBatch',
				className: z !== '通过' ? 'defaultRed' : 'defaultBlue',
				nameSpan: { big: 4, small: 5 },
				fileSpan: { big: 1, small: 1 },
				values: {
					[info.key]: info.value,
				},
				fields: [
					{
						key: 'reason',
						name: '审核意见',
						type: 'textarea',
					},
				],
				beforeSubmit: (props, formValues) => {
					return {
						taskForm: {},
						examineMap: JSON.stringify(formValues),
					};
				},
				sourceKey: 'taskId',
				postKey: 'taskIds',
				apiServiceApi: handleAudit,
			});
		}
		return buttonConfig;
	};
王绍森's avatar
王绍森 committed
271

272 273 274 275 276
	render() {
		const { formValues, selectRows, list, pagination, loading } = this.state;
		const { listConfig, pageSearch, pageButton, children, addFields } = this.props;
		let { columns, search } = pageSearch;
		columns = handleColumns(columns);
王绍森's avatar
王绍森 committed
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		let data = list;
		for (let item of addFields) {
			if (item.type === 'tableSelect' && item.c1) {
				for (let i = 0; i < data.length; i++) {
					let nameKey = item.componentProps.labelName;
					if (data[i][item.c1] && data[i][item.c1].selects) {
						let res = [];
						for (let x in data[i][item.c1].selects) {
							res.push(data[i][item.c1].selects[x][nameKey]);
						}
						data[i][item.c1] = res.join(',');
					}
				}
			}
		}
王绍森's avatar
王绍森 committed
293

294 295 296 297 298 299 300 301 302
		const tableProps = {
			rowKey: pageSearch.tableRowKey || 'id',
			selectedRows: selectRows,
			data: { list: data, pagination },
			columns,
			bordered: pageSearch.bordered || false,
			loading,
			noSelectRow: !listConfig.selectRows,
			onSelectRow: this.handleSelectRows,
303
			scroll: pageSearch.scroll || { x: true },
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		};
		if (!listConfig.paging) {
			tableProps.data = { list: data, pagination: false };
		}
		if (search && search.handleSort) {
			tableProps.onChange = this.handleOnChange;
		}
		return (
			<Fragment>
				{listConfig.searchArea && this.props.searchCondition?.length ? (
					<SearchDom
						formStateChange={this.formStateChange}
						formValues={formValues}
						getPage={this.getPage}
						resetFormValues={this.resetFormValues}
						config={pageSearch.search}
					/>
				) : null}
王绍森's avatar
王绍森 committed
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
				<Shell>
					{pageButton.length ? (
						<ButtonListDom
							config={pageButton}
							handleSelectRows={this.handleSelectRows}
							selectRows={selectRows}
							formValues={formValues}
							listData={list}
							search={pageSearch.search}
							getPage={this.getPage}
							children={children}
						/>
					) : null}
					<StandardTable {...tableProps} />
				</Shell>
			</Fragment>
		);
	}
王绍森's avatar
王绍森 committed
341 342 343
}

List.propTypes = {
344 345 346
	listConfig: PropTypes.object.isRequired,
	// pageButton: PropTypes.array,
	pageSearch: PropTypes.object.isRequired,
王绍森's avatar
王绍森 committed
347 348 349
};

List.defaultProps = {
350 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 384 385
	listConfig: {
		selectRows: true, // 是否可以行选择,
		paging: true, // 是否可以分页,
		searchArea: true, // 是否拥有 搜索区dom,
		buttonArea: true, // 是否拥有 按钮区,
	},
	pageSearch: {
		search: {
			url: '',
			field: {},
			responseCallBack: (response) => {
				return response;
			},
			condition: [
				{
					key: 'studentNo',
					name: '学号',
					type: 'input',
				},
			],
			tableRowKey: 'id',
			columns: [
				{
					title: '姓名',
					dataIndex: '',
				},
				{
					title: '操作',
					dataIndex: 'operation',
					renderType: 'update', // 修改
				},
			],
			nameSpan: { big: 8, small: 9 },
			fileSpan: { big: 4, small: 4 },
		},
	},
王绍森's avatar
王绍森 committed
386
};