How can I render api response in React? - reactjs

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>

Related

Sorting columns with smart table

I am trying to sort columns by clicking on the header item using smart table. But it does not work for me. What am I doing wrong?
This is my HTML:
<div class="pane pane--table1" st-table="data.content" st-safe-src="data">
<div class="pane-hScroll">
<table>
<thead>
<tr>
<th ng-repeat="item in data.header" st-sort="{{item}}">{{item}}</th>
</tr>
</thead>
</table>
<div class="pane-vScroll">
<table>
<tbody>
<div ng-include="'templates/includes/ajax_spinner.html'"></div>
<tr ng-repeat="value in data.content">
<td ng-repeat="(key, val) in value[0]">{{val}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

Take data from one list to another

I have two tables in my HTML. I want to take data from one list and pass it to another. I can't do the same ng-repeat call because on the second table I have one individual customer and I just want to display his ID. On the first table, I have all the customers.
<table border="1" id="userTable">
<tr>
<b>#</b>
</tr>
<tr ng-repeat="user in userList" ng-click="viewUserDiv()">
<td ng-model="id">{{user.id}}</td>
</tr>
</table>
<div ng-show="userDiv">
<table border="1">
<tr>
<b>#</b>
</tr>
<td ng-model="id">{{id}}</td>
</table>
</div>
<table border="1" id="userTable">
<tr>
<b>#</b>
</tr>
<tr ng-repeat="user in userList" ng-click="viewUserDiv(user)">
<td ng-model="id">{{user.id}}</td>
</tr>
</table>
$scope.viewUserDiv = function(user) {
if (user) {
$scope.userDiv = true;
$scope.userDivUser = user;
}
}
<div ng-show="userDiv">
<table border="1">
<tr>
<b>#</>
</tr>
<td ng-model="userDivUser">{{userDivUser.id}}</td>
</table>
<div>

Bootstrap collapse close other on click

I am using ng-repeat from AngularJS in a table.
When I click on a row, there is data that appears just below this row.
The problem is that when I click on another row while one is already expanded, both are expanded.
I would like to close the first one and expand the second one.
HTML
<div id="accordion">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-10">Name</th>
<th class="col-md-1">Collateral</th>
<th class="col-md-1"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="pack in packages | filter:search" id="row">
<td data-toggle="collapse" data-target="#collapse{{$index}}" data-parent="#accordion" ng-click="expand(pack)">{{pack.name}}</td>
<td><button class="btn btn-info"><span class="glyphicon glyphicon-open-file"></span></button></td>
<td><input type="radio" ng-model="$parent.selectedPackage" ng-value="pack" /></td>
</tr>
<tr ng-repeat-end>
<td colspan="3">
<div class="collapse" id="collapse{{$index}}">
<table class="table">
<tbody>
<tr>
<td>{{indication}}</td>
</tr>
<tr ng-repeat="bb in bananas">
<td>{{bb.name}}</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
first you don't need of unique id for it... Just try with https://github.com/angular-ui/bootstrap/tree/master/src/collapse or in your case maybe: https://github.com/angular-ui/bootstrap/tree/master/src/accordion
Good Luck.

Multiple Filtered Counts with Angular

I have a list of users for each of my stores [model.stores.users] in JSON object and currently show the number of users against each store by accessing store.users.length
Now I want two extra counts, these counts are just based on the number of users with a simple boolean filter.
Count of Active Users is where user.is_user_active = true
Count of Pending Users is where user.is_user_active = false
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th class="hidden">ID</th>
<th>Name</th>
<th>Users</th>
</tr>
</thead>
<tbody class="tbl-jobs-data" ng-repeat="item in model.stores">
<tr data-toggle="collapse" class="accordion-toggle" data-target="#store_user{{$index}}">
<td class="hidden">{{item.id}}</td>
<td class="">{{item.name}}</td>
<td class="user-count">{{item.users.length}}</td>
</tr>
<tr ng-if="item.users.length > 0">
<td colspan="100" class="hiddenRow">
<div class="accordian-body collapse" id="store_user{{$index}}">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>State</th>
<th>Is Active</th>
<th>Last Login</th>
</tr>
</thead>
<tbody class="tbl-search-data">
<tr ng-repeat="app in item.users">
<td>{{app.id}}</td>
<td>{{app.name}}</td>
<td>{{app.state}}</td>
<td>{{app.is_user_active}}</td>
<td>{{app.last_login}}</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Just use filter, eg
<td class="active_user_count">
{{ (item.users | filter : { is_user_active: true }).length }}
</td>
<td class="pending_user_count">
{{ (item.users | filter : { is_user_active: false }).length }}
</td>
I hope I've got the syntax right, the Angular docs are down at the moment >:(

AngularJS ng-include finished

I'm loading a large amount of data using ng-include.
I'd like to know if there is a way to know when a template is "rendered", because sometimes it seems like it's been "frozen", because I want to do a "loading screen" or something.
Thx.
Here's the code
controller code:
$scope.layout = {
current: 'single-table.html'
};
$scope.layout.get = function() {
return $scope.layout.current;
};
templates:
main.html
<div class="btn-group">
<button ng-click="layout.current = 'single-table.html'">Single</button>
<button ng-click="layout.current = 'multiple-table.html'">Multiple</button>
</div>
<div ng-include="layout.get()"></div>
single-table.html
<table class="table table-bordered">
<thead>
<th ng-repeat="column in columns">{{ column.title }}</th>
</thead>
<tbody>
<tr ng-repeat="record in records">
<td ng-repeat="field in record.fields"
ng-controller="FieldController"
ng-class="getClass()">
{{ field.display }}
</td>
</tr>
</tbody>
</table>
multiple-table.html
<table class="table table-bordered" ng-repeat="record in records">
<tbody>
<tr ng-repeat="field in record.fields">
<th>{{ columns[$index].title }}</th>
<td ng-controller="FieldController" ng-class="getClass()">
{{ field.display }}
</td>
</tr>
</tbody>
</table>
EDIT
I'm using version 1.2.0
One solution (probably not the best) is doing this (no ng-include)
<table class="table table-bordered" ng-show="layout.current == 'single-table.html'">
<thead>
<th ng-repeat="column in columns">{{ column.title }}</th>
</thead>
<tbody>
<tr ng-repeat="record in records">
<td ng-repeat="field in record.fields"
ng-controller="FieldController"
ng-class="getClass()">
<span lud-run="{{ field.ng_directive }}"></span>
</td>
</tr>
</tbody>
</table>
<table class="table table-bordered" ng-repeat="record in records" ng-show="layout.current == 'multiple-table.html'">
<tbody>
<tr ng-repeat="field in record.fields">
<th>{{ columns[$index].title }}</th>
<td ng-controller="FieldController" ng-class="getClass()">
<span lud-run="{{ field.ng_directive }}"></span>
</td>
</tr>
</tbody>
</table>
I have only 20 records, but (is not in the code), each cells loads a custom directive depends on the data type (string, date, datetime, image...). This "solution" I think runs the same data twice (ng-show, not ng-if), but when a switch the layout mode there is no "lag".
You can use the onload hook on ng-include to update a variable when the include has finished rendering. If you set it to true on clicking your button, then revert it back to false in onload, you should be able to leverage it to temporarily display a loading screen.

Resources