You need to sign in before continuing.
Commit 7f97ea9cf65e0d74024a5e18c9ef2e2c88d7df08
1 parent
117ad119
齿轮排序
Showing
1 changed file
with
73 additions
and
14 deletions
src/components/Common/AffixMenu.js
| @@ -8,6 +8,8 @@ import config from '@/utils/config'; | @@ -8,6 +8,8 @@ import config from '@/utils/config'; | ||
| 8 | import * as commonFunc from '@/components/Common/commonFunc'; | 8 | import * as commonFunc from '@/components/Common/commonFunc'; |
| 9 | import AntdDraggableModal from '@/components/Common/AntdDraggableModal'; | 9 | import AntdDraggableModal from '@/components/Common/AntdDraggableModal'; |
| 10 | import SvgIcon from '../SvgIcon'; | 10 | import SvgIcon from '../SvgIcon'; |
| 11 | +import { DndProvider, useDrag, useDrop } from 'react-dnd'; | ||
| 12 | +import { HTML5Backend } from 'react-dnd-html5-backend'; | ||
| 11 | 13 | ||
| 12 | const { Option } = Select; | 14 | const { Option } = Select; |
| 13 | const { TabPane } = Tabs; | 15 | const { TabPane } = Tabs; |
| @@ -370,7 +372,48 @@ class AffixMenuComponent extends Component { | @@ -370,7 +372,48 @@ class AffixMenuComponent extends Component { | ||
| 370 | }) | 372 | }) |
| 371 | this.onInit(); | 373 | this.onInit(); |
| 372 | } | 374 | } |
| 375 | + moveRow = (dragIndex, hoverIndex, dataSourceKey) => { | ||
| 376 | + const dataSource = this.state[dataSourceKey]; | ||
| 373 | 377 | ||
| 378 | + // 确保 dataSource 是一个数组 | ||
| 379 | + if (!Array.isArray(dataSource)) { | ||
| 380 | + console.error(`dataSource for key ${dataSourceKey} is not an array.`, dataSource); | ||
| 381 | + return; | ||
| 382 | + } | ||
| 383 | + | ||
| 384 | + // 确保索引在合法范围内 | ||
| 385 | + if ( | ||
| 386 | + dragIndex < 0 || | ||
| 387 | + dragIndex >= dataSource.length || | ||
| 388 | + hoverIndex < 0 || | ||
| 389 | + hoverIndex >= dataSource.length || | ||
| 390 | + dragIndex === hoverIndex | ||
| 391 | + ) { | ||
| 392 | + console.warn('Invalid indices for moveRow:', dragIndex, hoverIndex); | ||
| 393 | + return; | ||
| 394 | + } | ||
| 395 | + | ||
| 396 | + // const newData = update(dataSource, { | ||
| 397 | + // $splice: [ | ||
| 398 | + // [dragIndex, 1], // 从 dragIndex 位置移除 1 个元素 | ||
| 399 | + // [hoverIndex, 0, dataSource[dragIndex]], // 在 hoverIndex 位置插入移除的元素 | ||
| 400 | + // ], | ||
| 401 | + // }); | ||
| 402 | + // 创建一个新的数组来存储更新后的数据 | ||
| 403 | + const newData = [...dataSource]; | ||
| 404 | + // 移除 dragIndex 位置的元素 | ||
| 405 | + const [removed] = newData.splice(dragIndex, 1); | ||
| 406 | + // 在 hoverIndex 位置插入移除的元素 | ||
| 407 | + newData.splice(hoverIndex, 0, removed); | ||
| 408 | + | ||
| 409 | + // 更新 iOrder 字段,使其与新的顺序一致 | ||
| 410 | + newData.forEach((item, index) => { | ||
| 411 | + // 根据索引来更新 iOrder,使用 index + 1 因为排序通常从1开始,而不是0 | ||
| 412 | + newData[index].iOrder = (index + 1); | ||
| 413 | + }); | ||
| 414 | + | ||
| 415 | + this.setState({ [dataSourceKey]: newData }); | ||
| 416 | + }; | ||
| 374 | render() { | 417 | render() { |
| 375 | const { modalData, roleShow } = this.state; | 418 | const { modalData, roleShow } = this.state; |
| 376 | const { app, sTabId, sModelsType } = this.props; | 419 | const { app, sTabId, sModelsType } = this.props; |
| @@ -400,12 +443,21 @@ class AffixMenuComponent extends Component { | @@ -400,12 +443,21 @@ class AffixMenuComponent extends Component { | ||
| 400 | const configName = commonUtils.isNotEmptyArr(splitArr) ? splitArr[0] : ''; | 443 | const configName = commonUtils.isNotEmptyArr(splitArr) ? splitArr[0] : ''; |
| 401 | const configId = commonUtils.isNotEmptyArr(splitArr) ? splitArr[1] : ''; | 444 | const configId = commonUtils.isNotEmptyArr(splitArr) ? splitArr[1] : ''; |
| 402 | let childTable = ''; | 445 | let childTable = ''; |
| 403 | - let dataSource = []; | ||
| 404 | - if (commonUtils.isNotEmptyObject(configId)) { /* 从缓存中取宽度 */ | ||
| 405 | - dataSource = this.state[`modalContentData_${configId}`]; | ||
| 406 | - } else { | ||
| 407 | - dataSource = this.state[`modalContentData${count}`]; | 446 | + // let dataSource = []; |
| 447 | + // if (commonUtils.isNotEmptyObject(configId)) { /* 从缓存中取宽度 */ | ||
| 448 | + // dataSource = this.state[`modalContentData_${configId}`]; | ||
| 449 | + // } else { | ||
| 450 | + // dataSource = this.state[`modalContentData${count}`]; | ||
| 451 | + // } | ||
| 452 | + let dataSourceKey = `modalContentData${count}`; | ||
| 453 | + if (commonUtils.isNotEmptyObject(configId)) { | ||
| 454 | + dataSourceKey = `modalContentData_${configId}`; | ||
| 408 | } | 455 | } |
| 456 | + let dataSource = this.state[dataSourceKey] || []; | ||
| 457 | + dataSource.forEach((item, index) => { | ||
| 458 | + // 根据索引来更新 iOrder,使用 index + 1 因为排序通常从1开始,而不是0 | ||
| 459 | + dataSource[index].iOrder = (index + 1) * 10; | ||
| 460 | + }); | ||
| 409 | dataSource?.forEach((item, index) => { | 461 | dataSource?.forEach((item, index) => { |
| 410 | // 根据索引来更新 iOrder,使用 index + 1 因为排序通常从1开始,而不是0 | 462 | // 根据索引来更新 iOrder,使用 index + 1 因为排序通常从1开始,而不是0 |
| 411 | dataSource[index].iOrder = (index + 1) * 10; | 463 | dataSource[index].iOrder = (index + 1) * 10; |
| @@ -510,21 +562,28 @@ class AffixMenuComponent extends Component { | @@ -510,21 +562,28 @@ class AffixMenuComponent extends Component { | ||
| 510 | {checkBoxShow} | 562 | {checkBoxShow} |
| 511 | | 563 | |
| 512 | {checkAll} | 564 | {checkAll} |
| 513 | - <Table | ||
| 514 | - bordered | ||
| 515 | - columns={columns} | ||
| 516 | - components={components} | ||
| 517 | - dataSource={dataSource_dep} | ||
| 518 | - pagination={false} | ||
| 519 | - scroll={{ y: 310 }} | ||
| 520 | - className={styles.affixMenuTable} | ||
| 521 | - /> | 565 | + <DndProvider backend={HTML5Backend}> |
| 566 | + <Table | ||
| 567 | + bordered | ||
| 568 | + columns={columns} | ||
| 569 | + dataSource={dataSource} | ||
| 570 | + components={components} | ||
| 571 | + onRow={(record, index) => ({ | ||
| 572 | + index, | ||
| 573 | + moveRow: (dragIndex, hoverIndex) => this.moveRow(dragIndex, hoverIndex, dataSourceKey), | ||
| 574 | + })} | ||
| 575 | + pagination={false} | ||
| 576 | + scroll={{ y: 310 }} | ||
| 577 | + className={styles.affixMenuTable} | ||
| 578 | + /> | ||
| 579 | + </DndProvider> | ||
| 522 | </div> | 580 | </div> |
| 523 | </TabPane> | 581 | </TabPane> |
| 524 | ); | 582 | ); |
| 525 | modalContent.push(childTable); | 583 | modalContent.push(childTable); |
| 526 | count += 1; | 584 | count += 1; |
| 527 | }); | 585 | }); |
| 586 | + | ||
| 528 | return ( | 587 | return ( |
| 529 | <div> | 588 | <div> |
| 530 | <Affix offsetTop={this.state.top} style={{ position: 'absolute', top: 50, left: 1000 }}> | 589 | <Affix offsetTop={this.state.top} style={{ position: 'absolute', top: 50, left: 1000 }}> |