Repository: gregnb/mui-datatables
Pull request #1034 (2019-10-25 20:03:35)

Fix download row order when using display true options
Closes https://github.com/gregnb/mui-datatables/issues/1033.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/26557337/badge)](https://coveralls.io/builds/26557337)

Coverage decreased (-0.05%) to 77.308% when pulling **ea2ec1fb77c7498eaaa9e1bd22aa49dd7ae02a44 on bugfix/issue-1033-preserve-display-order-for-download** into **86deadf48722453db45f68116638dee1c627f9b5 on master**.


Patch:
diff --git a/src/components/TableToolbar.js b/src/components/TableToolbar.js
index 0c9b6e6..ca32a64 100644
--- a/src/components/TableToolbar.js
+++ b/src/components/TableToolbar.js
@@ -13,6 +13,7 @@ import PrintIcon from '@material-ui/icons/Print';
 import ViewColumnIcon from '@material-ui/icons/ViewColumn';
 import FilterIcon from '@material-ui/icons/FilterList';
 import ReactToPrint from 'react-to-print';
+import find from 'lodash.find';
 import { withStyles } from '@material-ui/core/styles';
 import { createCSVDownload } from '../utils';
 
@@ -109,9 +110,10 @@ class TableToolbar extends React.Component {
               i += 1;
 
               // if we have a custom render, which will appear as a react element, we must grab the actual value from data
+              // that matches the dataIndex and column
               // TODO: Create a utility function for checking whether or not something is a react object
               return typeof column === 'object' && column !== null && !Array.isArray(column)
-                ? data[row.index].data[i]
+                ? find(data, d => d.index === row.dataIndex).data[i]
                 : column;
             }),
           };
diff --git a/src/utils.js b/src/utils.js
index 0e8fcb6..530ef0d 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -6,9 +6,10 @@ function buildMap(rows) {
 }
 
 function getPageValue(count, rowsPerPage, page) {
-  const totalPages = count === rowsPerPage ? 0 : Math.floor(count / rowsPerPage);
+  const totalPages = count <= rowsPerPage ? 1 : Math.ceil(count / rowsPerPage);
 
-  return page > totalPages ? totalPages : page;
+  // `page` is 0-indexed
+  return page >= totalPages ? totalPages - 1 : page;
 }
 
 function getCollatorComparator() {


Test patch:
diff --git a/test/utils.test.js b/test/utils.test.js
new file mode 100644
index 0000000..0d2311a
--- /dev/null
+++ b/test/utils.test.js
@@ -0,0 +1,60 @@
+import { getPageValue } from '../src/utils';
+import { assert } from 'chai';
+
+describe('utils.js', () => {
+  describe('getPageValue', () => {
+    it('returns the highest in bounds page value when page is out of bounds and count is greater than rowsPerPage', () => {
+      const count = 30;
+      const rowsPerPage = 10;
+      const page = 5;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 2);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is greater than rowsPerPage', () => {
+      const count = 30;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 1);
+    });
+
+    it('returns the highest in bounds page value when page is out of bounds and count is less than rowsPerPage', () => {
+      const count = 3;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is less than rowsPerPage', () => {
+      const count = 3;
+      const rowsPerPage = 10;
+      const page = 0;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is out of bounds and count is equal to rowsPerPage', () => {
+      const count = 10;
+      const rowsPerPage = 10;
+      const page = 1;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+
+    it('returns the highest in bounds page value when page is in bounds and count is equal to rowsPerPage', () => {
+      const count = 10;
+      const rowsPerPage = 10;
+      const page = 0;
+
+      const actualResult = getPageValue(count, rowsPerPage, page);
+      assert.strictEqual(actualResult, 0);
+    });
+  });
+});