mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:48:55 +08:00
fix: Table2事件动作调整
This commit is contained in:
parent
289c2ea91d
commit
021d033b06
@ -191,7 +191,7 @@ order: 67
|
||||
"rowSelection": {
|
||||
"type": "checkbox",
|
||||
"keyField": "id",
|
||||
"selectedRowKeysExpr": "<%= data.record.id === 1 %>"
|
||||
"selectedRowKeysExpr": "data.record.id === 1"
|
||||
},
|
||||
"columns": [
|
||||
{
|
||||
@ -498,7 +498,7 @@ order: 67
|
||||
"expandableOn": "this.record.id === 1 || this.record.id === 3",
|
||||
"keyField": "id",
|
||||
"expandedRowClassNameExpr": "<%= data.rowIndex % 2 ? 'bg-success' : '' %>",
|
||||
"expandedRowKeysExpr": "<%= data.record.id == '3' %>"
|
||||
"expandedRowKeysExpr": "data.record.id == '3'"
|
||||
},
|
||||
"expandableBody": [
|
||||
{
|
||||
@ -809,10 +809,11 @@ order: 67
|
||||
"type": "table-v2",
|
||||
"source": "$rows",
|
||||
"draggable": true,
|
||||
"keyField": "id",
|
||||
"columns": [
|
||||
{
|
||||
"title": "Engine",
|
||||
"key": "engine",
|
||||
"title": "ID",
|
||||
"key": "id"
|
||||
},
|
||||
{
|
||||
"title": "Grade",
|
||||
@ -827,8 +828,8 @@ order: 67
|
||||
"key": "badgeText",
|
||||
"children": [
|
||||
{
|
||||
"title": "ID",
|
||||
"key": "id"
|
||||
"title": "Engine",
|
||||
"key": "engine",
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -180,7 +180,10 @@ export class HeadCellFilter extends React.Component<Props, State> {
|
||||
const payload = {[column.key]: selectedKeys};
|
||||
|
||||
if (onFilter) {
|
||||
const prevented = await onFilter(payload);
|
||||
const prevented = await onFilter({
|
||||
filterName: column.key,
|
||||
filterValue: selectedKeys?.join(',')
|
||||
});
|
||||
if (prevented) {
|
||||
return;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ export class HeadCellSort extends React.Component<Props, State> {
|
||||
}
|
||||
|
||||
if (onSort) {
|
||||
const prevented = onSort({
|
||||
const prevented = await onSort({
|
||||
orderBy: this.state.orderBy,
|
||||
order: this.state.order
|
||||
});
|
||||
|
@ -10,7 +10,6 @@ import find from 'lodash/find';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import filter from 'lodash/filter';
|
||||
import intersection from 'lodash/intersection';
|
||||
import cloneDeep from 'lodash/cloneDeep';
|
||||
import Sortable from 'sortablejs';
|
||||
|
||||
import {themeable, ClassNamesFn, ThemeProps} from '../../theme';
|
||||
@ -325,12 +324,15 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
constructor(props: TableProps) {
|
||||
super(props);
|
||||
|
||||
this.selectedRows = props.rowSelection
|
||||
? this.getSelectedRows(
|
||||
props.dataSource,
|
||||
props.rowSelection?.selectedRowKeys
|
||||
)
|
||||
: [];
|
||||
if (props.rowSelection) {
|
||||
const selectedResult = this.getSelectedRows(
|
||||
props.dataSource,
|
||||
props.rowSelection?.selectedRowKeys
|
||||
);
|
||||
|
||||
this.selectedRows = selectedResult.selectedRows;
|
||||
this.unSelectedRows = selectedResult.unSelectedRows;
|
||||
}
|
||||
|
||||
this.state = {
|
||||
selectedRowKeys: props.rowSelection
|
||||
@ -361,6 +363,8 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
tdColumns: Array<TdProps>;
|
||||
// 表格当前选中行
|
||||
selectedRows: Array<any>;
|
||||
// 表格当前未选中行
|
||||
unSelectedRows: Array<any>;
|
||||
// 拖拽排序
|
||||
sortable: Sortable;
|
||||
// 记录点击起始横坐标
|
||||
@ -393,6 +397,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
selectedRowKeys: Array<string | number>
|
||||
) {
|
||||
const selectedRows: Array<any> = [];
|
||||
const unSelectedRows: Array<any> = [];
|
||||
dataSource.forEach(data => {
|
||||
if (
|
||||
find(
|
||||
@ -401,10 +406,12 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
)
|
||||
) {
|
||||
selectedRows.push(data);
|
||||
} else {
|
||||
unSelectedRows.push(data);
|
||||
}
|
||||
});
|
||||
|
||||
return selectedRows;
|
||||
return {selectedRows, unSelectedRows};
|
||||
}
|
||||
|
||||
updateTableBodyFixed() {
|
||||
@ -498,10 +505,12 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
// 选择项发生了变化触发
|
||||
if (!isEqual(prevState.selectedRowKeys, this.state.selectedRowKeys)) {
|
||||
// 更新保存的已选择行数据
|
||||
this.selectedRows = this.getSelectedRows(
|
||||
const selectedResult = this.getSelectedRows(
|
||||
this.state.dataSource,
|
||||
this.state.selectedRowKeys
|
||||
);
|
||||
this.selectedRows = selectedResult.selectedRows;
|
||||
this.unSelectedRows = selectedResult.unSelectedRows;
|
||||
|
||||
const {rowSelection} = this.props;
|
||||
rowSelection &&
|
||||
@ -526,10 +535,12 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
this.setState({
|
||||
selectedRowKeys: this.props.rowSelection.selectedRowKeys
|
||||
});
|
||||
this.selectedRows = this.getSelectedRows(
|
||||
const selectedResult = this.getSelectedRows(
|
||||
this.state.dataSource,
|
||||
this.state.selectedRowKeys
|
||||
);
|
||||
this.selectedRows = selectedResult.selectedRows;
|
||||
this.unSelectedRows = selectedResult.unSelectedRows;
|
||||
}
|
||||
}
|
||||
|
||||
@ -599,37 +610,6 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
this.destroyDragging();
|
||||
}
|
||||
|
||||
exchange(fromIndex: number, toIndex: number, item: any) {
|
||||
const {scroll, headSummary} = this.props;
|
||||
// 如果有头部总结行 fromIndex就会+1
|
||||
if ((!scroll || (scroll && !scroll.y)) && headSummary) {
|
||||
fromIndex = fromIndex - 1;
|
||||
}
|
||||
|
||||
const index = toIndex - fromIndex;
|
||||
|
||||
const levels = item.getAttribute('row-levels');
|
||||
const rowIndex = +item.getAttribute('row-index');
|
||||
|
||||
const dataSource = cloneDeep(this.state.dataSource);
|
||||
const levelsArray = levels ? levels.split(',') : [];
|
||||
const childrenColumnName = this.getChildrenColumnName();
|
||||
let data: Array<any> = dataSource;
|
||||
let i = 0;
|
||||
while (i < levelsArray.length) {
|
||||
data = data[levelsArray[i]][childrenColumnName];
|
||||
i++;
|
||||
}
|
||||
|
||||
if (data && data.length > 0) {
|
||||
const row = cloneDeep(data[rowIndex]);
|
||||
data.splice(rowIndex, 1);
|
||||
data.splice(rowIndex + index, 0, row);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
initDragging() {
|
||||
const {classnames: cx, onDrag} = this.props;
|
||||
|
||||
@ -666,14 +646,9 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
return;
|
||||
}
|
||||
|
||||
const {onDrag} = this.props;
|
||||
if (onDrag) {
|
||||
const data = this.exchange(e.oldIndex, e.newIndex, e.item);
|
||||
const prevented = await onDrag(data);
|
||||
if (prevented) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
const rowLevels = e.item.getAttribute('row-levels');
|
||||
|
||||
onDrag && onDrag(e.oldIndex, e.newIndex, rowLevels ? rowLevels.split(',') : []);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1037,9 +1012,8 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
const selectedRowKeys = value ? allRowKeys : [];
|
||||
if (onSelectAll) {
|
||||
const prevented = await onSelectAll(
|
||||
value,
|
||||
selectedRowKeys,
|
||||
selectedRows,
|
||||
value ? [] : selectedRows,
|
||||
changeRows
|
||||
);
|
||||
if (prevented) {
|
||||
@ -1152,9 +1126,16 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
);
|
||||
}
|
||||
|
||||
onRowClick(event: React.ChangeEvent<any>, record?: any, rowIndex?: number) {
|
||||
async onRowClick(event: React.ChangeEvent<any>, record?: any, rowIndex?: number) {
|
||||
const {rowSelection, onRow} = this.props;
|
||||
|
||||
if (onRow && onRow.onRowClick) {
|
||||
const prevented = await onRow.onRowClick(event, record, rowIndex);
|
||||
if (prevented) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (rowSelection && rowSelection.type && rowSelection.rowClick) {
|
||||
const defaultKey = this.getRowSelectionKeyField();
|
||||
|
||||
@ -1165,10 +1146,6 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
|
||||
this.selectedSingleRow(!isSelected, record);
|
||||
}
|
||||
|
||||
if (record && onRow) {
|
||||
onRow.onRowClick && onRow.onRowClick(event, record, rowIndex);
|
||||
}
|
||||
}
|
||||
|
||||
onRowMouseEnter(
|
||||
@ -1350,42 +1327,41 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
const defaultKey = this.getRowSelectionKeyField();
|
||||
const isRadio = rowSelection && rowSelection.type === 'radio';
|
||||
|
||||
let selectedRowKeys = [];
|
||||
if (value) {
|
||||
if (isRadio) {
|
||||
selectedRowKeys = [data[defaultKey]];
|
||||
} else {
|
||||
selectedRowKeys = [
|
||||
...this.state.selectedRowKeys,
|
||||
data[defaultKey],
|
||||
...this.getDataChildrenKeys(data)
|
||||
].filter((key, i, a) => a.indexOf(key) === i)
|
||||
}
|
||||
} else {
|
||||
if (!isRadio) {
|
||||
selectedRowKeys = this.state.selectedRowKeys.filter(
|
||||
key =>
|
||||
![data[defaultKey], ...this.getDataChildrenKeys(data)].includes(
|
||||
key
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (onSelect) {
|
||||
const selectedResult = this.getSelectedRows(this.state.dataSource, selectedRowKeys);
|
||||
const prevented = await onSelect(
|
||||
data,
|
||||
value,
|
||||
this.selectedRows,
|
||||
this.state.selectedRowKeys
|
||||
selectedResult.selectedRows,
|
||||
selectedRowKeys,
|
||||
selectedResult.unSelectedRows
|
||||
);
|
||||
if (prevented) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (value) {
|
||||
if (isRadio) {
|
||||
this.setState({selectedRowKeys: [data[defaultKey]]});
|
||||
} else {
|
||||
this.setState(prevState => ({
|
||||
selectedRowKeys: [
|
||||
...prevState.selectedRowKeys,
|
||||
data[defaultKey],
|
||||
...this.getDataChildrenKeys(data)
|
||||
].filter((key, i, a) => a.indexOf(key) === i)
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
if (!isRadio) {
|
||||
this.setState({
|
||||
selectedRowKeys: this.state.selectedRowKeys.filter(
|
||||
key =>
|
||||
![data[defaultKey], ...this.getDataChildrenKeys(data)].includes(
|
||||
key
|
||||
)
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
this.setState({selectedRowKeys});
|
||||
}
|
||||
|
||||
renderRow(data: any, rowIndex: number, levels: Array<number>) {
|
||||
@ -1396,6 +1372,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
draggable,
|
||||
indentSize,
|
||||
rowClassName,
|
||||
keyField,
|
||||
lineHeight // 是否设置了固定行高
|
||||
} = this.props;
|
||||
|
||||
@ -1515,10 +1492,11 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
key => key === data[defaultKey]
|
||||
);
|
||||
const hasChildrenChecked = this.hasCheckedChildrenRows(data);
|
||||
const isRadio = rowSelection && rowSelection.type === 'radio';
|
||||
|
||||
return [
|
||||
<tr
|
||||
key={rowIndex}
|
||||
key={`${data[keyField || 'key'] || rowIndex}`} // 可能会拖拽排序,就不能用rowIndex作为key了,否则显示会有问题
|
||||
row-index={rowIndex}
|
||||
row-levels={levels.join(',')}
|
||||
className={cx(
|
||||
@ -1546,8 +1524,8 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
<CheckBox
|
||||
name={'Table-checkbox'}
|
||||
type={rowSelection.type || 'checkbox'}
|
||||
partial={hasChildrenChecked && !isChecked}
|
||||
checked={hasChildrenChecked || isChecked}
|
||||
partial={!isRadio && hasChildrenChecked && !isChecked}
|
||||
checked={isRadio ? isChecked : (hasChildrenChecked || isChecked)}
|
||||
onChange={(value, shift) => {
|
||||
if (!(rowSelection && rowSelection.rowClick)) {
|
||||
this.selectedSingleRow(value, data);
|
||||
@ -1603,9 +1581,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
|
||||
</Cell>
|
||||
</tr>
|
||||
) : (
|
||||
this.state.dataSource.map((data, index) => {
|
||||
return this.renderRow(data, index, []);
|
||||
})
|
||||
this.state.dataSource.map((data, index) => this.renderRow(data, index, []))
|
||||
)}
|
||||
</tbody>
|
||||
);
|
||||
|
@ -143,8 +143,8 @@ export class HeadCellSearchDropDown extends React.Component<
|
||||
return schema || 'error';
|
||||
}
|
||||
|
||||
handleAction(e: any, action: Action, ctx: object, confirm: Function) {
|
||||
const {onAction} = this.props;
|
||||
async handleAction(e: any, action: Action, ctx: object, confirm: Function) {
|
||||
const {onAction, data, dispatchEvent, name} = this.props;
|
||||
|
||||
if (action.actionType === 'cancel' || action.actionType === 'close') {
|
||||
confirm();
|
||||
@ -157,17 +157,14 @@ export class HeadCellSearchDropDown extends React.Component<
|
||||
return;
|
||||
}
|
||||
|
||||
onAction && onAction(e, action, ctx);
|
||||
}
|
||||
|
||||
async handleReset() {
|
||||
const {onSearch, data, name, store, dispatchEvent} = this.props;
|
||||
const values = {...data};
|
||||
this.formItems.forEach(key => setVariable(values, key, undefined));
|
||||
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'columnSearch',
|
||||
createObject(data, {
|
||||
...values
|
||||
searchName: name,
|
||||
searchValue: values
|
||||
})
|
||||
);
|
||||
|
||||
@ -175,6 +172,13 @@ export class HeadCellSearchDropDown extends React.Component<
|
||||
return;
|
||||
}
|
||||
|
||||
onAction && onAction(e, action, ctx);
|
||||
}
|
||||
|
||||
async handleReset() {
|
||||
const {onSearch, data, name, store} = this.props;
|
||||
const values = {...data};
|
||||
|
||||
this.formItems.forEach(key => setVariable(values, key, undefined));
|
||||
|
||||
if (values.orderBy === name) {
|
||||
@ -200,7 +204,8 @@ export class HeadCellSearchDropDown extends React.Component<
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'columnSearch',
|
||||
createObject(data, {
|
||||
...values
|
||||
searchName: name,
|
||||
searchValue: values
|
||||
})
|
||||
);
|
||||
|
||||
|
@ -25,6 +25,7 @@ import {evalExpression, filter} from '../../utils/tpl';
|
||||
import {isEffectiveApi} from '../../utils/api';
|
||||
import Checkbox from '../../components/Checkbox';
|
||||
import {BadgeSchema} from '../../components/Badge';
|
||||
import {Icon} from '../../components/icons';
|
||||
import {TableStoreV2, ITableStore, IColumn, IRow} from '../../store/table-v2';
|
||||
|
||||
import {HeadCellSearchDropDown} from './HeadCellSearchDropdown';
|
||||
@ -383,10 +384,8 @@ export default class TableRenderer extends React.Component<
|
||||
|
||||
onSelect &&
|
||||
onSelect(
|
||||
null,
|
||||
false,
|
||||
store.selectedRowKeys.map(item => item),
|
||||
store.selectedRows.map(item => item.data)
|
||||
store.selectedRows.map(item => item.data),
|
||||
store.unSelectedRows.map(item => item.data)
|
||||
);
|
||||
}
|
||||
|
||||
@ -431,11 +430,11 @@ export default class TableRenderer extends React.Component<
|
||||
// selectedRowKeysExpr比selectedRowKeys优先级高
|
||||
if (props.rowSelection && props.rowSelection.selectedRowKeysExpr) {
|
||||
rows.forEach((row: any, index: number) => {
|
||||
const flag = filter(props.rowSelection.selectedRowKeysExpr, {
|
||||
const flag = evalExpression(props.rowSelection.selectedRowKeysExpr, {
|
||||
record: row,
|
||||
rowIndex: index
|
||||
});
|
||||
if (flag === 'true') {
|
||||
if (flag) {
|
||||
selectedRowKeys.push(row[props?.rowSelection?.keyField || 'key']);
|
||||
}
|
||||
});
|
||||
@ -450,11 +449,11 @@ export default class TableRenderer extends React.Component<
|
||||
let expandedRowKeys: Array<string | number> = [];
|
||||
if (props.expandable && props.expandable.expandedRowKeysExpr) {
|
||||
rows.forEach((row: any, index: number) => {
|
||||
const flag = filter(props.expandable.expandedRowKeysExpr, {
|
||||
const flag = evalExpression(props.expandable.expandedRowKeysExpr, {
|
||||
record: row,
|
||||
rowIndex: index
|
||||
});
|
||||
if (flag === 'true') {
|
||||
if (flag) {
|
||||
expandedRowKeys.push(row[props?.expandable?.keyField || 'key']);
|
||||
}
|
||||
});
|
||||
@ -1019,19 +1018,16 @@ export default class TableRenderer extends React.Component<
|
||||
|
||||
@autobind
|
||||
async handleSelected(
|
||||
record: any,
|
||||
value: boolean,
|
||||
selectedRows: Array<any>,
|
||||
selectedRowKeys: Array<string | number>
|
||||
selectedRowKeys: Array<string | number>,
|
||||
unSelectedRows: Array<string | number>
|
||||
) {
|
||||
const {dispatchEvent, data, rowSelection, onSelect, store} = this.props;
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'selected',
|
||||
'selectedChange',
|
||||
createObject(data, {
|
||||
record,
|
||||
value,
|
||||
selectedRows,
|
||||
selectedRowKeys
|
||||
selectedItems: selectedRows,
|
||||
unSelectedItems: unSelectedRows
|
||||
})
|
||||
);
|
||||
|
||||
@ -1040,34 +1036,7 @@ export default class TableRenderer extends React.Component<
|
||||
}
|
||||
|
||||
store.updateSelected(selectedRowKeys, rowSelection.keyField);
|
||||
onSelect && onSelect(record, value, selectedRows, selectedRowKeys);
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleSelectedAll(
|
||||
value: boolean,
|
||||
selectedRowKeys: Array<string | number>,
|
||||
selectedRows: Array<any>,
|
||||
changeRows: Array<any>
|
||||
) {
|
||||
const {dispatchEvent, data, rowSelection, onSelectAll, store} = this.props;
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'selectedAll',
|
||||
createObject(data, {
|
||||
value,
|
||||
selectedRowKeys,
|
||||
selectedRows,
|
||||
changeRows
|
||||
})
|
||||
);
|
||||
|
||||
if (rendererEvent?.prevented) {
|
||||
return rendererEvent?.prevented;
|
||||
}
|
||||
|
||||
store.updateSelected(selectedRowKeys, rowSelection.keyField);
|
||||
onSelectAll &&
|
||||
onSelectAll(value, selectedRowKeys, selectedRows, selectedRowKeys);
|
||||
onSelect && onSelect(selectedRows, unSelectedRows);
|
||||
}
|
||||
|
||||
@autobind
|
||||
@ -1076,7 +1045,8 @@ export default class TableRenderer extends React.Component<
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'columnSort',
|
||||
createObject(data, {
|
||||
...payload
|
||||
orderBy: payload.orderBy,
|
||||
orderDir: payload.order
|
||||
})
|
||||
);
|
||||
|
||||
@ -1088,13 +1058,14 @@ export default class TableRenderer extends React.Component<
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleFilter(payload: any) {
|
||||
async handleFilter(payload: {
|
||||
filterName: string,
|
||||
filterValue: string
|
||||
}) {
|
||||
const {dispatchEvent, data, onFilter} = this.props;
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'columnFilter',
|
||||
createObject(data, {
|
||||
payload
|
||||
})
|
||||
createObject(data, payload)
|
||||
);
|
||||
|
||||
if (rendererEvent?.prevented) {
|
||||
@ -1105,24 +1076,66 @@ export default class TableRenderer extends React.Component<
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleDragOver(dataSource: Array<any>) {
|
||||
const {dispatchEvent, data, onDrag} = this.props;
|
||||
async handleRowClick(event: React.ChangeEvent<any>, rowItem: any, rowIndex?: number) {
|
||||
const {dispatchEvent, data, onRow} = this.props;
|
||||
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'dragOver',
|
||||
createObject(data, {
|
||||
dataSource
|
||||
})
|
||||
'rowClick',
|
||||
createObject(data, {rowItem})
|
||||
);
|
||||
|
||||
if (rendererEvent?.prevented) {
|
||||
return rendererEvent?.prevented;
|
||||
}
|
||||
|
||||
onDrag && onDrag(dataSource);
|
||||
if (rowItem && onRow) {
|
||||
onRow.onRowClick && onRow.onRowClick(event, rowItem, rowIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleOrderChange(oldIndex: number, newIndex: number, levels: Array<string>) {
|
||||
const {store} = this.props;
|
||||
const rowItem = store.getRowByIndex(oldIndex, levels);
|
||||
|
||||
store.exchange(oldIndex, newIndex, rowItem);
|
||||
}
|
||||
|
||||
@autobind
|
||||
async handleSaveOrder() {
|
||||
const {store, onSaveOrder, data, dispatchEvent} = this.props;
|
||||
|
||||
const movedItems = store.movedRows.map(item => item.data);
|
||||
const items = store.rows.map(item => item.getDataWithModifiedChilden());
|
||||
|
||||
const rendererEvent = await dispatchEvent(
|
||||
'orderChange',
|
||||
createObject(data, {movedItems})
|
||||
);
|
||||
|
||||
if (rendererEvent?.prevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!onSaveOrder || !store.movedRows.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
onSaveOrder(
|
||||
movedItems,
|
||||
items
|
||||
);
|
||||
}
|
||||
|
||||
@autobind
|
||||
reset() {
|
||||
const {store} = this.props;
|
||||
|
||||
store.reset();
|
||||
}
|
||||
|
||||
doAction(action: Action, args: any, throwErrors: boolean): any {
|
||||
const {store, rowSelection} = this.props;
|
||||
const {store, rowSelection, data} = this.props;
|
||||
|
||||
const actionType = action?.actionType as string;
|
||||
const keyField = rowSelection?.keyField;
|
||||
@ -1135,7 +1148,15 @@ export default class TableRenderer extends React.Component<
|
||||
store.updateSelected([], keyField);
|
||||
break;
|
||||
case 'select':
|
||||
store.updateSelected(args?.selectedRowKeys, keyField);
|
||||
const dataSource = store.getData(data);
|
||||
const selected: Array<any> = [];
|
||||
dataSource.items.forEach((item: any, rowIndex: number) => {
|
||||
const flag = evalExpression(args?.selectedRowKeysExpr, {record: item, rowIndex});
|
||||
if (flag) {
|
||||
selected.push(item[keyField]);
|
||||
}
|
||||
});
|
||||
store.updateSelected(selected, keyField);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -1158,6 +1179,7 @@ export default class TableRenderer extends React.Component<
|
||||
placeholder,
|
||||
rowClassNameExpr,
|
||||
itemActions,
|
||||
onRow,
|
||||
store,
|
||||
...rest
|
||||
} = this.props;
|
||||
@ -1310,23 +1332,73 @@ export default class TableRenderer extends React.Component<
|
||||
loading={this.renderSchema('loading', loading)}
|
||||
placeholder={this.renderSchema('placeholder', placeholder)}
|
||||
onSelect={this.handleSelected}
|
||||
onSelectAll={this.handleSelectedAll}
|
||||
onSelectAll={this.handleSelected}
|
||||
onSort={this.handleSort}
|
||||
onFilter={this.handleFilter}
|
||||
onDrag={this.handleDragOver}
|
||||
onDrag={this.handleOrderChange}
|
||||
itemActions={itemActionsConfig}
|
||||
onRow={{
|
||||
...onRow,
|
||||
onRowClick: this.handleRowClick
|
||||
}}
|
||||
></Table>
|
||||
);
|
||||
}
|
||||
|
||||
renderHeading() {
|
||||
let {
|
||||
store,
|
||||
classnames: cx,
|
||||
headingClassName,
|
||||
translate: __
|
||||
} = this.props;
|
||||
|
||||
if (store.moved) {
|
||||
return (
|
||||
<div className={cx('Table-heading', headingClassName)} key="heading">
|
||||
{
|
||||
store.moved ? (
|
||||
<span>
|
||||
{__('Table.moved', {
|
||||
moved: store.moved
|
||||
})}
|
||||
<button
|
||||
type="button"
|
||||
className={cx('Button Button--xs Button--success m-l-sm')}
|
||||
onClick={this.handleSaveOrder}
|
||||
>
|
||||
<Icon icon="check" className="icon m-r-xs" />
|
||||
{__('Form.submit')}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={cx('Button Button--xs Button--danger m-l-sm')}
|
||||
onClick={this.reset}
|
||||
>
|
||||
<Icon icon="close" className="icon m-r-xs" />
|
||||
{__('Table.discard')}
|
||||
</button>
|
||||
</span>
|
||||
) : null
|
||||
}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {classnames: cx} = this.props;
|
||||
|
||||
this.renderedToolbars = []; // 用来记录哪些 toolbar 已经渲染了
|
||||
|
||||
const heading = this.renderHeading();
|
||||
|
||||
return (
|
||||
<div className={cx('Table-render-wrapper')}>
|
||||
{this.renderActions('header')}
|
||||
{heading}
|
||||
{this.renderTable()}
|
||||
</div>
|
||||
);
|
||||
|
@ -23,7 +23,8 @@ import {
|
||||
isObject,
|
||||
immutableExtends,
|
||||
isEmpty,
|
||||
extendObject
|
||||
extendObject,
|
||||
findTree
|
||||
} from '../utils/helper';
|
||||
import {normalizeApiResponseData} from '../utils/api';
|
||||
import {Api, Payload, fetchOptions, ApiObject} from '../types';
|
||||
@ -71,6 +72,7 @@ export const Row = types
|
||||
.model('Row', {
|
||||
storeType: 'Row',
|
||||
id: types.identifier,
|
||||
parentId: '',
|
||||
key: types.string,
|
||||
pristine: types.frozen({} as any), // 原始数据
|
||||
data: types.frozen({} as any),
|
||||
@ -100,6 +102,10 @@ export const Row = types
|
||||
);
|
||||
},
|
||||
|
||||
get moved() {
|
||||
return self.index !== self.newIndex;
|
||||
},
|
||||
|
||||
get locals(): any {
|
||||
let children: Array<any> | null = null;
|
||||
if (self.children.length) {
|
||||
@ -120,6 +126,20 @@ export const Row = types
|
||||
}
|
||||
: self.data
|
||||
);
|
||||
},
|
||||
|
||||
getDataWithModifiedChilden() {
|
||||
let data = {
|
||||
...self.data
|
||||
};
|
||||
|
||||
if (data.children && self.children) {
|
||||
data.children = self.children.map(item =>
|
||||
item.getDataWithModifiedChilden()
|
||||
);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}))
|
||||
.actions(self => ({
|
||||
@ -161,6 +181,10 @@ export const Row = types
|
||||
change(values: object, savePristine?: boolean) {
|
||||
self.data = immutableExtends(self.data, values);
|
||||
savePristine && (self.pristine = self.data);
|
||||
},
|
||||
reset() {
|
||||
self.newIndex = self.index;
|
||||
self.data = self.pristine;
|
||||
}
|
||||
}));
|
||||
|
||||
@ -250,10 +274,26 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
});
|
||||
}
|
||||
|
||||
function getRowByIndex(rowIndex: number, levels?: Array<string>): IRow {
|
||||
if (levels && levels.length > 0) {
|
||||
const index = +(levels.shift() || 0);
|
||||
return getRowByIndex(index, levels);
|
||||
}
|
||||
return self.rows[rowIndex];
|
||||
}
|
||||
|
||||
function isSelected(row: IRow): boolean {
|
||||
return !!~self.selectedRows.indexOf(row);
|
||||
}
|
||||
|
||||
function getMovedRows() {
|
||||
return flattenTree(self.rows).filter((item: IRow) => item.moved);
|
||||
}
|
||||
|
||||
function getMoved() {
|
||||
return getMovedRows().length;
|
||||
}
|
||||
|
||||
return {
|
||||
get toggable() {
|
||||
return getToggable();
|
||||
@ -287,6 +327,10 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
return self.expandedRowKeys.map(item => item);
|
||||
},
|
||||
|
||||
get unSelectedRows() {
|
||||
return getUnSelectedRows();
|
||||
},
|
||||
|
||||
// 是否隐藏了某列
|
||||
hasColumnHidden() {
|
||||
return hasColumnHidden();
|
||||
@ -294,7 +338,21 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
|
||||
getData,
|
||||
|
||||
isSelected
|
||||
getRowById(id: string) {
|
||||
return findTree(self.rows, item => item.id === id);
|
||||
},
|
||||
|
||||
isSelected,
|
||||
|
||||
getRowByIndex,
|
||||
|
||||
get moved() {
|
||||
return getMoved();
|
||||
},
|
||||
|
||||
get movedRows() {
|
||||
return getMovedRows();
|
||||
},
|
||||
};
|
||||
})
|
||||
.actions(self => {
|
||||
@ -333,6 +391,31 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
}
|
||||
}
|
||||
|
||||
function exchange(fromIndex: number, toIndex: number, item?: IRow) {
|
||||
item = item || self.rows[fromIndex];
|
||||
|
||||
if (item.parentId) {
|
||||
const parent: IRow = self.getRowById(item.parentId) as any;
|
||||
const offset = parent.children.indexOf(item) - fromIndex;
|
||||
toIndex += offset;
|
||||
fromIndex += offset;
|
||||
|
||||
const newRows = parent.children.concat();
|
||||
newRows.splice(fromIndex, 1);
|
||||
newRows.splice(toIndex, 0, item);
|
||||
newRows.forEach((item, index) => (item.newIndex = index));
|
||||
parent.children.replace(newRows);
|
||||
return;
|
||||
}
|
||||
|
||||
const newRows = self.rows.concat();
|
||||
newRows.splice(fromIndex, 1);
|
||||
newRows.splice(toIndex, 0, item);
|
||||
|
||||
newRows.forEach((item, index) => (item.newIndex = index));
|
||||
self.rows.replace(newRows);
|
||||
}
|
||||
|
||||
function persistSaveToggledColumns() {
|
||||
const key =
|
||||
location.pathname +
|
||||
@ -600,6 +683,22 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
}
|
||||
});
|
||||
|
||||
function reset() {
|
||||
self.rows.forEach(item => item.reset());
|
||||
let rows = self.rows.concat();
|
||||
eachTree(rows, item => {
|
||||
if (item.children) {
|
||||
let rows = item.children.concat().sort((a, b) => a.index - b.index);
|
||||
rows.forEach(item => item.reset());
|
||||
item.children.replace(rows);
|
||||
}
|
||||
});
|
||||
rows.forEach(item => item.reset());
|
||||
rows = rows.sort((a, b) => a.index - b.index);
|
||||
self.rows.replace(rows);
|
||||
self.dragging = false;
|
||||
}
|
||||
|
||||
return {
|
||||
update,
|
||||
persistSaveToggledColumns,
|
||||
@ -609,6 +708,8 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
updateSelected,
|
||||
updateSelectedAll,
|
||||
updateExpanded,
|
||||
exchange,
|
||||
reset,
|
||||
|
||||
// events
|
||||
afterCreate() {
|
||||
@ -631,11 +732,7 @@ export const TableStoreV2 = ServiceStore.named('TableStoreV2')
|
||||
}
|
||||
}, 200);
|
||||
},
|
||||
saveRemote,
|
||||
|
||||
getRowByIndex(rowIndex: number) {
|
||||
return self.rows[rowIndex];
|
||||
}
|
||||
saveRemote
|
||||
};
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user