简体   繁体   中英

react-bootstrap-table search on nested fields

Hello I'm using the react-bootstrap-table module to display some data in my page and I want to use the search function to filter the results by specific columns. Here's a snippet of my render function:

render() {
    function showOSName(cell, row) {
      return cell.name;
    }

    function showBatteryCondition(cell, row) {
      return cell.condition;
    }

    var selectRowProp = {
      mode: "checkbox", 
      bgColor: "rgb(204, 230, 255)" 
    };  

    var tableOptions = {
        sizePerPage: 5,
        deleteText: "✗ Delete Selected",
        paginationSize: 3,
        clearSearch: true
    };

    return (
        <BootstrapTable
            data={this.state.data.systems}
            striped={true}
            hover={true}
            pagination={true}
            selectRow={selectRowProp}
            deleteRow={true}
            multiColumnSearch={true}
            search={true}
            ignoreSinglePage={true}
            options={tableOptions}
            >
          <TableHeaderColumn dataField="_id" isKey={true} dataAlign="center" 
            dataSort={true} searchable={false}>ID</TableHeaderColumn>
          <TableHeaderColumn dataField="model" dataAlign="center" 
            dataSort={true}>Model</TableHeaderColumn>
          <TableHeaderColumn dataField="serialnumber" dataAlign="center"
            searchable={false}>Serial Number</TableHeaderColumn>
          <TableHeaderColumn dataField="os" dataAlign="center" dataSort={true} 
            dataFormat={showOSName}>OS</TableHeaderColumn>
          <TableHeaderColumn dataField="battery" dataAlign="center" dataSort={true} 
            dataFormat={showBatteryCondition}>Battery Condition</TableHeaderColumn>
        </BootstrapTable>
    )
}

The OS and Battery Condition fields are nested values as in:

(example data)

id:           "ABC123"
model:        "modelName"
serialnumber: "1223334444"
os: {
     name: "Linux"
}
battery: {
     condition: "Good"
}

I use the dataFormat attribute to show the info I need in these columns. I also want to be able to make searches by the following columns: model, os & battery condition. Everything works fine when I search by model, but whenever I try to filter by the nested fields it yields no results. Can anyone help me out? Thank you!

I was able to do it by adding a filterValue attribute to each column and passing the same function I used for the dataFormat.

function filterFunction(cell, row) {
    // just return type for filtering or searching.
    return cell.type;
}

// add this attribute to TableHeaderColumn: filterValue={ filterFunction }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM