0

I have a data store in state which needs to be displayed in a tabular format in UI using React. I am using a map function to display the data. I am able to display the data but the header is being displayed after each row as they are iterated in a loop.

Please let me know how can i display the output in tabular format with header being displayed only once?

return method code in React:-

return (
      <div>
        <br />
        <input
          type="submit"
          class="btn btn-info btn-sm"
          value="Create Itinerary"
          onClick={this.callItinAPI}
        />
        <br />
        <br />

        {this.state.ItinData.map((item) => (
          <div>
            <table id="DisplayRequest">
              <tr>
                <th>TripName</th>
                <th>Booking ID</th>
                <th>Start Date</th>
                <th>End Date</th>
              </tr>

              <tr>
                <td>{item.Itinerary.TripName._text}</td>
                <td>{item.Itinerary.Bookings.Booking.RecordLocator._text}</td>
                <td>{item.Itinerary.StartDateLocal._text}</td>
                <td>{item.Itinerary.EndDateLocal._text}</td>
              </tr>
            </table>
          </div>
        ))}
      </div>
    );

Current code output

Expected output:- Table header to displayed only once followed by all the data rows

3 Answers 3

1

You just need to extract the header from your loop :

<div>
  {this.state.ItinData.length > 0 ? (
    <table id={'DisplayRequest'}>
      <tr>
        <th>TripName</th>
        <th>Booking ID</th>
        <th>Start Date</th>
        <th>End Date</th>
      </tr>
      {this.state.ItinData.map((item) => (
        <tr>
          <td>{item.Itinerary.TripName._text}</td>
          <td>{item.Itinerary.Bookings.Booking.RecordLocator._text}</td>
          <td>{item.Itinerary.StartDateLocal._text}</td>
          <td>{item.Itinerary.EndDateLocal._text}</td>
        </tr>
      ))}
    </table>;
  ) : null}
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

The table is displayed based on Create Itinerary event. If i extract out of loop, it will be visible without event. Please assist.
it solved but i see default 0 being displayed. How can i hide it?
@aquinq- The issue is resolved but i see dummy value as 0 being displayed
Ok so maybe the ternary was not explicit enough, you can try with this new edit.
0
return (
    <div>
      <br />
      <input
        type="submit"
        class="btn btn-info btn-sm"
        value="Create Itinerary"
        onClick={this.callItinAPI}
      />
      <br />
      <br />
        <div>
          <table id="DisplayRequest">
            <tr>
              <th>TripName</th>
              <th>Booking ID</th>
              <th>Start Date</th>
              <th>End Date</th>
            </tr>
            {this.state.ItinData.map((item) => (

              <tr>
                <td>{item.Itinerary.TripName._text}</td>
                <td>{item.Itinerary.Bookings.Booking.RecordLocator._text}</td>
                <td>{item.Itinerary.StartDateLocal._text}</td>
                <td>{item.Itinerary.EndDateLocal._text}</td>
              </tr>
            ))}
          </table>
        </div>
    </div>
  );

just move that td out side the loop

Comments

0

The issue with your code is, you have put the table header in the loop itself which is wrong. Try something like this:

{
    ( this.state.ItinData.length > 0 )  // conditional rendering if data exist
    ?
    <table id={'DisplayRequest'}>
        <tr>
            <th>TripName</th>
            <th>Booking ID</th>
            <th>Start Date</th>
            <th>End Date</th>
        </tr>
        // Use the map() function to iterate the array and display the table body here
        {this.state.ItinData.map((item) => (
        <tr>
          <td>{item.Itinerary.TripName._text}</td>
          <td>{item.Itinerary.Bookings.Booking.RecordLocator._text}</td>
          <td>{item.Itinerary.StartDateLocal._text}</td>
          <td>{item.Itinerary.EndDateLocal._text}</td>
        </tr>
      ))}
    </table>
    null
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.