Problema: Tenho uma tabela que é populada por dados vindos de multiplos arquivos JSON, até ai nenhum problema. A questão é que, preciso atualizar os dados caso os valores contidos no JSON sejam diferentes do que está na tabela, mas sem alterar a ordem dos itens.
Todo esse processo deve preferencialmente ser feito utilizando somente AngularJS.
Se um código (symbol) que veio no JSON não existir na tabela eu o adiciono, se já existir eu atualizo. No momento só tenho concluido o código abaixo:
<!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>
Mapeamento das colunas da tabela para o JSOn:
- Código => Symbol
- Cotação => LastTradePriceOnly
- Variação (RS) => Change
- Variação (%) => ChangeinPercent
- Maior cotação => DaysHigh
- Menor cotação => DaysLow
- Abertura => Open
- Fechamento => PreviousClose
- Volume => Volume
Arquivo JSON que popula a tabela:
{
"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%"
}
}
}
}
Outro exemplo de 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%"
}
}
}
}
Publiquei o código em: https://github.com/fabiojaniolima/money
angular
que você fez até o momento.