当前位置:网站首页>[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 !
边栏推荐
- New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and
- KVM 脚本管理 —— 筑梦之路
- Seven competencies required by architects
- C# 切换中英文输入法
- QT mouse tracking
- 关于扫雷的简易实现
- The editor is used every day. What is the working principle of language service protocol?
- [AI helps scientific research] fool drawing of loss curve
- 15 basic SEO skills to improve ranking
- 关于结构体,枚举,联合的一些知识
猜你喜欢
golang键盘输入语句scanln scanf代码示例
Fedora 35 部署DNS主从和分离解析 —— 筑梦之路
Detailed explanation of string operation functions and memory functions
剑指Offer 第 2 天链表(简单)
Stockage des données en mémoire
Optimization of lazyagg query rewriting in parsing data warehouse
Sword finger offer day 3 string (simple)
Sword finger offer day 2 linked list (simple)
Sword finger offer II 032 Effective anagrams
The editor is used every day. What is the working principle of language service protocol?
随机推荐
Storage related contents of data in memory
Sword finger offer II 029 Sorted circular linked list
KDD 2022 | graphmae: self supervised mask map self encoder
KDD 2022 | GraphMAE:自监督掩码图自编码器
leetcode - 384. Scramble array
AGCO AI frontier promotion (6.25)
Pointer, it has to say that the subject
关于数据在内存中存储的相关例题
Another night when visdom crashed
时间过滤器(el-table)中使用
深圳民太安智能二面_秋招第一份offer
剑指Offer 第 2 天链表(简单)
Germany holds global food security Solidarity Conference
Using CMD (command prompt) to install MySQL & configure the environment
1251- Client does not support authentication protocol MySql错误解决方案
Differences between JS and JQ operation objects
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
You can't specify target table 'xxx' for update in from clause
Detailed explanation of string operation functions and memory functions
Alibaba stability fault emergency handling process