6
Problem: I have a table that is populated by data coming from multiple JSON files, so far no problem. The point is, I need to update the data if the values contained in JSON are different than in the table, but without changing the order of the items.
This whole process should preferably be done using only Angularjs.
If a code (Symbol) that came in JSON does not exist in the table I add it, if it already exists I update it. At the moment I have only completed the code below:
<!DOCTYPE html>
<html lang="pt-br" ng-app="money">
<head>
<meta charset="UTF-8"/>
<title>BMF&Bovespa</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<script src="assets/lib/angular.min.js"></script>
<style>
.green {
color: green;
}
.red {
color: red;
}
</style>
<script>
angular.module("money", []);
angular.module("money").controller("moneyController", function ($scope, $http, $interval) {
var papeis = ['ABEaV3.SA','BBTG12.SA', 'AGRO3.SA', 'BPAN4.SA', 'BPHA3.SA', 'BRML3.SA', 'BRSR6.SA', 'BTOW3.SA', 'CARD3.SA', 'CIEL3.SA', 'CMIG4.SA', 'CTKA4.SA', 'CTSA3.SA'];
function consultarCotacao(papeis) {
var arr = [];
angular.forEach(papeis, function (papel) {
var yql = 'select * from yahoo.finance.quotes where symbol in ("' + papel + '")';
var api = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(yql);
var url = api + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
$http({
method: 'GET',
url: url,
timeout: 3000,
headers: {'Content-Type': 'json'}
}).then(function successCallback(response) {
resultado = response.data.query.results.quote;
if (resultado.Name !== null) {
arr.push(resultado);
} else {
console.log(resultado.Symbol + ' não foi encontrado!');
}
}, function errorCallback(response) {
console.log('Falha na chamada do recurso.');
});
});
$scope.valores = arr;
}
$scope.color = function (valor)
{
if (valor.indexOf("+") !== -1) {
return "green";
}
return "red";
}
consultarCotacao(papeis);
//$interval(consultarCotacao, 5000, 0);
});
</script>
</head>
<body ng-controller="moneyController">
<table class="table table-striped">
<thead>
<tr>
<th>Código</th>
<th>Cotação</th>
<th>Variação (RS)</th>
<th>Variação (%)</th>
<th>Maior cotação</th>
<th>Menor cotação</th>
<th>Abertura</th>
<th>Fechamento</th>
<th>Volume</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="valor in valores">
<td>{{valor.Symbol}}</td>
<td>R$ {{valor.LastTradePriceOnly}}</td>
<td ng-class="color(valor.Change)">R$ {{valor.Change}}</td>
<td ng-class="color(valor.ChangeinPercent)">{{valor.ChangeinPercent}}</td>
<td>R$ {{valor.DaysHigh}}</td>
<td>R$ {{valor.DaysLow}}</td>
<td>R$ {{valor.Open}}</td>
<td>R$ {{valor.PreviousClose}}</td>
<td>{{valor.Volume}}</td>
</tr>
</tbody>
</table>
</body>
</html>
Mapping table columns to Json:
- Code => Symbol
- Quotation => Lasttradepriceonly
- Variation (RS) => Change
- Variation (%) => Changeinpercent
- Highest quotation => Dayshigh
- Lowest quotation => Dayslow
- Opening => Open
- Closure => Previousclose
- Volume => Volume
JSON file populating the table:
{
"query": {
"count": 1,
"created": "2017-02-22T12:29:35Z",
"lang": "pt-br",
"results": {
"quote": {
"symbol": "CTSA3.SA",
"Ask": "2.07",
"AverageDailyVolume": "6374",
"Bid": "1.91",
"AskRealtime": null,
"BidRealtime": null,
"BookValue": "6.24",
"Change_PercentChange": "-0.06 - -2.90%",
"Change": "-0.06",
"Commission": null,
"Currency": "BRL",
"ChangeRealtime": null,
"AfterHoursChangeRealtime": null,
"DividendShare": null,
"LastTradeDate": "2/21/2017",
"TradeDate": null,
"EarningsShare": "-0.37",
"ErrorIndicationreturnedforsymbolchangedinvalid": null,
"EPSEstimateCurrentYear": null,
"EPSEstimateNextYear": null,
"EPSEstimateNextQuarter": "0.00",
"DaysLow": "2.00",
"DaysHigh": "2.07",
"YearLow": "1.46",
"YearHigh": "3.29",
"HoldingsGainPercent": null,
"AnnualizedGain": null,
"HoldingsGain": null,
"HoldingsGainPercentRealtime": null,
"HoldingsGainRealtime": null,
"MoreInfo": null,
"OrderBookRealtime": null,
"MarketCapitalization": "78.99M",
"MarketCapRealtime": null,
"EBITDA": "136000.00",
"ChangeFromYearLow": "0.55",
"PercentChangeFromYearLow": "+37.67%",
"LastTradeRealtimeWithTime": null,
"ChangePercentRealtime": null,
"ChangeFromYearHigh": "-1.28",
"PercebtChangeFromYearHigh": "-38.91%",
"LastTradeWithTime": "4:29pm - <b>2.01</b>",
"LastTradePriceOnly": "2.01",
"HighLimit": null,
"LowLimit": null,
"DaysRange": "2.00 - 2.07",
"DaysRangeRealtime": null,
"FiftydayMovingAverage": "2.03",
"TwoHundreddayMovingAverage": "1.84",
"ChangeFromTwoHundreddayMovingAverage": "0.17",
"PercentChangeFromTwoHundreddayMovingAverage": "+9.44%",
"ChangeFromFiftydayMovingAverage": "-0.02",
"PercentChangeFromFiftydayMovingAverage": "-0.77%",
"Name": "SANTANENSE ON",
"Notes": null,
"Open": "2.07",
"PreviousClose": "2.07",
"PricePaid": null,
"ChangeinPercent": "-2.90%",
"PriceSales": "0.23",
"PriceBook": "0.33",
"ExDividendDate": "5/4/2015",
"PERatio": null,
"DividendPayDate": null,
"PERatioRealtime": null,
"PEGRatio": "0.00",
"PriceEPSEstimateCurrentYear": null,
"PriceEPSEstimateNextYear": null,
"Symbol": "CTSA3.SA",
"SharesOwned": null,
"ShortRatio": "0.00",
"LastTradeTime": "4:29pm",
"TickerTrend": null,
"OneyrTargetPrice": null,
"Volume": "6800",
"HoldingsValue": null,
"HoldingsValueRealtime": null,
"YearRange": "1.46 - 3.29",
"DaysValueChange": null,
"DaysValueChangeRealtime": null,
"StockExchange": "SAO",
"DividendYield": null,
"PercentChange": "-2.90%"
}
}
}
}
Another example of JSON:
{
"query": {
"count": 1,
"created": "2017-02-22T12:29:35Z",
"lang": "pt-br",
"results": {
"quote": {
"symbol": "BPHA3.SA",
"Ask": "7.70",
"AverageDailyVolume": "230233",
"Bid": "7.46",
"AskRealtime": null,
"BidRealtime": null,
"BookValue": "5.54",
"Change_PercentChange": "-0.15 - -1.96%",
"Change": "-0.15",
"Commission": null,
"Currency": "BRL",
"ChangeRealtime": null,
"AfterHoursChangeRealtime": null,
"DividendShare": null,
"LastTradeDate": "2/21/2017",
"TradeDate": null,
"EarningsShare": "-9.42",
"ErrorIndicationreturnedforsymbolchangedinvalid": null,
"EPSEstimateCurrentYear": "-0.50",
"EPSEstimateNextYear": null,
"EPSEstimateNextQuarter": "0.00",
"DaysLow": "7.47",
"DaysHigh": "7.90",
"YearLow": "3.92",
"YearHigh": "19.20",
"HoldingsGainPercent": null,
"AnnualizedGain": null,
"HoldingsGain": null,
"HoldingsGainPercentRealtime": null,
"HoldingsGainRealtime": null,
"MoreInfo": null,
"OrderBookRealtime": null,
"MarketCapitalization": "848.11M",
"MarketCapRealtime": null,
"EBITDA": "-296.97M",
"ChangeFromYearLow": "3.58",
"PercentChangeFromYearLow": "+91.33%",
"LastTradeRealtimeWithTime": null,
"ChangePercentRealtime": null,
"ChangeFromYearHigh": "-11.70",
"PercebtChangeFromYearHigh": "-60.94%",
"LastTradeWithTime": "6:04pm - <b>7.50</b>",
"LastTradePriceOnly": "7.50",
"HighLimit": null,
"LowLimit": null,
"DaysRange": "7.47 - 7.90",
"DaysRangeRealtime": null,
"FiftydayMovingAverage": "6.39",
"TwoHundreddayMovingAverage": "8.00",
"ChangeFromTwoHundreddayMovingAverage": "-0.50",
"PercentChangeFromTwoHundreddayMovingAverage": "-6.30%",
"ChangeFromFiftydayMovingAverage": "1.11",
"PercentChangeFromFiftydayMovingAverage": "+17.40%",
"Name": "BR PHARMA ON NM",
"Notes": null,
"Open": "7.65",
"PreviousClose": "7.65",
"PricePaid": null,
"ChangeinPercent": "-1.96%",
"PriceSales": "0.41",
"PriceBook": "1.38",
"ExDividendDate": "5/2/2012",
"PERatio": null,
"DividendPayDate": null,
"PERatioRealtime": null,
"PEGRatio": "0.00",
"PriceEPSEstimateCurrentYear": null,
"PriceEPSEstimateNextYear": null,
"Symbol": "BPHA3.SA",
"SharesOwned": null,
"ShortRatio": "0.00",
"LastTradeTime": "6:04pm",
"TickerTrend": null,
"OneyrTargetPrice": null,
"Volume": "268800",
"HoldingsValue": null,
"HoldingsValueRealtime": null,
"YearRange": "3.92 - 19.20",
"DaysValueChange": null,
"DaysValueChangeRealtime": null,
"StockExchange": "SAO",
"DividendYield": null,
"PercentChange": "-1.96%"
}
}
}
}
I published the code on: https://github.com/fabiojaniolima/money
And what you’ve done?
– Laerte
Related: http://answall.com/questions/185159/como-locator-values-attributeapplicationsusing-o-angularjs
– novic
@Fabio put everything you did, the ideal would be to put the
angular
that you’ve made so far.– novic
Virgilio, the code hasn’t changed, it’s posted here: http://answall.com/questions/185159/howto locate values-values-attributeapplications-utilindo-o-angularjs I posted above a little bit so as not to pollute the page too much, since these characteristics can be considered or treated independently
– Fábio Jânio
Have you ever tried to map your JSON to a javascript object and bind to HTML with the angular?
– Rafael B. Marcílio
Yes, but I didn’t succeed. Front-end is not my strong suit.
– Fábio Jânio
Fabio, when you get the array, is there any field that is always unique? regardless of how many times you make the request.. For example, I believe the camp
symbol
be unique, correct? If not, is there any other who is? like an ID, for example.– celsomtrindade
I would indicate using Oneway Databind and every 10 seconds as you commented, reset the array from the list used in ngRepeate and load the new items into the array. More: http://answall.com/questions/153646/para-que-serve-o-dois-pontos-duplos-angular
– Matheus
@Celsomtrindade the field Symbol will be unique
– Fábio Jânio