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

How to display multiple items in single cell of each row?

Open dashName opened this issue 7 years ago • 8 comments

I have this array of data: Products:Array(102)

0:{id:’1’,productDetail:Array(5) > 0:{title:’item-1’,code:’11’}1:{title:’item-2’,code:’12’}} 1:{id:’2’,productDetail:Array(4) > 0:{title:’item-1-1’,code:’11’}…..so on} ….. ….. …..

I want to display in my table one column header as items and display

Screen Shot 2019-11-19 at 9 51 09 AM

dashName avatar Nov 19 '19 17:11 dashName

Hi, could you send a sample of your Products array? Don't need to be the full array only 10 records are fine, with that I can try to reproduce the behavior that you expect.

Ymbere avatar Nov 20 '19 00:11 Ymbere

Hi, could you send a sample of your Products array? Don't need to be the full array only 10 records are fine, with that I can try to reproduce the behavior that you expect.

{ "data": [ { "id": "1", "product": [ { "title": "book-1", }, { "title": "book-2", }, { "title": "book-3", } ], }, { "id": "2", "product": [ { "title": "note-1", }, { "title": "note-1", } ], }, { "id": "3", "product": [ { "title": "pen-1", }, { "title": "pen-2", } ], },{ "id": "4", "product": [ { "title": "copy-1", }, { "title": "copy-2", }, { "title": "copy-1", }, { "title": "copy-2", }, { "title": "copy-1", }, { "title": "copy-2", } ], } { "id": "5", "product": [ { "title": "name-1", }, { "title": "name-2", }, { "title": "name-3", } ], },

] }

id item


Screen Shot 2019-11-19 at 5 11 45 PM

will this work? @Ymbere

dashName avatar Nov 20 '19 00:11 dashName

Hi, could you send a sample of your Products array? Don't need to be the full array only 10 records are fine, with that I can try to reproduce the behavior that you expect.

{ "data": [ { "id": "1", "product": [ { "title": "book-1", }, { "title": "book-2", }, { "title": "book-3", } ], }, { "id": "2", "product": [ { "title": "note-1", }, { "title": "note-1", } ], }, { "id": "3", "product": [ { "title": "pen-1", }, { "title": "pen-2", } ], },{ "id": "4", "product": [ { "title": "copy-1", }, { "title": "copy-2", }, { "title": "copy-1", }, { "title": "copy-2", }, { "title": "copy-1", }, { "title": "copy-2", } ], } { "id": "5", "product": [ { "title": "name-1", }, { "title": "name-2", }, { "title": "name-3", } ], },

] }

id item

Screen Shot 2019-11-19 at 5 11 45 PM

will this work? @Ymbere

I am trying something like below:

const productInfoFormatter =(cell) => { console.log(cell); if(cell.length === 1){ return ( cell[0].title ) }else { cell.map(x => { console.log(x.title) return ( x.title ) }) }

    };

it works when cell.length is 1 but not for multiple

dashName avatar Nov 21 '19 18:11 dashName

Hi @dashName, could you create a Code Box with your code? With that, it's easier to help you.

Ymbere avatar Nov 21 '19 23:11 Ymbere

Hi @dashName, could you create a Code Box with your code? With that, it's easier to help you.

https://codesandbox.io/s/pedantic-allen-i2u7e @Ymbere

dashName avatar Nov 21 '19 23:11 dashName

Hi, @dashName your implementation is correct you just forgot a return statement, your code will look like this.

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    if (cell.length === 1) {
      return cell[0].title;
    } else {
      // Return here was missing
      return cell.map(x => {
        console.log(x.title);
        return x.title;
      });
    }
  };

Although you can make the code more clean removing the first if statment

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    return cell.map(x => {
      console.log(x.title);
      return x.title;
    });
  };

Ymbere avatar Nov 22 '19 02:11 Ymbere

Hi, @dashName your implementation is correct you just forgot a return statement, your code will look like this.

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    if (cell.length === 1) {
      return cell[0].title;
    } else {
      // Return here was missing
      return cell.map(x => {
        console.log(x.title);
        return x.title;
      });
    }
  };

Although you can make the code more clean removing the first if statment

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    return cell.map(x => {
      console.log(x.title);
      return x.title;
    });
  };
69200394-d843b980-0aef-11ea-84a2-51e3b5933bc0

to display something like like can we add a condition to check the length of array and if the length is > 2 can display + rest of items?

dashName avatar Nov 22 '19 02:11 dashName

Hi, @dashName your implementation is correct you just forgot a return statement, your code will look like this.

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    if (cell.length === 1) {
      return cell[0].title;
    } else {
      // Return here was missing
      return cell.map(x => {
        console.log(x.title);
        return x.title;
      });
    }
  };

Although you can make the code more clean removing the first if statment

  const productInfo = (cell, row, rowIndex, formatExtraData) => {
    console.log(cell);
    return cell.map(x => {
      console.log(x.title);
      return x.title;
    });
  };
69200394-d843b980-0aef-11ea-84a2-51e3b5933bc0

to display something like like can we add a condition to check the length of array and if the length is > 2 can display + rest of items?

Yes I am able to do this thanks

dashName avatar Nov 22 '19 03:11 dashName