The below code is working for me to get data on API calls but I have one question here. Every time it creates elements in DOM and populate the data which we get from API. But if I get lesser records it is showing blank/empty records. How can I remove or stop generating those blank records?
this.infiniteItems = {
numLoaded_: 0,
toLoad_: 0,
items: [],
getItemAtIndex: function (index) {
if (index > this.numLoaded_) {
this.fetchMoreItems_(index);
return null;
}
return this.items[index];
},
getLength: function() {
return this.numLoaded_ + 25;
},
fetchMoreItems_: function (index) {
if (this.toLoad_ < index) {
this.toLoad_ += 5;
var offset = 0;
$http({
method: 'GET',
datatype: 'json',
url: '{my-api-call}',
contentType: "application/json; charset=utf-8",
cache: false,
params: {
param: query,
page: offset
}
}).then(angular.bind(this, function (obj) {
this.items = this.items.concat(obj.data.SearchResults);
this.numLoaded_ = this.toLoad_;
this.offset++;
$scope.searchResults = obj.data.SearchResults;
}));
}
}
};
Thanks in advance.
I had the same problem and I solved it like this:
$scope.infiniteBrands = {
numLoaded_: 0,
toLoad_: 0,
items: [],
page: -1,
hasMoreItems: true,
perPage: 7,
//REQUIRED.
getItemAtIndex: function(index) {
if (index > this.numLoaded_) {
this.fetchMoreItems_(index);
return null;
}
return this.items[index];
},
//REQUIRED
getLength: function() {
return this.numLoaded_ + 1;
},
fetchMoreItems_: function(index) {
if ( this.hasMoreItems && (this.toLoad_ < index)) {
this.page++;
this.toLoad_ += this.perPage;
brandsFactory.getBrands(this.perPage, this.page)
.then(angular.bind(this, function (data) {
this.items = this.items.concat(data);
if(data.length < this.perPage){
this.numLoaded_ = this.toLoad_ - (this.perPage - data.length) -1;
this.hasMoreItems = false;
}else{
this.numLoaded_ = this.toLoad_;
}
}))
.catch(function(errorResponse){
console.log('Error in brands controller, status : ', errorResponse.status);
});
}
}
};
As you can see the lines that prevents the empty rows are:
if(data.length < this.perPage){
this.numLoaded_ = this.toLoad_ - (this.perPage - data.length) -1;
this.hasMoreItems = false;
}else{
this.numLoaded_ = this.toLoad_;
}
where this.perPage are the number of items I want to ask per page but if the server returns me a lower number, because there are no more, I need to substract those from the this.numLoaded_ variable.
Its late but I hope it helps to anyone who had the same problem.
You should check if your API call has reached the lastPage.
fetchMoreItems_: function (index) {
if ((this.toLoad_ < index) && !this.lastPage) {
this.toLoad_ += this.size;
newsActions.getNews(this.page++, this.size).then(angular.bind(this, function (newsData) {
this.items = this.items.concat(newsData.content);
this.numLoaded_ = this.toLoad_ >= newsData.totalElements ? newsData.totalElements : this.toLoad_ ;
this.lastPage = newsData.last;
}));
}
}
Then use it in getLength function:
getLength: function () {
if(this.lastPage){
return this.numLoaded_
}
return this.numLoaded_ + 2;
},
Related
I have an issue I have a "chat" and when I add a new text in my infinite scroll container,
I go back to the top of the page, i'm not stuck to the bottom.
How can I maintain the page in the bottom of my page while people chat
my service
tabs.factory('chat', function ($http, $timeout, $q) {
return {
default: {
delay: 100
},
data: [],
dataScroll: [],
init: function (data) {
if (this.data.length == 0) {
for (var i = 0; i < data.length; i++) {
this.data[i] = data[i]
}
} else {
var tailleDataSaved = this.data.length
var dataAAjouter = data.slice(tailleDataSaved)
for (var i = 0; i < dataAAjouter.length; i++) {
this.data.push(dataAAjouter[i])
}
}
},
request: function (showAll) {
var self = this;
var deferred = $q.defer();
var index = this.dataScroll.length
var ndDataVisible = 7
var start = index;
var end = index + ndDataVisible - 1;
$timeout(function () {
if (!showAll) {
var item = []
if (start < end) {
for (var i = start; i < end; i++) {
console.log(start)
console.log(end)
console.log(self.data[i])
if (item = self.data[i]) {
self.dataScroll.push(item);
}
}
}
} else {
self.dataScroll = self.data
}
deferred.resolve(self.dataScroll);
}, 0);
return deferred.promise;
}
}
})
my js file
$scope.listChat= function () {
$scope.isActionLoaded = false;
$http.get(apiurl).then(function (response) {
chat.init(response.data)
$scope.isActionLoaded = true
})
}
$scope.listChatInfiniteScroll = function () {
$scope.isScrollDataLoaded = false
ticketListeActionScroll.request(false).then(function (response) {
$scope.actionsInfiniteScroll = response
$scope.isScrollDataLoaded = true
})
}
html file
<div ng-if="isActionLoaded" infinite-scroll='listChatInfiniteScroll ()' infinite-scroll-distance='1'>
<div ng-repeat="chat in actionsInfiniteScroll">
{{chat.text}}
</div>
</div>
Each time I add a new message it calls listChat
The goal is to return a boolean value defining if a Job can be posted based on Earned staffing.
There are different pieces that come from three different sql tables. Rather than making a sql query that returns all of them in one result, i'm trying to understand how to use the $q.all function. The problem is that I am not getting a promise back from the service. The error is: TypeError: Cannot read property 'then' of undefined
There a few articles on this subject but most that I have found are old and still refer to using defer. I have tried the suggestions in the others and none of them have worked. They mentioned that the $q.all needs a return, and to return the $q.resolve and $q.reject for the return values.
Here is the code in my service:
function isMDOLevelAllowed(mdoLevel, finance) {
this.crData = "";
this.pData = "";
var mdoLevelToMatch = "", mdoMatrix = "", mdoOnRollsTotal = 0, mdoAuthTotal = 0;
var mdoVarianceTotal = 0, mdoPending = 0, mdoPendingThisLevel = 0;
return $q.all([
getCRO36ByFinance(finance),
epEarnedMDOSDOResource(finance),
getPendingByFinance(finance)
]).then(function (data) {
var crData = data[0];
var eData = data[1];
var pData = data[2];
var mdoData = crData.filter(function (m) { return m.jobType === "MDO"; });
mdoLevelToMatch = mdoData.filter(function (m) { return m.payGrade === mdoLevel; })[0];
mdoVarianceTotal = mdoData.reduce(function (a, b) { return a + b.variance; }, 0);
mdoMatrix = mdoData.map(function (m) { return { payGrade: m.payGrade, authorized: m.totalAuthorized }; });
mdoPending = pData.mdoTotalCount;
mdoPendingThisLevel = eval("pData.mdO" + mdoLevelToMatch.payGrade + "Count");
// Check if over Total Authorized
if (mdoVarianceTotal + mdoPending < 0) {
// Check if over Paylevel Authorized
if (mdoLevelToMatch.variance + mdoPendingThisLevel < 0) {
return $q.resolve();
}
else {
return $q.reject();
}
}
else {
return $q.reject();
}
}).$promise;
}
var service = {
getEarnedByFinance: getEarnedByFinance,
getCRO36ByFinance: getCRO36ByFinance,
getPendingByFinance: getPendingByFinance,
isMDOLevelAllowed: isMDOLevelAllowed,
isSDOAllowed: isSDOAllowed
};
return service;
How about trying:
function isMDOLevelAllowed(mdoLevel, finance) {
var defer = $q.defer();
this.crData = "";
this.pData = "";
var mdoLevelToMatch = "", mdoMatrix = "", mdoOnRollsTotal = 0, mdoAuthTotal = 0;
var mdoVarianceTotal = 0, mdoPending = 0, mdoPendingThisLevel = 0;
$q.all([
getCRO36ByFinance(finance),
epEarnedMDOSDOResource(finance),
getPendingByFinance(finance)
]).then(function (data) {
var crData = data[0];
var eData = data[1];
var pData = data[2];
var mdoData = crData.filter(function (m) { return m.jobType === "MDO"; });
mdoLevelToMatch = mdoData.filter(function (m) { return m.payGrade === mdoLevel; })[0];
mdoVarianceTotal = mdoData.reduce(function (a, b) { return a + b.variance; }, 0);
mdoMatrix = mdoData.map(function (m) { return { payGrade: m.payGrade, authorized: m.totalAuthorized }; });
mdoPending = pData.mdoTotalCount;
mdoPendingThisLevel = eval("pData.mdO" + mdoLevelToMatch.payGrade + "Count");
// Check if over Total Authorized
if (mdoVarianceTotal + mdoPending < 0) {
// Check if over Paylevel Authorized
if (mdoLevelToMatch.variance + mdoPendingThisLevel < 0) {
defer.resolve();
}
else {
defer.reject();
}
}
else {
defer.reject();
}
});
return defer.promise;
}
Take a note on how i defined var defer = $q.defer(); and then returned defer.promise just once. You dont need to return resolve and reject
Thank you for your help. I got it to work. I changed the $q.resolve, $q.reject to a return true or false, and removed the $promise at the end.
function isMDOLevelAllowed(mdoLevel, finance) {
this.crData = "";
this.pData = "";
var mdoLevelToMatch = "", mdoMatrix = "", mdoOnRollsTotal = 0, mdoAuthTotal = 0;
var mdoVarianceTotal = 0, mdoPending = 0, mdoPendingThisLevel = 0;
return $q.all([
getCRO36ByFinance(finance),
epEarnedMDOSDOResource(finance),
getPendingByFinance(finance)
]).then(function (data) {
var crData = data[0];
var eData = data[1];
var pData = data[2];
var mdoData = crData.filter(function (m) { return m.jobType === "MDO"; });
mdoLevelToMatch = mdoData.filter(function (m) { return m.payGrade === mdoLevel; })[0];
//mdoOnRollsTotal = mdoData.reduce(function (a, b) { return a + b.totalOnRolls; }, 0);
//mdoAuthTotal = mdoData.reduce(function (a, b) { return a + b.totalAuthorized; }, 0);
mdoVarianceTotal = mdoData.reduce(function (a, b) { return a + b.variance; }, 0);
mdoMatrix = mdoData.map(function (m) { return { payGrade: m.payGrade, authorized: m.totalAuthorized }; });
mdoPending = pData.mdoTotalCount;
mdoPendingThisLevel = eval("pData.mdO" + mdoLevelToMatch.payGrade + "Count");
// Check if over Total Authorized
if (mdoVarianceTotal + mdoPending < 0) {
// Check if over Paylevel Authorized
if (mdoLevelToMatch.variance + mdoPendingThisLevel < 0) {
return true;
}
else {
return false;
}
}
else {
return false;
}
});
}
I had this plnkr, working with an array. It had some problems (data being pushed into two arrays), but it was doing its job. Now I moved that array into a .json, and pizze.n is no longer being pushed into $scope.pizze nor $scope.orderList. I guess at this point a good solution would be to create a second .json and use it as my new orderList, or am I wrong? By the way, how do I do it or a better solution? Here's the updated code.
app.factory('elencoPizze', function($http) {
return {
getdata: function() {
return $http.get('json/pizze.json');
}
};
});
app.controller('showcaseCtrl', function($scope, $timeout, elencoPizze) {
$scope.pizze = [];
elencoPizze.getdata().success(function(data) {
$scope.pizze = data;
});
return;
$scope.orderList = [];
$scope.add = function(pizza) {
$scope.placeholder = 'Aggiungi altro?';
$scope.empty = false;
if ($scope.orderList.indexOf(pizza) === -1) {
pizza.n = 1;
return $scope.orderList.push(pizza);
} else {
return pizza.n += 1;
}
};
return $scope.remove = function(pizza) {
var lastRemoving;
if (pizza.n <= 1) {
pizza.n = 0;
lastRemoving = function() {
return $timeout((function() {
var index;
pizza.n = '';
index = $scope.orderList.indexOf(pizza);
$scope.orderList.splice(index, 1);
if ($scope.orderList.length === 0) {
$scope.example();
return $scope.empty = true;
}
}), 300);
};
return lastRemoving();
} else {
return pizza.n -= 1;
}
};
});
// ---
// generated by coffee-script 1.9.2
You haven't returned a promise from your factory. Change to:
return {
getdata: function() {
return $http.get('json/pizze.json').then(function(data) {
return data;
})
}
};
I had in my old project this bit of code for an API:
.factory('Api', ['$resource', 'apiUrl', function ($resource, api) {
var Api = $resource(api + ':path', {
path: '#path'
});
return Api;
}])
and then I had an Order model which extended this factory class like this:
.factory('Order', ['$filter', 'Api', function ($filter, api) {
var Order = api;
angular.extend(Order.prototype, {
getDescription: function () {
var rolls = 0,
cuts = 0,
skus = [],
lines = $filter('orderBy')(this.lines, 'sku');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
switch (line.type) {
case 0: // cut
cuts++;
break;
case 1: // roll
rolls++
break;
}
if (skus.indexOf(line.sku) == -1) {
skus.push(line.sku);
}
}
var description = '';
description += cuts > 0 ? cuts > 1 ? cuts + ' x cuts' : cuts + ' x cut' : '';
description += rolls > 0 && description.length > 0 ? ', ' : '';
description += rolls > 0 ? rolls > 1 ? rolls + ' x rolls' : rolls + ' x roll' : '';
description += skus.length == 1 ? ' of ' + skus[0] : '';
return description;
},
getStatus: function () {
var lines = this.lines,
status = lines[0].status;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (status !== line.status)
return 'Multiple';
}
return status;
},
getDeliveryDate: function () {
var lines = this.lines,
date = lines[0].dates.delivery;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (date !== line.dates.delivery)
return 'Multiple';
}
date = new Date(date);
return date;
},
getDispatchDate: function () {
var lines = this.lines,
date = lines[0].orderDate;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (date !== lines.orderDate)
return 'Multiple';
}
date = new Date(date);
return date;
}
});
return Order;
}]);
Now, I have recently changed my API factory to this:
// ---
// CONSTANTS.
// ---
.constant('apiUrl', 'http://localhost:54326/')
//.constant('apiUrl', 'http://localhost:81/')
// ---
// SERVICES.
// ---
.service('Api', ['$http', 'HttpHandler', 'apiUrl', function ($http, handler, apiUrl) {
// Private function to build our request
var buildRequest = function (url, method, data, params) {
var model = {
method: method,
url: apiUrl + url,
data: data,
params: params
};
return $http(model);
}
// GET
this.get = function (url, params) {
return handler.loadData(buildRequest(url, 'GET', null, params));
}
// POST
this.post = function (url, data) {
return handler.loadData(buildRequest(url, 'POST', data));
}
// PUT
this.put = function (url, data) {
return handler.loadData(buildRequest(url, 'PUT', data));
}
// DELETE
this.delete = function (url, data) {
return handler.loadData(buildRequest(url, 'DELETE', data));
}
}])
When I did this, my order model no longer works. I get an error stating:
Cannot read property '$$hashKey' of undefined
Is there a way I can get my Order model to use the new API factory? Specifically I want to attach functions each object returned by the API.
I always manage pagination with angular
retrieve all the data from the server
and cache it client side (simply put it in a service)
now I have to cope with quite lot of data
ie 10000/100000.
I'm wondering if can get into trouble
using the same method.
Imo passing parameter to server like
page search it's very annoying for a good
user experience.
UPDATE (for the point in the comment)
So a possible way to go
could be get from the server
like 1000 items at once if the user go too close
to the offset (ie it's on the 800 items)
retrieve the next 1000 items from the server
merge cache and so on
it's quite strange not even ng-grid manage pagination
sending parameters to the server
UPDATE
I ended up like:
(function(window, angular, undefined) {
'use strict';
angular.module('my.modal.stream',[])
.provider('Stream', function() {
var apiBaseUrl = null;
this.setBaseUrl = function(url) {
apiBaseUrl = url;
};
this.$get = function($http,$q) {
return {
get: function(id) {
if(apiBaseUrl===null){
throw new Error('You should set a base api url');
}
if(typeof id !== 'number'){
throw new Error('Only integer is allowed');
}
if(id < 1){
throw new Error('Only integer greater than 1 is allowed');
}
var url = apiBaseUrl + '/' + id;
var deferred = $q.defer();
$http.get(url)
.success(function (response) {
deferred.resolve(response);
})
.error(function(data, status, headers, config) {
deferred.reject([]);
});
return deferred.promise;
}
};
};
});
})(window, angular);
(function(window, angular, undefined) {
'use strict';
angular.module('my.mod.pagination',['my.mod.stream'])
.factory('Paginator', function(Stream) {
return function(pageSize) {
var cache =[];
var staticCache =[];
var hasNext = false;
var currentOffset= 0;
var numOfItemsXpage = pageSize;
var numOfItems = 0;
var totPages = 0;
var currentPage = 1;
var end = 0;
var start = 0;
var chunk = 0;
var currentChunk = 1;
var offSetLimit = 0;
var load = function() {
Stream.get(currentChunk).then(function(response){
staticCache = _.union(staticCache,response.data);
cache = _.union(cache,response.data);
chunk = response.chunk;
loadFromCache();
});
};
var loadFromCache= function() {
numOfItems = cache.length;
offSetLimit = (currentPage*numOfItemsXpage)+numOfItemsXpage;
if(offSetLimit > numOfItems){
currentChunk++;
load();
}
hasNext = numOfItems > numOfItemsXpage;
totPages = Math.ceil(numOfItems/numOfItemsXpage);
paginator.items = cache.slice(currentOffset, numOfItemsXpage*currentPage);
start = totPages + 1;
end = totPages+1;
hasNext = numOfItems > (currentPage * numOfItemsXpage);
};
var paginator = {
items : [],
notFilterLabel: '',
hasNext: function() {
return hasNext;
},
hasPrevious: function() {
return currentOffset !== 0;
},
hasFirst: function() {
return currentPage !== 1;
},
hasLast: function() {
return totPages > 2 && currentPage!==totPages;
},
next: function() {
if (this.hasNext()) {
currentPage++;
currentOffset += numOfItemsXpage;
loadFromCache();
}
},
previous: function() {
if(this.hasPrevious()) {
currentPage--;
currentOffset -= numOfItemsXpage;
loadFromCache();
}
},
toPageId:function(num){
currentPage=num;
currentOffset= (num-1) * numOfItemsXpage;
loadFromCache();
},
first:function(){
this.toPageId(1);
},
last:function(){
this.toPageId(totPages);
},
getNumOfItems : function(){
return numOfItems;
},
getCurrentPage: function() {
return currentPage;
},
getEnd: function() {
return end;
},
getStart: function() {
return start;
},
getTotPages: function() {
return totPages;
},
getNumOfItemsXpage:function(){
return numOfItemsXpage;
},
search:function(str){
if(str===this.notFilterLabel){
if(angular.equals(staticCache, cache)){
return;
}
cache = staticCache;
}
else{
cache = staticCache;
cache = _.filter(cache, function(item){ return item.type == str; });
}
currentPage = 1;
currentOffset= 0;
loadFromCache();
}
};
load();
return paginator;
}
});
})(window, angular);
server side with laravel (All the items are cached)
public function tag($page)
{
$service = new ApiTagService(new ApiTagModel());
$items = $service->all();
$numOfItems = count($items);
if($numOfItems > 0){
$length = self::CHUNK;
if($length > $numOfItems){
$length = $numOfItems;
}
$numOfPages = ceil($numOfItems/$length);
if($page > $numOfPages){
$page = $numOfPages;
}
$offSet = ($page - 1) * $length;
$chunk = array_slice($items, $offSet, $length);
return Response::json(array(
'status'=>200,
'pages'=>$numOfPages,
'chunk'=>$length,
'data'=> $chunk
),200);
}
return Response::json(array(
'status'=>200,
'data'=> array()
),200);
}
The only trouble by now is managing filter
I've no idea how to treat filtering :(