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

Allow emptyDataSourceMessage to differentiate between No Results and No Results with Filter

Open hamiltondanielb opened this issue 6 years ago • 4 comments

Currently emptyDataSourceMessage is the same error message no matter what. It would be great if we could allow a specific error message if a filter/search was apply and no result was found.

For example I would like to display "No results" found if the remote API returns no results but "No results found, please clear filters" if no results but filter applied.

hamiltondanielb avatar Jul 16 '20 21:07 hamiltondanielb

This is why I'm doing for this functionality. You can tailor is to your use case by looking at the length of the array of your datasource. Note that you have to manage the state appropriately within fetching functions, etc localization={{ body: { emptyDataSourceMessage: !hasSearched && ['Closed Negs', 'Closed Appeals', 'Client Volume'].includes(activeReport) ? 'Enter search parameters and click Get Report' : activeReport ? 'No records to display' : 'Select a report to display', }, }}

janderson215 avatar Jul 21 '20 19:07 janderson215

Can this look at the response as a whole. IE display message for 4XXs vs 5XX?

hamiltondanielb avatar Jul 26 '20 01:07 hamiltondanielb

Can this look at the response as a whole. IE display message for 4XXs vs 5XX?

Yep! You just have to set the state in the data fetching function based on the conditions you care about. In this case, you would do something like this if (response.status === 500) { setDataSourceMessage('Server Error') } else if (response.status === 200) { setDataSourceMessage('No Data Matching Parameters') }

So on and so forth…

Funny thing about error codes in my case…maybe somebody else can shed some light on my issue with HTTP error codes that is unrelated to Material Table. When I return a 4xx or 5xx code from the dev build of my API server (node/express), I can inspect the response body to get a more specific error message that I'm returning from my API server, but my production build does not return a body. So if you run into that issue, what I did is just a total hack job and I'm returning a 200 status with the body { error: 'Specific error message }

janderson215 avatar Jul 26 '20 02:07 janderson215

@janderson215 https://github.com/mbrn/material-table/pull/2241#event-3587401366 this should help with this. You can reject inside the promise and return a message something like

 data={(query: any) => {
              const qs = switchPage(query.page);
              const filter = applyFilters(query);
              return new Promise((resolve, reject) => {
                getTechicalAssessments(query.pageSize, filter, qs)
                  .then(technicalAssessmmentsData => {
                    if (
                      !technicalAssessmmentsData ||
                      !technicalAssessmmentsData.data
                    ) {
                      reject(
                        "No data was found with the query parameters selected. Please refine the search and try again",
                      );
                    }
                    resolve({
                      data: technicalAssessmmentsData.data,
                      page: query.page,
                      totalCount: technicalAssessmmentsData.originalTotalCount,
                    });
                  })
                  .catch(err => {
//4XX or 5XX
                    reject({
                      message:
                        "An unexpected error was encountered, please try again",
                    });
                  });

hamiltondanielb avatar Jul 29 '20 14:07 hamiltondanielb