Skip to content

Commit

Permalink
Fix #3817: Row reorder fixes from PrimeNG (#3818)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Dec 23, 2022
1 parent aa596e0 commit 0d313c0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
14 changes: 7 additions & 7 deletions components/doc/datatable/reorderdoc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useState, useEffect, useRef } from 'react';
import { DataTable } from '../../lib/datatable/DataTable';
import React, { useEffect, useRef, useState } from 'react';
import { ProductService } from '../../../service/ProductService';
import { Column } from '../../lib/column/Column';
import { DataTable } from '../../lib/datatable/DataTable';
import { Toast } from '../../lib/toast/Toast';
import { ProductService } from '../../../service/ProductService';
import { DocSectionCode } from '../common/docsectioncode';
import { DocSectionText } from '../common/docsectiontext';

Expand Down Expand Up @@ -37,7 +37,7 @@ export function ReorderDoc(props) {

const code = {
basic: `
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll" paginator rows={5}>
<Column rowReorder style={{width: '3em'}} />
{dynamicColumns}
</DataTable>
Expand Down Expand Up @@ -83,7 +83,7 @@ const ReorderDoc = () => {
<Toast ref={toast}></Toast>
<div className="card">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll" paginator rows={5}>
<Column rowReorder style={{width: '3em'}} />
{dynamicColumns}
</DataTable>
Expand Down Expand Up @@ -133,7 +133,7 @@ const ReorderDoc = () => {
<Toast ref={toast}></Toast>
<div className="card">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll" paginator rows={5}>
<Column rowReorder style={{width: '3em'}} />
{dynamicColumns}
</DataTable>
Expand All @@ -151,7 +151,7 @@ const ReorderDoc = () => {
</DocSectionText>
<div className="card">
<Toast ref={toast}></Toast>
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll">
<DataTable value={products} reorderableColumns reorderableRows onRowReorder={onRowReorder} onColReorder={onColReorder} responsiveLayout="scroll" paginator rows={5}>
<Column rowReorder style={{ width: '3em' }} />
{dynamicColumns}
</DataTable>
Expand Down
4 changes: 2 additions & 2 deletions components/lib/datatable/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ export const TableBody = React.memo(

if (droppedRowIndex.current != null) {
let dropIndex = draggedRowIndex.current > droppedRowIndex.current ? droppedRowIndex.current : droppedRowIndex.current === 0 ? 0 : droppedRowIndex.current - 1;
let val = [...props.value];
let val = [...props.tableProps.value];

ObjectUtils.reorderArray(val, draggedRowIndex.current, dropIndex);

Expand All @@ -663,7 +663,7 @@ export const TableBody = React.memo(
originalEvent: event,
value: val,
dragIndex: draggedRowIndex.current,
dropIndex: droppedRowIndex.current
dropIndex: dropIndex
});
}
}
Expand Down
9 changes: 2 additions & 7 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,10 @@ export default class ObjectUtils {
}

static reorderArray(value, from, to) {
let target;

if (value && from !== to) {
if (to >= value.length) {
target = to - value.length;

while (target-- + 1) {
value.push(undefined);
}
to %= value.length;
from %= value.length;
}

value.splice(to, 0, value.splice(from, 1)[0]);
Expand Down

0 comments on commit 0d313c0

Please sign in to comment.