Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up on custom data accessor #418 #508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions demo/js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function addProducts(quantity) {
price: 100 + i,
supplierId: id+2,
discount: "10%",
categoryId: "catorage-"+id+6
category: {
id: "catorage-"+id+6,
},
});
}
}
Expand Down Expand Up @@ -92,7 +94,7 @@ React.render(
<TableHeaderColumn dataField="price" width="100px" dataFormat={priceFormatter} editable={false}>Product Price</TableHeaderColumn>
<TableHeaderColumn dataField="supplierId" editable={true}>Supplier ID</TableHeaderColumn>
<TableHeaderColumn dataField="discount" editable={false}>Discount(Percentage)</TableHeaderColumn>
<TableHeaderColumn dataField="categoryId" editable={true}>Category ID</TableHeaderColumn>
<TableHeaderColumn dataField="categoryId" dataAccess={(item) => item.category.id} editable={true}>Category ID</TableHeaderColumn>
</BootstrapTable>,
document.getElementById("basic")
);
1 change: 1 addition & 0 deletions examples/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const routes = (
<Route path='getting-started' component={ GettingStarted }/>
<Route path='examples'>
<Route path='basic' component={ require('./basic/demo') } />
<Route path='custom-data-access' component={ require('./custom-data-access/demo') } />
<Route path='column' component={ require('./column/demo') } />
<Route path='sort' component={ require('./sort/demo') } />
<Route path='column-format' component={ require('./column-format/demo') } />
Expand Down
3 changes: 3 additions & 0 deletions examples/js/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class App extends React.Component {
const examples = [ {
text: 'Basic Table',
href: 'basic'
}, {
text: 'Custom Data Access',
href: 'custom-data-access'
}, {
text: 'Work on Column',
href: 'column'
Expand Down
41 changes: 41 additions & 0 deletions examples/js/custom-data-access/custom-data-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint max-len: 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,
category: {
id: 20 + i,
name: 'Category name ' + (quantity + i)
}
});
}
}

addProducts(5);

function categoryName(product) {
return product.category.name;
}

export default class TrClassStringTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } search={ true } >
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price' dataAccess={ categoryName } filter={ { type: 'TextFilter' } }>Category name</TableHeaderColumn>
</BootstrapTable>
);
}
}

24 changes: 24 additions & 0 deletions examples/js/custom-data-access/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint max-len: 0 */
import React from 'react';
import CustomDataAccessTable from './custom-data-access';

class Demo extends React.Component {
render() {
return (
<div>
<div className='col-md-offset-1 col-md-8'>
<div className='panel panel-default'>
<div className='panel-heading'>Use a custom function to access data</div>
<div className='panel-body'>
<h5>Source in /examples/js/custom-data-access/custom-data-access.js</h5>
<CustomDataAccessTable />
</div>
</div>
</div>
</div>
);
}
}

export default Demo;

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"dependencies": {
"classnames": "^2.1.2",
"react-toastr": "^2.3.1"
"react-toastr": "2.4.0"
},
"peerDependencies": {
"react": "^0.14.3 || ^15.0.0"
Expand Down
1 change: 1 addition & 0 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class BootstrapTable extends Component {
return React.Children.map(children, (column, i) => {
return {
name: column.props.dataField,
dataAccess: column.props.dataAccess,
align: column.props.dataAlign,
sort: column.props.dataSort,
format: column.props.dataFormat,
Expand Down
5 changes: 4 additions & 1 deletion src/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class TableBody extends Component {

const tableRows = this.props.data.map(function(data, r) {
const tableColumns = this.props.columns.map(function(column, i) {
const fieldValue = data[column.name];
const fieldValue = typeof column.dataAccess === 'function' ?
column.dataAccess(data) :
data[column.name];

if (this.editing &&
column.name !== this.props.keyField && // Key field can't be edit
column.editable && // column is editable? default is true, user can set it false
Expand Down
6 changes: 4 additions & 2 deletions src/TableHeaderColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ TableHeaderColumn.propTypes = {
headerAlign: PropTypes.string,
dataSort: PropTypes.bool,
onSort: PropTypes.func,
dataFormat: PropTypes.func,
csvFormat: PropTypes.func,
csvHeader: PropTypes.string,
dataAccess: PropTypes.func,
dataFormat: PropTypes.func,
isKey: PropTypes.bool,
editable: PropTypes.any,
hidden: PropTypes.bool,
Expand Down Expand Up @@ -168,9 +169,10 @@ TableHeaderColumn.defaultProps = {
dataAlign: 'left',
headerAlign: undefined,
dataSort: false,
dataFormat: undefined,
csvFormat: undefined,
csvHeader: undefined,
dataAccess: undefined,
dataFormat: undefined,
isKey: false,
editable: true,
onSort: undefined,
Expand Down
10 changes: 7 additions & 3 deletions src/store/TableDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ export class TableDataStore {
let valid = true;
let filterVal;
for (const key in filterObj) {
let targetVal = row[key];
let targetVal = typeof this.colInfos[key].dataAccess === 'function' ?
this.colInfos[key].dataAccess(row) :
row[key];
if (targetVal === null) return false;

switch (filterObj[key].type) {
Expand Down Expand Up @@ -425,7 +427,7 @@ export class TableDataStore {
if (this.colInfos[key]) {
const { format, filterFormatted, formatExtraData } = this.colInfos[key];
if (filterFormatted && format) {
targetVal = format(row[key], row, formatExtraData);
targetVal = format(targetVal, row, formatExtraData);
}
}

Expand Down Expand Up @@ -478,7 +480,9 @@ export class TableDataStore {
const key = keys[i];
if (this.colInfos[key] && row[key]) {
const { format, filterFormatted, formatExtraData, searchable } = this.colInfos[key];
let targetVal = row[key];
let targetVal = typeof this.colInfos[key].dataAccess === 'function' ?
this.colInfos[key].dataAccess(row) :
row[key];
if (searchable) {
if (filterFormatted && format) {
targetVal = format(targetVal, row, formatExtraData);
Expand Down