Skip Function repeating values while appending in View - asp.net-mvc

I am trying to use skip in mvc4 project so that previous value should not repeated while it append in the Razor view but i am getting same value after some value
here is my code :
var planetfeedsOrder = from a in db.PlanetFeeds
where a.PlanetFeedOwnerId == id || a.PlanetFeedPosterId ==id
&& a.CurrentState != 1
join c in db.Graphs on a.PlanetFeedItemGraphId equals c.GraphID
join u in db.UserInfos on a.PlanetFeedOwnerId equals u.UserInfoID
orderby a.PostDate descending
select new UserInfoViewModel
{
AvatarURL = u.AvatarURL,
UserName=u.FirstName +" "+u.LastName,
GraphItemDescription = c.GraphItemDescription,
GraphItemURL = c.GraphItemURL,
GraphItemUserFullName = c.GraphItemUserFullName,
ItemSummary = c.ItemSummary,
GraphItemThumbUrl = c.GraphItemThumbUrl,
GraphID = c.GraphID,
ItemType = c.ItemType,
ItemUserID = c.ItemUserID,
GraphItemTitle = c.GraphItemTitle,
isRootFeed = a.isRootFeed,
PostDate = a.PostDate,
CommentCount = c.CountResponses,
CountReactions = c.CountPositiveReactions,
CountReaction = c.CountNegativeReactions,
ItemID = c.ItemID,
UserLevel =u.UserLevel,
CurrentState = a.CurrentState,
RecieverUserAvatarURL = c.Address
};
return PartialView("_PlanetfeedPartial", planetfeedsOrder.Skip(SkipCount).Take(itemCount).ToList());
and here is ajax view where i am trying to append it :
$.ajax(
{
type: "Post",
url: "/PlanetFeed/PlanetfeedPartial",
dataType: "html",
data: { id: planetFeedOwnerId, filterType: filterType, taggedItemGraphId: taggedItemGraphId, itemCount: itemCount,skipCount: SkipCount }, //"feedGraphId=10696",
success: function (data) {
var div = $('.planetFeed:last');
$('.planetFeed').removeClass('.tile-group');
SkipCount = itemCount+2;
div.after(data);
var bigDivCount = $('.tile-group').length;
var bigDivWidth = bigDivCount * 486;
$('.tile-area').css({ 'width': bigDivWidth });

Based on your code and subsequent comments if the initial values of itemCount and skipCount are zero then in the first call (note I have substituted variables with the actual values in the data parameter so its easier to see what will be returned)
itemCount = itemCount + 5; // itemCount = 0 + 5 = 5
data: { skipCount: 0, itemCount: 5 } // returns items 1-5
skipCount = itemCount + 5; // skipCount = 5 + 5 = 10;
second call
itemCount = itemCount + 5; // itemCount = 5 + 5 = 10;
data: { skipCount: 10, itemCount: 10 } // returns items 11-20 (items 6-10 missed)
skipCount = itemCount + 5; // skipCount = 10 + 5 = 15;
third call
itemCount = itemCount + 5; // itemCount = 10 + 5 = 15;
data: { skipCount: 15, itemCount: 15 } // returns items 16-30 (items 16-20 repeated)
skipCount = itemCount + 5; // skipCount = 15 + 5 = 20;
You need to change the way you set the value of skip count and use skipCount = skipCount + itemCount;
first call
itemCount = itemCount + 5; // itemCount = 0 + 5 = 5
data: { skipCount: 0, itemCount: 5 } // returns items 1-5
skipCount = skipCount + itemCount; // skipCount = 0 + 5 = 5;
second call
itemCount = itemCount + 5; // itemCount = 5 + 5 = 10;
data: { skipCount: 5, itemCount: 10 } // returns items 6-15
skipCount = skipCount + itemCount; // skipCount = 5 + 10 = 15;
third call
itemCount = itemCount + 5; // itemCount = 10 + 5 = 15;
data: { skipCount: 15, itemCount: 15 } // returns items 16-30
skipCount = skipCount + itemCount; // skipCount = 15 + 15 = 30;
forth call
itemCount = itemCount + 5; // itemCount = 15 + 5 = 20;
data: { skipCount: 30, itemCount: 20 } // returns items 31-50
skipCount = skipCount + itemCount; // skipCount = 30 + 20 = 50;

Related

Google Sheet formula for cumulative sum with condition

I have a Google Sheet with the following layout:
Number | Counted? | Cumulative Total
4 | Y | 4
2 | | 6
9 | Y | 15
... | ... | ...
The first cell in the Cumulative Total column is populated with this formula:
=ArrayFormula((SUMIF(ROW(C2:C1000),"<="&ROW(C2:1000),C2:C1000)
However this counts all rows in the 'Number' column. How can I make the Cumulative Total only count rows where the Counted? cell is Y?
Try this in C2 and copy down:
= N(C1) + A2 * (B2 = "Y")
Update
Doesn't seem to work with SUMIFS, but there is a very slow matrix multiplication alternative:
=ArrayFormula(MMult((Row(2:1000)>=Transpose(Row(2:1000)))*Transpose(A2:A1000*(B2:B1000="Y")), Row(2:1000)^0))
Assuming "Number" in column A and "Counted?" in column B, try in C1
={"SUM"; ArrayFormula(if(ISBLANK(B2:B),,mmult(transpose(if(transpose(row(B2:B))>=row(B2:B), if(B2:B="Y", A2:A,0), 0)),row(B2:B)^0)))}
(Change ranges to suit).
Example
custom formula sample:
=INDEX(IF(B3:B="","", runningTotal(B3:B,1,,A3:A)))
sample file
source code
related
code:
/**
* Get running total for the array of numbers
* by makhrov.max#gmail.com
*
* #param {array} numbers The array of numbers
* #param {number} total_types (1-dafault) sum, (2) avg, (3) min, (4) max, (5) count;
* 1-d array or number
* #param {number} limit number of last values to count next time.
* Set to 0 (defualt) to take all values
* #param {array} keys (optional) array of keys. Function will group result by keys
* #return The hex-code of cell background & font color
* #customfunction
*/
function runningTotal(numbers, total_types, limit, keys) {
// possible types to return
var oTypes = {
'1': 'sum',
'2': 'avg',
'3': 'min',
'4': 'max',
'5': 'count'
}
// checks and defaults
var errPre = '🥴 ';
if( typeof numbers != "object" ) {
numbers = [ [numbers] ];
}
total_types = total_types || [1];
if( typeof total_types != "object" ) {
total_types = [ total_types ];
}
if( keys && typeof keys != "object" ) {
keys = [ [keys] ];
}
if (keys) {
if (numbers.length !== keys.length) {
throw errPre + 'Numbers(' +
numbers.length +
') and keys(' +
keys.length +
') are of different length'; }
}
// assign types
var types = [], type, k;
for (var i = 0; i < total_types.length; i++) {
k = '' + total_types[i];
type = oTypes[k];
if (!type) {
throw errPre + 'Unknown total_type = ' + k;
}
types.push(type);
}
limit = limit || 0;
if (isNaN(limit)) {
throw errPre + '`limit` is not a Number!';
}
limit = parseInt(limit);
// calculating running totals
var result = [],
subres = [],
nodes = {},
key = '-',
val;
var defaultNode_ = {
values: [],
count: 0,
sum: 0,
max: null,
min: null,
avg: null,
maxA: Number.MIN_VALUE,
maxB: Number.MIN_VALUE,
maxC: Number.MIN_VALUE,
minA: Number.MAX_VALUE,
minB: Number.MAX_VALUE,
minC: Number.MAX_VALUE
};
for (var i = 0; i < numbers.length; i++) {
val = numbers[i][0];
// find correct node
if (keys) { key = keys[i][0]; }
node = nodes[key] ||
JSON.parse(JSON.stringify(defaultNode_));
/**
* For findig running Max/Min
* sourse of algorithm
* https://www.geeksforgeeks.org
* /sliding-window-maximum-maximum-of-all-subarrays-of-size-k/
*/
// max
//reset first second and third largest elements
//in response to new incoming elements
if (node.maxA<val) {
node.maxC = node.maxB;
node.maxB = node.maxA;
node.maxA = val;
} else if (node.maxB<val) {
node.maxC = node.maxB;
node.maxB = val;
} else if (node.maxC<val) {
node.maxC = val;
}
// min
if (node.minA>val) {
node.minC = node.minB;
node.minB = node.minA;
node.minA = val;
} else if (node.minB>val) {
node.minC = node.minB;
node.minB = val;
} else if (node.minC>val) {
node.minC = val;
}
// if limit exceeds
if (limit !== 0 && node.count === limit) {
//if the first biggest we earlier found
//is matching from the element that
//needs to be removed from the subarray
if(node.values[0]==node.maxA) {
//reset first biggest to second and second to third
node.maxA = node.maxB;
node.maxB = node.maxC;
node.maxC = Number.MIN_VALUE;
if (val <= node.maxB) {
node.maxC = val;
}
} else if (node.values[0]==node.maxB) {
node.maxB = node.maxC;
node.maxC = Number.MIN_VALUE;
if (val <= node.maxB) {
node.maxC = val;
}
} else if (node.values[0]==node.maxC) {
node.maxC = Number.MIN_VALUE;
if (val <= node.maxB) {
node.maxC = val;
}
} else if(node.values[0]==node.minA) {
//reset first smallest to second and second to third
node.minA = node.minB;
node.minB = node.minC;
node.minC = Number.MAX_VALUE;
if (val > node.minB) {
node.minC = val;
}
}
if (node.values[0]==node.minB) {
node.minB = node.minC;
node.minC = Number.MAX_VALUE;
if (val > node.minB) {
node.minC = val;
}
}
if (node.values[0]==node.minC) {
node.minC = Number.MAX_VALUE;
if (val > node.minB) {
node.minC = val;
}
}
// sum
node.sum -= node.values[0];
// delete first value
node.values.shift();
// start new counter
node.count = limit-1;
}
// add new values
node.count++;
node.values.push(val);
node.sum += val;
node.avg = node.sum/node.count;
node.max = node.maxA;
node.min = node.minA;
// remember entered values for the next loop
nodes[key] = node;
// get the result depending on
// selected total_types
subres = [];
for (var t = 0; t < types.length; t++) {
subres.push(node[types[t]]);
}
result.push(subres);
}
// console.log(JSON.stringify(nodes, null, 4));
return result;
}

More efficient way to execute this jquery script

Is there a more efficient way to execute the following jquery script? I need to access the 4 individual variables once the script has run, which I will then send to my database using ajax
var column_1 = $('#column-1').sortable("toArray");
for ( var i = 0, n = column_1.length; i < n; i++ ) {
var v = $('#' + column_1[i] ).find('.inner').is(':visible');
column_1[i] = column_1[i] + ":" + v;
}
var column_2 = $('#column-2').sortable("toArray");
for ( var i = 0, n = column_2.length; i < n; i++ ) {
var v = $('#' + column_2[i] ).find('.inner').is(':visible');
column_2[i] = column_2[i] + ":" + v;
}
var column_3 = $('#column-3').sortable("toArray");
for ( var i = 0, n = column_3.length; i < n; i++ ) {
var v = $('#' + column_3[i] ).find('.inner').is(':visible');
column_3[i] = column_3[i] + ":" + v;
}
var column_4 = $('#column-4').sortable("toArray");
for ( var i = 0, n = column_4.length; i < n; i++ ) {
var v = $('#' + column_4[i] ).find('.inner').is(':visible');
column_4[i] = column_4[i] + ":" + v;
}
This code has not been tested. But should work fine, does what you need it to do. ^^
function x ()
{
var columns = new Array();
columns.push({
column_1 : $('#column-1').sortable("toArray"),
column_2 : $('#column-2').sortable("toArray"),
column_3 : $('#column-3').sortable("toArray"),
column_4 : $('#column-4').sortable("toArray")
});
$.each(columns, function (key, item)
{
SaveToDatabase(item);
});
}
function SaveToDatabase (yourArray)
{
$.each(yourArray, function (key, item) {
var v = $('#' + item).find('.inner').is(':visible');
item = item + ":" + v;
});
}

code not working on server but working on localhost

Here I am using a code for getting pictures of user and his friends from facebook via app the code is working properly in localhost but not in server. Here is the whole functionality which i want:
on a button click get the user info from facebook data
show popup with drop down
display pictures of the user 1st album
display pictures of the frnds on selection
onclick of any image , the selected image should be displayed in next div
All the above functionalities are achieved on localhost and server but the 1st album images are not displayed properly and on click of any image is also not working
// chk for login of user
function Login(y) {
picidgen = y;
FB.login(function (response) {
if (response.authResponse) {
getUserInfo();//get user info
sbsfbdemo();// open popup
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'email,user_photos,user_videos,read_friendlists,friends_photos',
});
}
function getUserInfo() {
FB.api('/me?fields=name,albums,photos,friends', function (response) {
var str = " Select Friends " + "<select name='frndlst2' onchange='frndsalbum()' id='frndlst2'>"
str += "<option value='" + response.id + "'selected ='selected'>" + "me" + "</option>";
for (i = 0; i < response.friends.data.length; i++)
str += "<option value='" + response.friends.data[i].id + "'>" + response.friends.data[i].name + "</option>"
str += "</select>"
str += " Select Album " + "<select name='frndlst1' onchange='getfrndsphotos()' id='frndlst1'>"
for (x = 0; x < response.albums.data.length; x++)
str += "<option value='" + response.albums.data[x].id + "'>" + response.albums.data[x].name + "</option>"
str += "</select>";
document.getElementById("status").innerHTML = str;
picid = response.albums.data[0].id;
var c = response.albums.data[0].count;
FB.api('/' + picid + '/photos?limit=400&offset=0', function (photos) {
if (photos && photos.data && photos.data.length) {
for (var j = 0; j < c; j++) {
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.style.border = "1px solid black";
image.style.margin = "10px";
image.style.cursor = "pointer";
image.src = photo.picture; //user photo is displayed in div(named picture)
picsrc["a" + j] = photo.source;
image.className = "a";
image.id = "a" + j;
document.getElementById("picture").appendChild(image);
}
}
$(".a").bind("click", function () {
var photo = $(this).attr("id");
imagecrop = picsrc[photo];
$("#big-pic").find("img").attr("src", picsrc[photo]);// onclick of image it should be displayed in 2nd div(named big-pic)
});
});
});
}
function frndsalbum() {
document.getElementById("frndlst1").options.length = 0;
var f2 = document.getElementById("frndlst2");
ffrr = f2.options[f2.selectedIndex].value;
FB.api('/' + ffrr + '?fields=albums', function (response) {
var f1 = document.getElementById("frndlst1");
for (var i = 0; i < response.albums.data.length; i++) {
var album = response.albums.data[i];
var option = document.createElement('option');
option.text = album.name;
option.value = album.id;
f1.appendChild(option);
}
document.getElementById("picture").innerHTML = "";
var picid = response.albums.data[0].id;
var c = response.albums.data[0].count; //1st album lenth
FB.api('/' + picid + '/photos?limit=400&offset=0', function (photos) {
if (photos && photos.data && photos.data.length) {
for (var j = 0; j < c; j++) {
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.style.border = "1px solid black";
image.style.margin = "10px";
image.style.cursor = "pointer";
image.src = photo.picture;
picsrc["a" + j] = photo.source;
image.className = "a";
image.id = "a" + j;
document.getElementById("picture").appendChild(image);
}
}
$(".a").bind("click", function () {
var photo = $(this).attr("id");
imagecrop = picsrc[photo];
$("#big-pic").find("img").attr("src", picsrc[photo]);
});
});
});
}
function getfrndsphotos() {
document.getElementById("picture").innerHTML = "";
var f1 = document.getElementById("frndlst1");
ffr = f1.options[f1.selectedIndex].value;
FB.api('/' + ffr + '/photos?limit=400&offset=0', function (photos) {
if (photos && photos.data && photos.data.length) {
for (var j = 0; j < photos.data.length; j++) {
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.style.border = "1px solid black";
image.style.margin = "10px";
image.style.cursor = "pointer";
image.src = photo.picture;
picsrc["a" + j] = photo.source;
image.className = "a";
image.id = "a" + j;
document.getElementById("picture").appendChild(image);
}
}
$(".a").bind("click", function () {
var photo = $(this).attr("id");
imagecrop = picsrc[photo];
$("#big-pic").find("img").attr("src", picsrc[photo]);
});
});
}

ActionScript Unexpected Slashes, Parenthesis, and Squiggly-brackets?

This ActionScript code I have been working on for a few days now works 100% just fine in JavaScript, but when I try to compile it in ActionScript it says I have unexpected /, ), and } symbols. Is this syntax wrong and if so how should I fix it? I figured I could test it as Javascript for quicker testing using http://jsfiddle.net/ but now I'm like =(
var txt = "This is a [rainbow]test to show that I can[/rainbow] make whatever I want [rainbow]appear as a rainbow[/rainbow] because I am [rainbow]awesome[/rainbow].";
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var colors = ['f0f', 'f0c', 'f09', 'f06', 'f03', 'f00', 'f30', 'f60', 'f90', 'fc0', 'ff0', 'cf0', '9f0', '6f0', '3f0', '0f0', '0f3', '0f6', '0f9', '0fc', '0ff', '0cf', '09f', '06f', '03f', '00f', '30f', '60f', '90f', 'c0f'];
function rainbowify(text) {
return text.replace(/\[rainbow\](.+?)\[\/rainbow\]/g, function(_, inner) {
return inner.replace(/./g, function(ch, i) {
return '<font color="#' + colors[i % colors.length] + '">' + ch + '</font>';
});
})
}
txt = rainbowify(txt);
document.write(txt);
}​
Well, this is it:
txt = txt.replace("&apos;", "#");
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var firstChar = txt.indexOf("[rainbow]") + 9;
var lastChar = txt.indexOf("[/rainbow]");
while (lastChar <= txt.lastIndexOf("[/rainbow]")) {
var RAINBOWTEXT = '';
var i = firstChar;
while (i < lastChar) {
RAINBOWTEXT += txt.charAt(i);
i++
}
var text = RAINBOWTEXT;
var texty = '';
colors = new Array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000','ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00','99ff00','66ff00','33ff00','00ff00','00ff33','00ff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');
i = 0;
while (i <= text.length) {
var t = text.charAt(i);
if (t != undefined) {
texty += "<font color=\"#" + colors[i % colors.length] + "\">" + t + "</font>";
i++;
}
}
texty = texty.replace("> <", "> <");
var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
txt = txt.replace(REPLACEME, texty);
if (lastChar == txt.lastIndexOf("[/rainbow]")) {
break;
}
nextChar = lastChar + 10;
firstChar = txt.indexOf("[rainbow]", lastChar) + 9;
lastChar = txt.indexOf("[/rainbow]", lastChar);
}
}
txt = txt.replace("#", "&apos;");

How to create/read/delete cookie in blackberry widget?

How to create/read/delete cookie in blackberry widget?
have same issue but trying out these codes:
function createCookie(name, value, days) {
eraseCookie(name);
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}

Resources