How to make table header display once in map function in React JS? - reactjs

I'm displaying data in a tabular format in UI using React and TailwindCSS. 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?
React JS code:
{data && toggle ? (
<div className="table-container">
{data.map((project, idx) => {
return (
<>
<div className="overflow-x-none flex items-center justify-center w-fit mx-auto border border-[#1B71E8]">
<table className="table-fixed max-w-screen-lg border-seperate border-spacing">
<thead>
<tr>
<th>name</th>
<th>country</th>
<th>carbon credits</th>
<th>type</th>
<th>vintage</th>
<th>serial number</th>
</tr>
</thead>
<tbody>
<tr>
<td className="w-52 h-8 text-center">
{project.project.name}
</td>
<td className="w-52 text-center">
{project.project.country}
</td>
<td className="w-32 max-h-2 text-center">
{project.number_of_credits}
</td>
<td className="w-52 text-center">
{project.project.type}
</td>
<td className="w-52 text-center">
{project.vintage}
</td>
<td className="w-52 text-center">
{project.serial_number}
</td>
</tr>
</tbody>
</table>
</div>
</>
);
})}
</div>
table
Can you please help.

Move the element outside of the {data.map(...)} loop. You can also move the element outside of the loop and use the map function to generate the rows inside of it:
{data && toggle ? (
<div className="table-container">
<div className="overflow-x-none flex items-center justify-center w-fit mx-auto border border-[#1B71E8]">
<table className="table-fixed max-w-screen-lg border-seperate border-spacing">
<thead>
<tr>
<th>name</th>
<th>country</th>
<th>carbon credits</th>
<th>type</th>
<th>vintage</th>
<th>serial number</th>
</tr>
</thead>
<tbody>
{data.map((project, idx) => {
return (
<tr>
<td className="w-52 h-8 text-center">
{project.project.name}
</td>
<td className="w-52 text-center">
{project.project.country}
</td>
<td className="w-32 max-h-2 text-center">
{project.number_of_credits}
</td>
<td className="w-52 text-center">
{project.project.type}
</td>
<td className="w-52 text-center">
{project.vintage}
</td>
<td className="w-52 text-center">
{project.serial_number}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
)}

Related

How can I render api response in React?

I am using sock.js topic subscription for getting data. I can type response to console with this;
<div>
<SockJsClient url='http://localhost:8080/websocket-example' topics={['/topic/currency']}
onMessage={(msg) => {console.log(msg);}}
ref={(client) => {this.clientRef = client}}>
</SockJsClient>
</div>
But i couldn't render it.
Questions;
I am making topic subscription in the render function. Do I have to
do it on another function? If yes how can i achive that?
After the first question, i have to render it. Incoming messages;
{parity: "EURTRY", buy: 7.873162974672604, sell: 7.071410177380406}
{parity: "USDTRY", buy: 7.58763763316203, sell: 7.158500227651455}
I want to render it in a table. Using bootstrap for the table. Each message will be updated in the table by parity property. There can be a switch for it.
<div class="row">
<div class="col-md-12">
<table id="currencies" class= "table table-hover" >
<thead class="thead-dark">
<tr>
<th>Currency Pair</th>
<th>Buy</th>
<th>Sell</th>
</tr>
</thead>
<tbody>
<tr>
<td>USDTRY</td>
<td id="usdTryBuy"></td>
<td id="usdTrySell"></td>
</tr>
<tr>
<td>EURTRY</td>
<td id="eurTryBuy"></td>
<td id="eurTrySell"></td>
</tr>
<tr>
<td>GBPTRY</td>
<td id="gbpTryBuy"></td>
<td id="gbpTrySell"></td>
</tr>
<tr>
<td>EURUSD</td>
<td id="eurUsdBuy"></td>
<td id="eurUsdSell"></td>
</tr>
</tbody>
</table>
</div>
</div>
Thanks for your valuable thoughts.
iterate it using map function
array
let arr = [{parity: "EURTRY", buy: 7.873162974672604, sell: 7.071410177380406},
{parity: "USDTRY", buy: 7.58763763316203, sell: 7.158500227651455}]
JSX
<div class="row">
<div class="col-md-12">
<table id="currencies" class= "table table-hover" >
<thead class="thead-dark">
<tr>
<th>Currency Pair</th>
<th>Buy</th>
<th>Sell</th>
</tr>
</thead>
<tbody>
{arr.map((item,i)=>
<tr key={i}>
<td>USDTRY: {item.parity}</td>
<td id="usdTryBuy">{item.buy}</td>
<td id="usdTrySell">{item.sell}</td>
</tr>
)}
</tbody>
</div>
</div>

Search on bootstrap table with expanding rows

I have a bootstrap table with expanding rows. Each row can expand into multiple rows, and those can too expand in multiple rows (3 levels in total).
The search does find items on the 3rd level but it doesn't display them correctly.
This is an example code on codepen.
https://codepen.io/anon/pen/gGOOBz
My HTML code is:
<table st-table="displayedCollection" st-safe-src="domains" class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th st-sort="name">Domains</th>
</tr>
<tr>
<th colspan="5"><input st-search="" class="form-control" placeholder="Search ..." type="text" /></th>
</tr>
</thead>
<tbody ng-cloak>
<tr ng-repeat-start="row in displayedCollection">
<td id="package1-{{$index}}" class="accordion-toggle data-exists cursor-pointer" data-toggle="collapse" data-parent="#OrderPackages-{{$index}}" data-target=".packageDetails1-{{$index}}">{{row.name}}
</td>
</tr>
<tr ng-repeat-end>
<td id="package2-{{$index}}" colspan="3">
<div class="accordion-body collapse packageDetails1-{{$index}} + 1">
<table st-table="beansCollection" st-safe-src="types" class="table table-condensed" style="border-collapse:collapse;">
<!--<thead>
<tr>
<th st-sort="name">Types</th>
</tr>
</thead>-->
<tbody>
<tr ng-repeat-start="beanType in row.beans">
<td id="type1-{{$index}}" class="accordion-toggle" data-toggle="collapse" data-parent="#typePackages-{{$index}}" data-target=".typesDetails1-{{$index}}">
{{beanType.name}}
</td>
<td align="right">
<button ng-click="getBeanName(beanType.name, null, beanType)" data-toggle="modal" data-target="#myModal" type="button" class="btn btn-default" ng-if="showButton(beanType.beans)">
Display
</button>
</td>
</tr>
<!---->
<tr ng-repeat-end ng-visible="false">
<td id="typeId2-{{$index}}" colspan="3">
<div class="accordion-body collapse typesDetails1-{{$index}} + 1">
<table class="table table-condensed">
<tbody>
<tr ng-repeat="leafBean in beanType.beans">
<td>
{{leafBean.name}}
</td>
<td align="right">
<button ng-click="getBeanName(beanType.name, leafBean.name, leafBean)" data-toggle="modal" data-target="#myModal" type="button" class="btn btn-default">Display</button>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
I want that when I search something under a certain row it would be displayed with the row above already expanded.
PHOTO:
Say I want to search for TBLEXCHANGEMEMBERSHIPS which is on the 3rd level.
When I input it into the search bar, it finds it but doesn't display it correctly.
What should I do?

Bootstrap dropdown using accordion is not working

I am implementing bootstrap dropdown in my Angular application using accordion. When I click on accordion toggle, it is not sliding down and up.
This is my code:
<div class="container-fluid mt100 mr5 ml5">
<div class="row">
<table class='table s12'>
<tr style="background: #e4e9eb;">
<td>Name</td>
<td>Address</td>
<td>City</td>
<td>Edit</td>
<td></td>
</tr>
<tr ng-repeat="listItem in Items">
<td>
{{listItem.name}}
</td>
<td>
{{listItem.address}}
</td>
<td>
{{listItem.city}}
</td>
<td>
<a class="dropdown-toggle" ng-click="isCollapsed = !isCollapsed" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
</a>
</td>
</tr>
<tr>
<td colspan="3" class="hiddenRow">
<div class="collapse" collapse="isCollapsed">
<table class="table display" style="border-collapse:collapse;">
<thead>
<tr class='pix2' style="background: #e4e9eb;">
<th>First</th>
<th>Second</th>
<th>Third</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='item in listItem.subItems'>
<td>{{item.first}}</td>
<td>{{item.second}}</td>
<td>{{item.third}}</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</div>
</div>
What is causing the issue?

Infinite Scroll in angular UI Modal

I am trying to use infinite scroll in a angular UI modal and the modal is defined in ng-template as shown below
<script type="text/ng-template" id="someID" >
<div infinite-scroll="loadMoreEmployees()" infinite-scroll-distance="1">
<div class="modal-header">
<div class="vertical-margin row">
<div class="col-lg-12">
<h2 class="help-block">Departments assign</h2>
</div>
</div>
</div>
<div class="modal-body">
<div class="content-container">
<div class="col-lg-12 col-md-12 simple-data-table-content">
<table data-ng-if="vm.employees!=null || vm.employees.length!=0" class="table table-condensed text-center clear-bottom">
<tbody>
<tr>
<th>Employee No</th>
<th>Employee Name</th>
<th>Email</th>
<th data-ng-repeat="DeptType in vm.DeptTypes track by $index">{{DeptType.name}}</th>
<th></th>
</tr>
<tr data-ng-if="vm.employees==null || vm.employees.length==0"><td colspan="{{vm.DeptTypes.length + 4}}"></td></tr>
<tr data-ng-repeat="employee in vm.employees | limitTo:vm.loadEmployeeLimitCount" data-ng-if="vm.employees!=null && vm.employees.length > 0">
<td class="">{{employee.employeeNumber}}</td>
<td class="">{{employee.name}}</td>
<td class="">{{employee.email}}</td>
<td class="" data-ng-repeat="DeptType in vm.DeptTypes">
<input type="text" only-digits data-ng-model="employee.DeptSummary[DeptType.id]" style="width:50px;" min="1" />
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
But infinite scroll as does not respond to scroll action, please point out where I am wrong.

AngularJS(1.3.0-rc.2): data-ng-repeat-end animation issue

Im wondering why an animation will not work on the <tr> element in this case (plnkr):
<tr data-ng-repeat-end data-ng-show="employee.show" class="animate-show">
<td colspan="5">
<div class="employeeInfo">
<img ng-src="http://placehold.it/{{employee.size}}" />Employee info
</div>
</td>
</tr>
But will work on the <td> element when written like this (plnkr):
<tr data-ng-repeat-end ng-animate-children>
<td colspan="5" data-ng-show="isToggled[$index]" class="animate-show">
<div class="employeeInfo">
<img ng-src="http://placehold.it/{{employee.size}}" />Employee info
</div>
</td>
</tr>
Why is this happening, and what can be done to achive animation on the <tr> with data-ng-repeat-end attribute?

Resources