使用PrimeReact数据表的服务器端排序

huangapple go评论130阅读模式
英文:

Server-side sort with primereact datatable

问题

I try to do a datatable with server-side rendering.
我尝试创建一个带有服务器端渲染的数据表格。

I use primereact datatable v9.5
我使用 primereact 数据表格 v9.5。

I have a problem with multi-column sort: data received from the server are correct but primereact sorts data again.
我在多列排序方面遇到了问题:从服务器收到的数据是正确的,但 primereact 再次对数据进行排序。

For example, a part of data.
例如,部分数据。

{id:2, name:"124"},
{id:3, name:"5"}]

I want to sort by name
我想按名称排序

Server response:
服务器响应:

{id:2, name:"5"},
{id:1, name:"alex"}]

And displayed data:
并且显示的数据:

{id:3, name:"124"},
{id:1, name:"alex"}]

Here is a part of my code:
这是我的代码的一部分:

function DatatableContacts(props: {
  data: Array<ContactInterface>
  totalRecords: number
  loading: boolean
}) {
  const [data, setData] = useState(props.data) //要显示的数据

  const [first1, setFirstRow] = useState(0)
  const [rowsPerPage, setRowsPerPage] = useState(10)
  const [nbDataDisplayed, setNbDataDisplayed] = useState(props.totalRecords)
  //初始化排序
  const [sortMeta, setSortMeta] = useState([])

  //调用 API 的函数
  const fetchData = async (
    page: number,
    itemsPerPage: number,
    sort?: Array<{ field: string; order: number }>
  ) => {
    //从 API 获取数据的函数
    setData(fetchedData)
  }

  const handleSort = async (e: any) => {
    setSortMeta(e.multiSortMeta)
    await fetchData(1, rowsPerPage, e.multiSortMeta)
    setFirstRow(0)
  }

  return (
    <>
      <DataTable
        value={data}
        rows={rowsPerPage}
        first={first1}
        multiSortMeta={sortMeta}
        sortMode="multiple"
        responsiveLayout="scroll"
        dataKey="id"
        loading={isLoading}
        removableSort
        onSort={handleSort}
      >
        <Column
          field="id"
          header={t('contacts.id')}
        ></Column>
        <Column
          field="person.firstname"
          header={t('contacts.firstname')}
          sortable
        ></Column>
        <Column
          field="person.lastname"
          header={t('contacts.lastname')}
          sortable
        ></Column>
      </DataTable>
    </>
  )
}
英文:

I try to do a datatable with server-side rendering.
I use primereact datatable v9.5

I have a problem with multi-column sort : data recieved from server are correct but primereact sorts data again.
So I

For example, a part of data.

data = [{id:1, name:&quot;alex&quot;},
{id:2, name:&quot;124&quot;},
{id:3, name:&quot;5&quot;}]

I want to sort by name
Server response :

[{id:3, name:&quot;124&quot;},
{id:2, name:&quot;5&quot;},
{id:1, name:&quot;alex&quot;}]

And displayed data :

[{id:2, name:&quot;5&quot;},
{id:3, name:&quot;124&quot;},
{id:1, name:&quot;alex&quot;}]

Here is a part of my code :

function DatatableContacts(props: {
data: Array&lt;ContactInterface&gt;
totalRecords: number
loading: boolean
}) {
const [data, setData] = useState(props.data) //donn&#233;es &#224; afficher
const [first1, setFirstRow] = useState(0)
const [rowsPerPage, setRowsPerPage] = useState(10)
const [nbDataDisplayed, setNbDataDisplayed] = useState(props.totalRecords)
//initialisation du tri
const [sortMeta, setSortMeta] = useState([])
//fonction appel API
const fetchData = async (
page: number,
itemsPerPage: number,
sort?: Array&lt;{ field: string; order: number }&gt;
) =&gt; {
//function to get data from API
setData(fetchedData)
}
const handleSort = async (e: any) =&gt; {
setSortMeta(e.multiSortMeta)
await fetchData(1, rowsPerPage, e.multiSortMeta)
setFirstRow(0)
}
return (
&lt;&gt;
&lt;DataTable
value={data}
rows={rowsPerPage}
first={first1}
multiSortMeta={sortMeta}
sortMode=&quot;multiple&quot;
responsiveLayout=&quot;scroll&quot;
dataKey=&quot;id&quot;
loading={isLoading}
removableSort
onSort={handleSort}
&gt;
&lt;Column
field=&quot;id&quot;
header={t(&#39;contacts.id&#39;)}
&gt;&lt;/Column&gt;
&lt;Column
field=&quot;person.firstname&quot;
header={t(&#39;contacts.firstname&#39;)}
sortable
&gt;&lt;/Column&gt;
&lt;Column
field=&quot;person.lastname&quot;
header={t(&#39;contacts.lastname&#39;)}
sortable
&gt;&lt;/Column&gt;
&lt;/DataTable&gt;
&lt;/&gt;
)
}

答案1

得分: 0

我将"lazy"添加到数据表中,一切正常。

英文:

I add lazy to datatable and it is ok

huangapple
  • 本文由 发表于 2023年6月15日 21:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定