当前位置:网站首页>[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
2022-06-25 13:15:00 【Coconut brine Engineer】
DragSortTable Sort by react-sortable-hoc, Need to provide rowKey To determine the unique value of the data , Otherwise it won't work properly . Temporary does not support request Sort the requested data , Can be request The requested data is saved through dataSource Pass in .
️: If the list is not reordered after dragging , The big reason is that there is no transmission dataSource As a result of . So in request The data requested in the needs to be stored dataSource Talent .
ProTable To configure
<ProTable
rowKey="index"
columns={
columns}
actionRef={
actionRef}
form={
form}
pagination={
{
pageSize: 10,
}}
dataSource={
dataSource}
search={
true}
request={
async (params) => {
const res = await getRuleList(p, {
...params,
orderByPriority: true,
current: 1,
pageSize: 10,
status: 3,
});
const list = res.data.list.map((item: any, index: number) => ({
...item, index }));
setDataSource(list);
setDisableBtn(list[0].inPriorityAudit);
return {
data: list,
success: res?.success,
total: res?.data?.total,
};
}}
components={
{
body: {
wrapper: DraggableContainer,
row: DraggableBodyRow,
},
}}
toolBarRender={
() => [
<Button
onClick={
() => setIsEdit(true)}
type="primary"
disabled={
disableBtn}
style={
{
display: isEdit ? 'none' : 'block' }}
>
Ed Compilation
</Button>,
<Button
onClick={
() => setIsEdit(false)}
type="primary"
style={
{
display: isEdit ? 'block' : 'none' }}
>
Cancel editing
</Button>,
<Popconfirm
title=" Are you sure to submit for review ?"
onConfirm={
confirmSubmit}
okText=" confirm "
cancelText=" Cancel "
>
<Button type="primary" disabled={
!isEdit}>
Submit audit
</Button>
</Popconfirm>,
<Popconfirm
title=" Whether it is approved or not "
onConfirm={
async () => {
await client<any>(` The audit passes the interface `, {
method: 'put',
params: {
},
}).then((res) => {
if (res?.success)
notification.success({
message: ' Audit completed ',
});
});
actionRef?.current?.reload();
}}
onCancel={
async () => {
await client<any>(` Audit failed interface `, {
method: 'put',
params: {
},
}).then((res) => {
if (res?.success)
notification.success({
message: ' Audit completed ',
});
});
actionRef?.current?.reload();
}}
okText=" adopt "
cancelText=" Refuse "
>
<Button type="primary" disabled={
!disableBtn}>
To examine nucleus
</Button>
</Popconfirm>,
]}
/>

Drag and drop functionality
components={
{
body: {
wrapper: DraggableContainer,
row: DraggableBodyRow,
},
}}
const onSortEnd = ({
oldIndex, newIndex }: {
oldIndex: number; newIndex: number }) => {
if (oldIndex !== newIndex) {
setIsDrag(true);
const newData = arrayMoveImmutable([...dataSource], oldIndex, newIndex).filter((el) => !!el);
const list = [...newData].map((item: any, index: number) => ({
...item, index })) as any;
list.map((item: any) => {
obj = {
...obj,
ruleId: item.id,
newPriority: item.index + 1,
};
itemList.push(obj);
setDragList(itemList);
});
setDataSource(list);
}
};
const DraggableContainer = (props: any) => (
<SortContainer
useDragHandle
disableAutoscroll
helperClass="row-dragging"
onSortEnd={
onSortEnd}
{
...props}
/>
);
const DraggableBodyRow = (props: any) => {
const {
className, style, ...restProps } = props;
const index = dataSource.findIndex((x) => x.index === restProps['data-row-key']);
return <SortableItem index={
index} {
...restProps} />;
}
thus , Drag and drop sorting can be realized .
Drag and drop sorting through editing
1)InputNumber Listen for changes in the input box
const columns = [
{
title: ' Rule priority ',
dataIndex: 'businessPriority',
hideInSearch: true,
align: 'center',
render: (text: any, record: any) => {
if (isEdit) {
return (
<div>
<InputNumber
defaultValue={
isDrag ? record.index + 1 : record.businessPriority}
onChange={
(value: any) => inputChange(value, record)}
/>
</div>
);
} else {
return record.businessPriority;
}
},
},
{
title: 'index',
dataIndex: 'index',
hideInSearch: true,
hideInTable: true,
},
] as any;
2) Set two status values , It is judged to be in editing status or Drag state
const [isEdit, setIsEdit] = useState(false);
const [isDrag, setIsDrag] = useState(false);
3) Get the whole Table Edited data , There is itemList In the array
let itemList = [] as any;
let obj = {
} as any;
const inputChange = (value: any, record: any) => {
let ruleId;
let newPriority;
if (value) {
obj = {
...obj,
ruleId: record.id,
newPriority: value,
};
for (let i = 0; i < itemList.length; i++) {
if (itemList[i].ruleId === obj.ruleId) {
itemList[i].newPriority = obj.newPriority;
return;
}
}
itemList.push(obj);
}
};
Drag and drop sorting combined with editing function , How to filter duplicate data
There is no problem using the above steps alone , But if we After dragging -> Then edit the input box perhaps Edit the input box -> Then drag and drop to adjust the sorting , When the value is finally transferred , There will be duplicate values in the array , Similar to the following structure :
[
{
id: 1, newIndex: 2},
{
id: 2, newIndex: 5},
{
id: 1, newIndex: 3} // Once again, it has been revised id by 1 Sort value of
]
In this way, the back end cannot know id:1 What is the sorting value of the data of , So we need to do it for the last time , Replace the old sort value .
1) Record the array after dragging 、 The array after editing
Drag array
const [dragList, setDragList] = useState([]);
Edit array
let itemList = [] as any;
2) Re assign values before submitting data
const confirmSubmit = async () => {
console.log(' The submitted dragList', dragList);
console.log(' The submitted itemList', itemList);
const result = handleListrepeat(dragList, itemList);
}
const handleListrepeat = (listdrag: any, listItem: any) => {
if (listdrag.length === 0) {
return listItem;
} else if (listItem.length === 0) {
return listdrag;
} else {
for (let i = 0; i < listdrag.length; i++) {
for (let k = 0; k < listItem.length; k++) {
if (listdrag[i].ruleId === listItem[k].ruleId) {
listdrag[i].newPriority = listItem[k].newPriority;
return listdrag;
}
}
}
}
};
Finally, you can correctly pass the newly sorted array to the back end !
边栏推荐
- LeetCode链表题解技巧归纳总结
- 20220620 interview reply
- Stockage des données en mémoire
- Summer Ending
- 關於數據在內存中的存儲下
- Sword finger offer day 1 stack and queue (simple)
- It's an artifact to launch a website in a few minutes
- 15 basic SEO skills to improve ranking
- Sword finger offer II 032 Effective anagrams
- Seven competencies required by architects
猜你喜欢

