Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public exportTable = (filtered: any) => {
- const { dataSource, filteredRecordCount, totalRecordCount, searchText } = this.state;
- const header = dataSource.columns.map((x: any) => x.Label);
- const visibility = dataSource.columns.map((x: any) => x.Visible);
- let count;
- let search;
- const processRow = (row: any) => {
- if (row instanceof Object) {
- row = Object.keys(row).map((key: any) => row[key]);
- }
- let finalVal = '';
- for (let i = 0; i < row.length; i++) {
- if (visibility[i]) {
- let innerValue = row[i] === null || row[i] === undefined ? '' : row[i].toString();
- if (moment(row[i], moment.ISO_8601, true).isValid()) {
- innerValue = moment(row[i]).format('MMMM Do YYYY, h:mm:ss a');
- }
- let result = innerValue.replace(/"/g, '""');
- if (result.search(/("|,|\n)/g) >= 0) {
- result = `"${result}"`;
- }
- if (i > 0) {
- finalVal += ',';
- }
- finalVal += result;
- }
- }
- return `${finalVal}\n`;
- };
- let csvFile = '';
- if (header.length > 0) {
- csvFile += processRow(header);
- }
- if (filtered) {
- count = filteredRecordCount;
- search = searchText;
- } else {
- count = totalRecordCount;
- search = '';
- }
- dataSource.getAllRecords(count, 0, search)
- .then(({ Payload }: any) => {
- Payload.forEach((row: any) => {
- csvFile += processRow(row);
- });
- }).then(() => {
- const blob = new Blob([`\uFEFF${csvFile}`], {
- type: 'text/csv;charset=utf-8;'
- });
- const fileURL = URL.createObjectURL(blob);
- const downloadLink = document.createElement('a');
- downloadLink.setAttribute('href', fileURL);
- downloadLink.setAttribute('download', 'data.csv');
- downloadLink.click();
- URL.revokeObjectURL(fileURL);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement