-
Notifications
You must be signed in to change notification settings - Fork 85
/
ReactTabulatorExample.tsx
211 lines (196 loc) · 6.93 KB
/
ReactTabulatorExample.tsx
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
211
import * as React from 'react';
// for styles:
// import 'react-tabulator/lib/styles.css'; // default theme
// import 'react-tabulator/css/bootstrap/tabulator_bootstrap.min.css'; // use Theme(s)
import ReactTabulator, { ReactTabulatorOptions, ColumnDefinition } from './ReactTabulator';
import DateEditor from './editors/DateEditor';
import MultiSelectEditor from './editors/MultiSelectEditor';
import MultiValueFormatter from './formatters/MultiValueFormatter';
import { reactFormatter } from './Utils';
function SimpleButton(props: any) {
const rowData = props.cell._cell.row.data;
const cellValue = props.cell._cell.value || 'Edit | Show';
return <button onClick={() => alert(rowData.name)}>{cellValue}</button>;
}
const columns: ColumnDefinition[] = [
{ title: 'Name', field: 'name', width: 150 },
{ title: 'Age', field: 'age', hozAlign: 'left', formatter: 'progress' },
{ title: 'Favourite Color', field: 'color' },
{ title: 'Date Of Birth', field: 'dob', sorter: 'date' },
{ title: 'Rating', field: 'rating', hozAlign: 'center', formatter: 'star' },
{ title: 'Passed?', field: 'passed', hozAlign: 'center', formatter: 'tickCross' },
{ title: 'Custom', field: 'custom', hozAlign: 'center', editor: 'input', formatter: reactFormatter(<SimpleButton />) }
];
const data = [
{ id: 1, name: 'Oli Bob', age: '12', color: 'red', dob: '01/01/1980', rating: 5, passed: true, pets: ['cat', 'dog'] },
{ id: 2, name: 'Mary May', age: '1', color: 'green', dob: '12/05/1989', rating: 4, passed: true, pets: ['cat'] },
{ id: 3, name: 'Christine Lobowski', age: '42', color: 'green', dob: '10/05/1985', rating: 4, passed: false },
{ id: 4, name: 'Brendon Philips', age: '125', color: 'red', dob: '01/08/1980', rating: 4.5, passed: true },
{ id: 5, name: 'Margret Marmajuke', age: '16', color: 'yellow', dob: '07/01/1999', rating: 4, passed: false },
{
id: 6,
name: 'Van Ng',
age: '37',
color: 'green',
dob: '06/10/1982',
rating: 4,
passed: true,
pets: ['dog', 'fish']
},
{ id: 7, name: 'Duc Ng', age: '37', color: 'yellow', dob: '10/10/1982', rating: 4, passed: true, pets: ['dog'] }
];
// Editable Example:
const colorOptions = { ['']: ' ', red: 'red', green: 'green', yellow: 'yellow' };
const petOptions = [
{ id: 'cat', name: 'cat' },
{ id: 'dog', name: 'dog' },
{ id: 'fish', name: 'fish' }
];
const editableColumns: any[] = [
{ title: 'Name', field: 'name', width: 150, editor: 'input', headerFilter: 'input' },
{ title: 'Age', field: 'age', hozAlign: 'left', formatter: 'progress', editor: 'star' },
{
title: 'Favourite Color',
field: 'color',
editor: 'select',
editorParams: { allowEmpty: true, showListOnEmpty: true, values: colorOptions },
headerFilter: 'select',
headerFilterParams: { values: colorOptions }
},
{ title: 'Date Of Birth', field: 'dob', editor: DateEditor, editorParams: { format: 'MM/DD/YYYY' } },
{
title: 'Pets',
field: 'pets',
sorter: (a: string[], b: string[]) => a.toString().localeCompare(b.toString()),
editor: MultiSelectEditor,
editorParams: { values: petOptions },
formatter: MultiValueFormatter,
formatterParams: { style: 'PILL' }
},
{ title: 'Passed?', field: 'passed', hozAlign: 'center', formatter: 'tickCross', editor: true }
];
export default () => {
const [state, setState] = React.useState<any>({
data: [],
selectedName: ''
});
let ref = React.useRef<any>();
const rowClick = (e: any, row: any) => {
console.log('ref table: ', ref.current); // this is the Tabulator table instance
// ref?.current && ref?.current.replaceData([])
console.log('rowClick id: ${row.getData().id}', row, e);
setState({ selectedName: row.getData().name });
};
const setData = () => {
setState({ data });
};
const clearData = () => {
setState({ data: [] });
};
const modifyData = () => {
const _newData = data.filter((item: any) => item.name === 'Oli Bob');
setState({ data: _newData });
};
const renderAjaxScrollExample = () => {
const columns = [
{ title: 'First Name', field: 'first_name', width: 150 },
{ title: 'Last Name', field: 'last_name', width: 150 },
{ title: 'Email', field: 'email', width: 150 }
];
const options = {
height: 100,
movableRows: true,
progressiveLoad: 'scroll',
progressiveLoadDelay: 200,
progressiveLoadScrollMargin: 30,
ajaxURL: 'https://reqres.in/api/users',
dataSendParams: {
page: 'page',
size: 'per_page'
},
dataReceiveParams: {
last_page: 'last'
},
paginationSize: 5,
ajaxResponse: (url, params, response) => {
console.log('url, params, response', url, params, response);
return {
data: response.data,
last: response.total_pages
};
},
ajaxError: function (error) {
console.log('ajaxError', error);
}
};
return (
<ReactTabulator
onRef={(r) => (ref = r)}
columns={columns}
options={options}
events={{
dataLoaded: function (data) {
console.log('dataLoaded', data);
// return data; //return the response data to tabulator
let modResponse: any = {};
modResponse.data = data;
modResponse.last = 5;
return modResponse;
},
ajaxError: function (error) {
console.log('ajaxError', error);
}
}}
/>
);
};
const options: ReactTabulatorOptions = {
height: 150,
movableRows: true,
movableColumns: true
};
return (
<div>
<ReactTabulator
onRef={(ref) => (ref = ref)}
columns={columns}
data={data}
events={{
rowClick: rowClick
}}
options={options}
data-custom-attr="test-custom-attribute"
className="custom-css-class"
/>
<i>
Selected Name: <strong>{state.selectedName}</strong>
</i>
<h3>
Asynchronous data: (e.g. fetch) - <button onClick={setData}>Set Data</button>{' '}
<button onClick={clearData}>Clear</button> <button onClick={modifyData}>Modify Data</button>
</h3>
<ReactTabulator columns={columns} data={state.data} />
<h3>Editable Table</h3>
<ReactTabulator
columns={editableColumns}
data={data}
cellEdited={(cell: any) => console.log('cellEdited', cell)}
dataChanged={(newData: any) => console.log('dataChanged', newData)}
footerElement={<span>Footer</span>}
options={{ movableColumns: true, movableRows: true }}
/>
<h3>Infinite Scrolling with Ajax Requests</h3>
{renderAjaxScrollExample()}
<p>
<a href="https://github.com/ngduc/react-tabulator" target="_blank">
Back to: Github Repo: react-tabulator
</a>
</p>
<p>
<a href="http://tabulator.info/examples/4.0" target="_blank">
More Tabulator's Examples
</a>
</p>
</div>
);
};