File size: 6,289 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import React from 'react';
import ReactDOM from 'react-dom';
import MUIDataTable from '../../src';
import { CircularProgress, Typography } from '@mui/material';
import Cities from './cities';
class Example extends React.Component {
state = {
page: 0,
count: 26,
data: [['Loading Data...']],
sortOrder: {},
loading: false,
};
componentDidMount() {
this.getData();
}
getData = () => {
this.setState({ loading: true });
this.xhrRequest().then(data => {
this.setState({ data, loading: false });
});
};
// mock async function
xhrRequest = url => {
let page = 0;
let order = '';
let column = '';
if (url != undefined) {
page = url.page;
order = url.order;
column = url.column;
}
return new Promise((resolve, reject) => {
const srcData = [
['Gabby George', 'Business Analyst', 'Minneapolis', 10, '$210,000'],
['Aiden Lloyd', 'Business Consultant', 'Dallas', 13, '$250,000'],
['Jaden Collins', 'Attorney', 'Santa Ana', 20, '$310,000'],
['Franky Rees', 'Business Analyst', 'St. Petersburg', 31, '$290,000'],
['Aaren Rose', 'Business Analyst', 'Toledo', 61, '$510,000'],
['Frankie Parry', 'Agency Legal Counsel', 'Jacksonville', 71, '$210,000'],
['Lane Wilson', 'Commercial Specialist', 'Omaha', 19, '$65,000'],
['Robin Duncan', 'Business Analyst', 'Los Angeles', 20, '$77,000'],
['Mel Brooks', 'Business Consultant', 'Oklahoma City', 37, '$135,000'],
['Harper White', 'Attorney', 'Pittsburgh', 52, '$420,000'],
['Kris Humphrey', 'Agency Legal Counsel', 'Laredo', 30, '$150,000'],
['Frankie Long', 'Industrial Analyst', 'Austin', 31, '$170,000'],
['Brynn Robbins', 'Business Analyst', 'Norfolk', 22, '$90,000'],
['Justice Mann', 'Business Consultant', 'Chicago', 24, '$133,000'],
['Jesse Welch', 'Agency Legal Counsel', 'Seattle', 28, '$200,000'],
['Eli Mejia', 'Commercial Specialist', 'Long Beach', 65, '$400,000'],
['Gene Leblanc', 'Industrial Analyst', 'Hartford', 34, '$110,000'],
['Danny Leon', 'Computer Scientist', 'Newark', 60, '$220,000'],
['Lane Lee', 'Corporate Counselor', 'Cincinnati', 52, '$180,000'],
['Jesse Hall', 'Business Analyst', 'Baltimore', 44, '$99,000'],
['Danni Hudson', 'Agency Legal Counsel', 'Tampa', 37, '$90,000'],
['Terry Macdonald', 'Commercial Specialist', 'Miami', 39, '$140,000'],
['Justice Mccarthy', 'Attorney', 'Tucson', 26, '$330,000'],
['Silver Carey', 'Computer Scientist', 'Memphis', 47, '$250,000'],
['Franky Miles', 'Industrial Analyst', 'Buffalo', 49, '$190,000'],
['Glen Nixon', 'Corporate Counselor', 'Arlington', 44, '$80,000'],
];
// here we're faking the sorting that would happen on the server-side
var offset = page * 10;
var data = [];
if (order !== '') {
var sortCol = ['Name', 'Title', 'Location', 'Age', 'Salary'].indexOf(column);
if (sortCol === -1) sortCol = 3;
if (order === 'asc') {
var tempData = srcData.sort((a, b) => {
if ( a[sortCol] < b[sortCol] ) return -1;
if ( a[sortCol] > b[sortCol] ) return 1;
return 0;
});
data =
offset + 10 >= srcData.length
? tempData.slice(offset, srcData.length)
: tempData.slice(offset, offset + 10);
} else {
tempData = srcData.sort((a, b) => {
if ( a[sortCol] < b[sortCol] ) return 1;
if ( a[sortCol] > b[sortCol] ) return -1;
return 0;
});
data =
offset + 10 >= srcData.length
? tempData.slice(offset, srcData.length)
: tempData.slice(offset, offset + 10);
}
} else {
data =
offset + 10 >= srcData.length ? srcData.slice(offset, srcData.length) : srcData.slice(offset, offset + 10);
}
setTimeout(() => {
resolve(data);
}, 250);
});
};
sort = (column, order) => {
let temp = {};
temp.column = column;
temp.order = order;
temp.page = this.state.page;
this.xhrRequest(temp).then(data => {
this.setState({
data,
sortOrder: {
name: column,
direction: order
}
});
});
};
render() {
const columns = [
{
name: 'Name',
options: {
customFilterListOptions: {
render: v => `Name: ${v}`,
}
},
},
{
name: 'Title',
options: {
customFilterListOptions: {
render: v => `Title: ${v}`,
}
},
},
{
name: 'Location',
options: {
customFilterListOptions: {
render: v => `Location: ${v}`,
},
customBodyRender: (value, tableMeta, updateValue) => {
return <Cities value={value || ''} index={tableMeta.columnIndex} change={event => updateValue(event)} />;
},
},
},
{
name: 'Age',
},
{
name: 'Salary',
},
];
const { page, count, data } = this.state;
const options = {
filter: true,
filterType: 'dropdown',
responsive: 'standard',
serverSide: true,
count: count,
page: page,
onColumnSortChange: (changedColumn, direction) => {
let order = 'desc';
if (direction === 'asc') {
order = 'asc';
}
this.sort(changedColumn, order);
},
onChangePage: (page) => {
this.setState({page}, () => {
this.sort(this.state.sortOrder.name, this.state.sortOrder.direction);
});
}
};
return (
<div>
<MUIDataTable
title={
<Typography variant="subtitle2">
ACME Employee list{' '}
{this.state.loading && (
<CircularProgress size={24} style={{ marginLeft: 15, position: 'relative', top: 4 }} />
)}
</Typography>
}
data={data}
columns={columns}
options={options}
/>
</div>
);
}
}
export default Example;
|