react-bootstrap-table: dataField can't access subobject
I have an object which is like that
{
"person_id": 1,
"username": "UserTest",
"created_at": "2015-09-10T21:13:56.000Z",
"updated_at": "2015-09-10T21:13:56.000Z",
"deleted_at": null,
"Person": {
"id": 1,
"firstname": "UserTest",
"lastname": "Obama",
"note": "DevOps",
"email": "iam@obama.com",
"created_at": "2015-09-10T21:13:56.000Z",
"updated_at": "2015-09-10T21:13:56.000Z",
"deleted_at": null
}
}
I can’t access the subojbect using
<TableHeaderColumn dataField="Person.firstname">Firstname</TableHeaderColumn>
Is there any workaround not specified in the doc ?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 25
- Comments: 51 (9 by maintainers)
sorry guys, as I mention on README, I already decide to create
react-bootstrap-table2
, I trust it will be better thanreact-bootstrap-table
. A good news is I’ll support the nested data inreact-bootstrap-table2
start fromv0.0.1
, bad news is I will not support the nested data inreact-bootstrap-table
.Feel free to let me know your idea and any idea/issues is welcome on
react-bootstrap-table2
Sorry for inconvenience.
I’m using lodash’s
_.get
lib to achieve this.Working example with the required field is the key. Thanks to ferrwanz and nivek91.
Then you need to add to the method render, next code:
I solved the issue without modification of the project 👍
<TableHeaderColumn dataField='Contact' dataFormat={ this.contactFormatter } >Contact</TableHeaderColumn>
and the function :
contactFormatter(cell, row){ return
${cell.first} ${cell.last}; }
building on @PraxentSupport’s response. I’m using:
function objectFormat(data, cell) { if(data.constructor===Array){ if(data[0]=='last'){ return <p>${cell[data.length-1][data[1]]}</p> } return <p>${cell[data[0]][data[1]]}</p> } return <p>${cell[data]}</p>; }
dataFormat={ objectFormat.bind(this, [0,'address'])}
ordataFormat={ objectFormat.bind(this, 'number')}
Note: need to ad ` around return values.
To people finding this thread: Using lodash works well with displaying data but the data presented will not be searchable or sortable.
@ferrwanz the key is missings in your code:
import _ from “lodash”;
const renderCustom = (cell, row, extra) => { const value = _.get(row, extra.path); return <div>{value}</div>; };
const columns = [ { path: “CPA”, title: “cpa”, isKey: true }, { path: “entreCalles.calleA”, title: “entreCa” } ];
const tableColumn = _.map(columns, column => ( <TableHeaderColumn dataField={column.path} dataFormat={renderCustom} formatExtraData={column} isKey={column.isKey} key={column.path}
</TableHeaderColumn> ));I was just about to add to this… Why can’t we simply make a PR that uses both the lodash
_.get
: https://lodash.com/docs/4.17.4#get and_.set
: https://lodash.com/docs/4.17.4#setWhen retrieving and setting values?
And this is quite common case if you are using graphql.However, you can easily hack it even without eval, google
js flattern object
you will get the answer.You can use
formatExtraData
to hackthen add colunms to
BootstrapTable
.I did have a look at the code and this seems complicated to implement the way it is made. I doubt this feature will be added in the futur.
using dataFormat to rewrite this cell .
Small workaround in the meantime is using the dataFormat.
Something like this works fine
Ok, all I can say is thanks for sharing the code. Wait & see
This feature is definitely welcomed. the fact that it does not support nested objects make it feel rather rigid in design. I hope to see this implemented soon =)
Hi @kopax, the nested object does not supported on newest version in this component, sorry for that. But I can add it in the future, but this is not in my plan, so I need more plan or thinking on this feature. 😃