javascript - React-Table: Show Nested Table for a selected rows nested data - Stack Overflow

How can I tell react-table to show a sub-ponentreact-table for a selected Rows nested Child Object?For

How can I tell react-table to show a sub-ponent/react-table for a selected Rows nested Child Object?

For example,

[
   {
      "objectA":{
         "field1":"field1Data",
         "nestedObjectA":{
            "nestedfield1":"nestedfield1Data",
            "nestedNestedObjectA":{
               "nestedNestedObjectA":"nestedNestedObjectA",
               "nestedNestedObjectB":"nestedNestedObjectB"
            }
         }
      }
   }
]

In the json above I will be creating react-table for objectA which will display its fields. Once the user selects objectA row, I want to show a nested table for the selected rows nested data.

In the examples given in react-table docs here or here doesnt contain much plex situations. The main concern here is to tell react-table that I have selected a row and for this selected rows data we have to create a separate table right underneath it. How can I achieve this? Thank you.

How can I tell react-table to show a sub-ponent/react-table for a selected Rows nested Child Object?

For example,

[
   {
      "objectA":{
         "field1":"field1Data",
         "nestedObjectA":{
            "nestedfield1":"nestedfield1Data",
            "nestedNestedObjectA":{
               "nestedNestedObjectA":"nestedNestedObjectA",
               "nestedNestedObjectB":"nestedNestedObjectB"
            }
         }
      }
   }
]

In the json above I will be creating react-table for objectA which will display its fields. Once the user selects objectA row, I want to show a nested table for the selected rows nested data.

In the examples given in react-table docs here or here doesnt contain much plex situations. The main concern here is to tell react-table that I have selected a row and for this selected rows data we have to create a separate table right underneath it. How can I achieve this? Thank you.

Share Improve this question edited Aug 22, 2019 at 0:45 Ahmed asked Aug 22, 2019 at 0:34 AhmedAhmed 3,2708 gold badges45 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

I figured it out myself. But still if anyone else has a more elegant solution please feel free to share.

Added a few lines to the example code in the links shared above. data and the row from subComponent have some significance in this.

In short I am calling a map function in data to check if id in row.original from subponent matches the id in data item.

In short,

data={rowData => rowData.map(item => { 
  if (item.id === row.original.id) {
    return item.nestedChild;
    } else {
      // Since an empty array is expected otherwise React-Table will //result in error.
      return []; 
    }
}

rowData is the actual data used as the source of the Nested React-Table. row.original is the data of the original item which can be used to check if the data matches.

Example,

<ReactTable
          data={data}
          columns={columns}
          defaultPageSize={10}
          className="-striped -highlight"
          SubComponent={row => {
            return (
              <div style={{ padding: "20px" }}>
                <em>
                  You can put any ponent you want here, even another React
                  Table!
                </em>
                <br />
                <br />
                <ReactTable
                    data={rowData =>
                    rowData.map(item => {
                      if (item.id === row.original.id) {
                        return item;
                      } else {
                        return [];
                      }
                    })
                  }
                  columns={columns}
                  defaultPageSize={3}
                  showPagination={false}
                  SubComponent={row => {
                    return (
                      <div style={{ padding: "20px" }}>
                        Another Sub Component!
                      </div>
                    );
                  }}
                />
              </div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630372a4637071.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信