-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathcell-edit-hook-table.js
73 lines (61 loc) · 2.16 KB
/
cell-edit-hook-table.js
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
/* eslint max-len: 0 */
/* eslint no-alert: 0 */
/* eslint guard-for-in: 0 */
/* eslint no-unused-vars: 0 */
import React from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
const products = [];
function addProducts(quantity) {
const startId = products.length;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: 'Item name ' + id,
price: 2100 + i
});
}
}
addProducts(5);
function onAfterSaveCell(row, cellName, cellValue) {
alert(`Save cell ${cellName} with value ${cellValue}`);
let rowStr = '';
for (const prop in row) {
rowStr += prop + ': ' + row[prop] + '\n';
}
alert('The whole row :\n' + rowStr);
}
function onBeforeSaveCell(row, cellName, cellValue) {
// You can do any validation on here for editing value,
// return false for reject the editing
return true;
}
function onBeforeSaveCellAsync(row, cellName, cellValue, done) {
// if your validation is async, for example: you want to pop a confirm dialog for user to confim
// in this case, react-bootstrap-table pass a callback function to you
// you are supposed to call this callback function with a bool value to perfom if it is valid or not
// in addition, you should return 1 to tell react-bootstrap-table this is a async operation.
// I use setTimeout to perform an async operation.
// setTimeout(() => {
// done(true); // it's ok to save :)
// done(false); // it's not ok to save :(
// }, 3000);
// return 1; // please return 1
}
const cellEditProp = {
mode: 'click',
blurToSave: true,
beforeSaveCell: onBeforeSaveCell, // a hook for before saving cell
afterSaveCell: onAfterSaveCell // a hook for after saving cell
};
export default class BlurToSaveTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } cellEdit={ cellEditProp }>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
);
}
}