material-table icon indicating copy to clipboard operation
material-table copied to clipboard

set defaultSort programmatically

Open hongjs opened this issue 6 years ago • 8 comments

Describe the bug I was set config and data from state programmatically but result doesn't sort

To Reproduce when data and config was bind, result doesn't sorted as config

Expected behavior result should be sorted

export default function TestReport() {
  const [data, setData] = useState([]);
  const [config, setConfig] = useState([]);

  useEffect(() => {
    setData(getData());
    setConfig(getConfig());
  }, []);

  const getData = () => {
    return [
      { name: 'Mehmet', year: 1987 },
      { name: 'Zerya Betül', year: 2017 }
    ];
  };

  const getConfig = () => {
    return [
      { title: 'Name', field: 'name' },
      { title: 'Year', field: 'year', defaultSort: 'desc' }
    ];
  };

  return <MaterialTable columns={config} data={data} />;
}

Screenshots image

Expected image

material-table v1.57.2

hongjs avatar Apr 27 '20 10:04 hongjs

have you set sort:true ?

tonypee avatar May 28 '20 00:05 tonypee

have you set sort:true ?

did you mean sorting:true ? I believe default value of sorting is true, both options and columns

hongjs avatar May 28 '20 04:05 hongjs

I just battled with this today too. Think i figured it out.

The first paint (render of the component) will have empty values, because the useState values are defaulted to []. This will render the Table, with its columns == []. This means that he columns do NOT have defaults. ie the defaultSort is null for both. When you run the effect, it sets the VALUES of the columns, but the defaults have no effect. It seems that the defaults are only use in the first paint. To refresh my values and the sort values. I make sure that the table is fully re-rendered. I did this in my app by fully removing the table when loading, then re placing it once data is loaded.

It seems like a bug to me

image

https://codesandbox.io/s/trusting-hertz-w1ufb?file=/src/App.js:691-778

make sure you hit the refresh button on the preview there to get the initial load values

tonypee avatar May 28 '20 11:05 tonypee

yes, It seem like a bug, defaultSort are inside "columns", Columns are changeable by useEffect. so, defaultSort should be changeable as well.

hongjs avatar Jun 01 '20 03:06 hongjs

You can use the following workaround for now, if you would like..

Live Demo Here

import React, { useState, useEffect } from "react";
import { render } from "react-dom";
import MaterialTable from "material-table";
import tableIcons from "./TableIcons.js";

const App = () => {
  const [data, setData] = useState([]);
  const [config, setConfig] = useState([]);

  useEffect(() => {
    const sortedData = sortByProp(getData(), "year", "desc");
    setData(sortedData);
    setConfig(getConfig());
  }, []);

  const sortByProp = (arr = [], property, ascOrDesc) => {
    if (
      !arr.length ||
      !property ||
      !["asc", "desc"].includes(ascOrDesc.toLowerCase())
    ) return;

    let sortingAlgo = (x, y) => (x[property] < y[property] ? 1 : -1);
    if (ascOrDesc === "asc") {
      sortingAlgo = (x, y) => (x[property] > y[property] ? 1 : -1);
    }

    return arr.sort(sortingAlgo);
  };

  const getData = () => {
    return [
      { name: "Mehmet", year: 1987 },
      { name: "Zerya Betül", year: 2017 }
    ];
  };

  const getConfig = () => {
    return [
      { title: "Name", field: "name" },
      { title: "Year", field: "year", defaultSort: "desc" }
    ];
  };

  return <MaterialTable columns={config} data={data} icons={tableIcons} />;
};

render(<App />, document.querySelector("#root"));

matthewoestreich avatar Jun 02 '20 00:06 matthewoestreich

You can use the following workaround for now, if you would like..

Live Demo Here

import React, { useState, useEffect } from "react";
import { render } from "react-dom";
import MaterialTable from "material-table";
import tableIcons from "./TableIcons.js";

const App = () => {
  const [data, setData] = useState([]);
  const [config, setConfig] = useState([]);

  useEffect(() => {
    const sortedData = sortByProp(getData(), "year", "desc");
    setData(sortedData);
    setConfig(getConfig());
  }, []);

  const sortByProp = (arr = [], property, ascOrDesc) => {
    if (
      !arr.length ||
      !property ||
      !["asc", "desc"].includes(ascOrDesc.toLowerCase())
    ) return;

    let sortingAlgo = (x, y) => (x[property] < y[property] ? 1 : -1);
    if (ascOrDesc === "asc") {
      sortingAlgo = (x, y) => (x[property] > y[property] ? 1 : -1);
    }

    return arr.sort(sortingAlgo);
  };

  const getData = () => {
    return [
      { name: "Mehmet", year: 1987 },
      { name: "Zerya Betül", year: 2017 }
    ];
  };

  const getConfig = () => {
    return [
      { title: "Name", field: "name" },
      { title: "Year", field: "year", defaultSort: "desc" }
    ];
  };

  return <MaterialTable columns={config} data={data} icons={tableIcons} />;
};

render(<App />, document.querySelector("#root"));

this will work only first render, I'm asking for programmatically sorting.

as columns can be changed by state, so columns.defaultSort should be changed as well

hongjs avatar Jun 22 '20 14:06 hongjs

@hongjs that should be fixed by the latest release. Can you try it out and reopen the issue if needed. Your initial code displayed in the initial description.

Domino987 avatar Jun 22 '20 15:06 Domino987

@Domino987 Thank you so much for the help

hongjs avatar Jun 23 '20 14:06 hongjs