table icon indicating copy to clipboard operation
table copied to clipboard

Custom Sorting Function

Open NickCain opened this issue 7 years ago • 13 comments

I am trying to implement my own sorting function, as this will be done Server Side based so I will just dispatch an action to talk to the API but I am having problems getting the name of the column and sort order in this func.

I'm using react table V7, and i've added the prop of manualSorting: true to disable the default behaviour, but when adding the custom orderByFn to the useTable hook it calls it but returns an undefined param, I was expecting something with the column name and the sort order in this? Any ideas would be great.

I've created a code sandbox here based on one of the React Table examples showing my issue.

https://codesandbox.io/embed/tannerlinsleyreact-table-sorting-f1bmd

NickCain avatar Oct 22 '19 08:10 NickCain

@NickCain This is my server side sorting solution:

import React, { useEffect } from "react";
import { useSortBy, useTable } from "react-table";

const ReactTableTest = (props) => {
  const { columns, data, initialSortBy, onChangeSort } = props;

  const {
    headerGroups,
    getTableProps,
    getTableBodyProps,
    prepareRow,
    rows,
    state: { sortBy }
  } = useTable(
    {
      columns,
      data,
      initialState: { sortBy: initialSortBy },
      manualSorting: true
    },
    useSortBy
  );

  // Store new sort state in reducer and call API to fetch new data from server
  useEffect(() => {
    onChangeSort(sortBy);
  }, [onChangeSort, sortBy]);

  return (
    <span>render table here</span>
  );
};

export default ReactTableTest;

MatyCZ avatar Oct 23 '19 13:10 MatyCZ

@MatyCZ - Thanks for your example it helped lead me to the issue, and I've now resolved this problem.

NickCain avatar Oct 23 '19 15:10 NickCain

Thanks @MatyCZ Doc still doesn't contains any mentions about manual sorting feature usage

abezhinaru avatar Mar 30 '20 14:03 abezhinaru

Need an example for custom server side sorting?

IamDhana avatar Apr 25 '20 08:04 IamDhana

Need an example for custom server side sorting?

yes

ravsaxena avatar Apr 28 '20 12:04 ravsaxena

@MatyCZ and @NickCain can you show an example in the codesandbox?

Luccasoli avatar Jun 07 '20 19:06 Luccasoli

I noticed there is no manualSorting flag anymore in the useSortBy hook. The one I have in my build is manualSortBy. Here is my example of remote sorting:

https://codesandbox.io/s/stupefied-nobel-tx4gq

import React, { useState, useEffect, useCallback } from "react";
import { useTable, useSortBy } from "react-table";

function Table({ columns, data, onSort }) {
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
    state: {sortBy}
  } = useTable(
    {
      columns,
      data,
      manualSortBy: true
    },
    useSortBy
  );

  useEffect(() => {
    onSort(sortBy);
  }, [onSort, sortBy]);

  return <table ... />;
}

function App() {
  const columns = React.useMemo(
    // ... columns ...
    []
  );

  const [data, setData] = useState([]);

  const handleSort = useCallback(sortBy => {
    //remote sort
    //... fetch("your-api", sortBy) ...
  }, [])

  return <Table columns={columns} data={data} onSort={handleSort} />;
}

export default App;

chaoszcat avatar Jul 07 '20 02:07 chaoszcat

@NickCain This is my server side sorting solution:

import React, { useEffect } from "react";
import { useSortBy, useTable } from "react-table";

const ReactTableTest = (props) => {
  const { columns, data, initialSortBy, onChangeSort } = props;

  const {
    headerGroups,
    getTableProps,
    getTableBodyProps,
    prepareRow,
    rows,
    state: { sortBy }
  } = useTable(
    {
      columns,
      data,
      initialState: { sortBy: initialSortBy },
      manualSorting: true
    },
    useSortBy
  );

  // Store new sort state in reducer and call API to fetch new data from server
  useEffect(() => {
    onChangeSort(sortBy);
  }, [onChangeSort, sortBy]);

  return (
    <span>render table here</span>
  );
};

export default ReactTableTest;

Im getting Property 'sortBy' does not exist on type 'TableState' when following this example :(

What have i done wrong?

gotpist1 avatar Oct 22 '20 13:10 gotpist1

@gotpist1 maybe add your code here? Not sure what's wrong since the example is working:

https://codesandbox.io/s/stupefied-nobel-tx4gq

chaoszcat avatar Oct 23 '20 01:10 chaoszcat

Is it possible to use sorting function for definite columns and leave default sorting for other ones?

tar-aldev avatar Oct 26 '20 15:10 tar-aldev

I'm kinda asking the same question as @tar-aldev. I need to use the default sorting but to save what the user has sorted (preference) on the UI, and then reapply it when it comes back which means i somehow need to access each column and tell it how to sort on page load. if anyone has any suggestions i could really use one

nicuvlad7 avatar Oct 30 '20 12:10 nicuvlad7

using the react-table npm package how to perform the sorting operation for the table. Also without using the useSortBy Hook.

shvamabps avatar Jan 18 '21 06:01 shvamabps

@tar-aldev have you found solution for sorting only specific columns?

sophia-kravchenko avatar Feb 01 '24 16:02 sophia-kravchenko