J2EE from entry to earth 01 MySQL installation

Uncover gaussdb (for redis): comprehensive comparison of CODIS

剑指 Offer 第 1 天栈与队列(简单)

leetcode - 384. 打乱数组
![[flask tutorial] flask overview](/img/e8/d85ac54f3a9eb3b1ab31852761154c.jpg)
[flask tutorial] flask overview

WIN10环境下配置pytorch

关于结构体,枚举,联合的一些知识

药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质

Sword finger offer day 1 stack and queue (simple)

Pointer, it has to say that the subject
随机推荐
Optimization of lazyagg query rewriting in parsing data warehouse
It's an artifact to launch a website in a few minutes
字符串各操作函数与内存函数详解
Spoken English - weak reading
Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
J2EE从入门到入土01.MySQL安装
Serevlt初识
用include what you use拯救混乱的头文件
模块五(微博评论)
Oracle trigger error report table or view does not exist
關於數據在內存中的存儲下
@Scheduled implementation of scheduled tasks (concurrent execution of multiple scheduled tasks)
[visio] solving the fuzzy problem of parallelogram in word
RESTful和RPC
Germany holds global food security Solidarity Conference
Online service emergency research methodology
[machine learning] parameter learning and gradient descent
剑指 Offer 第 1 天栈与队列(简单)
与生产环境中的 console.log 说再见
And console Log say goodbye