Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React DataTable value from Nested Array

Please look at my code:

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';

class App extends Component {
  constructor() {
    super();
    this.state = {
        student: [  
          {  
             "studentName":"Jack",
             "stdCtExamMarks":[  
                {  
                   "subjectName":"English "
                }
             ]
          },
          {  
             "studentName":"John",
             "stdCtExamMarks":[  
                {  
                   "subjectName":"Bangla "
                }
             ]
          }
       ]
    };
}
  render() {

    return (
      <div className="App">
    <BootstrapTable data={this.state.student} >
      <TableHeaderColumn dataField="studentName" isKey={true} dataAlign="center" dataSort={true}>Name</TableHeaderColumn>
      <TableHeaderColumn dataField="stdCtExamMarks.subjectName" dataSort={true}>Subject</TableHeaderColumn>

  </BootstrapTable>
      </div>
    );
  }
}

export default App;

In the Table I can get the value of studentName , but i need the value subjectName as well. How can i do that?

"studentName" simply works when i put it in the dataField but the nested array of "stdCtExamMarks" doesn't throw any value in the field

like image 300
todd Avatar asked Dec 01 '25 06:12

todd


1 Answers

I haven't used this control before, but based on the docs you might be able to use the dataFormat attribute of TableHeaderColumn. Something like this:

subjectFormatter(cell, row) {
  return cell[0].subjectName
}

render(){
  return (
    ...
    <TableHeaderColumn dataField="stdCtExamMarks" dataFormat={this.subjectFormatter} dataSort={true}>Subject</TableHeaderColumn>
    ...
  )
}

Just note that the stdCtExamMarks is an array as you have described it above, so I have used index 0, but you might want to get something different.

like image 141
Matt Way Avatar answered Dec 03 '25 20:12

Matt Way



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!