react-bootstrap-table2 icon indicating copy to clipboard operation
react-bootstrap-table2 copied to clipboard

How to create relation between two bootstrap next tables?

Open tesicg opened this issue 8 years ago • 3 comments

I would like to have two tables that should be in connection. The first one contains orders, and the second one contains products. The orders (Order class) among other properties have an Id. The products (Product class) also have an Id among other properties, and reference to orders, which is OrderId property.

I'm able to populate orders table by using:

componentDidMount() {
  axios.get("http://localhost:1234/api/orders").then(response => {
    console.log(response.data);
    this.setState({
      orders: response.data
    });
  });
}

But, I'm not sure what to do next. I suppose I should use react-router, but in which way?

I did the following with columns:

columns: [{
  dataField: 'Id',
  text: 'Order ID',
  formatter: this.idFormatter
}, {
  dataField: 'Date',
  text: 'Date',
  formatter: this.dateFormatter
}, {
  dataField: 'Total',
  text: 'Total'
}],

idFormatter = (cell, row) => {
  return (
    <Link to={`/api/products/${row.Id}`}>
      <span>#{cell}</span>
    </Link>
  )
}

That is when I click the cell with Id in order's table, the table with products should display only products for that order's Id. Without postback of course.

I'm not sure if I should use the second api controller - products, that has one method:

public List<Product> GetProductsByOrderId(int id)
{
    return _products.Where(product => product.OrderId == id).ToList();
}

I appreciate any help.

tesicg avatar Jul 15 '18 06:07 tesicg

I think there's no any best answer for this question, in your case, when user click the order, you have to populate the corresponding products via a API call. then feed the product data into product table, I think that's it.

I'm not sure why you need to use react-route, is that because you have to change to the other page when user want to see the product per order? if yes, I think you can use react-router, in this case, you still have other API to fetch the products.

Close this issue due to no it's not an issue about react-bootstrap-table but we can still discuss here :)

AllenFang avatar Jul 15 '18 06:07 AllenFang

Thank you. I did it in first way you'd suggested.

tesicg avatar Jul 15 '18 08:07 tesicg

Good job 👍

AllenFang avatar Jul 15 '18 09:07 AllenFang