JavaScript - Pagination with JSON - javascript

So I have made a kind of pagination with Javascript. First it gets JSON data from the file and puts it into array. Then it renders items to page. I have added there Previous and Next page buttons, but at the moment it works like if you click next page it just clears the div where are all the items and adds new ones, but my question is. How could I do it differently, so it would not clear out the div, because if I have chosen the item already and div is getting cleared, then the item will be unchosen.
Here's JSON javascript:
$.getJSON("/Search.php", function(itemsList){
if(itemsList.items){
for(var i = 0;i < itemsList.items.length; i++){
pruice = itemsList.items[i].price;
prices[itemsList.items[i].name] = pruice;
items[i] = {
name: itemsList.items[i].name,
img: itemsList.items[i].img,
price: itemsList.items[i].price,
color: itemsList.items[i].color
};
}
}
items.sort(function(a, b) {return b.price - a.price;});
OnFinished();
});
Render function
function OnFinished(){
$('#InventoryMe').empty();
var perPage = 30;
function paginate(items, page) {
var start = perPage * page;
return items.slice(start, start + perPage);
}
function renderItems(pageItems){
pageItems.forEach(function(item, index, arr){
$('#InventoryMe').append("<div class='item' style='background-image: url(https://steamcommunity-a.akamaihd.net/economy/image/"+item.img+"/116x116f)'> <span class='nameArea'>"+item.name+"</span><span class='priceArea' style='border: 1px solid #1f1e1e;border-bottom-color:"+item.color+"'>"+item.price+"</span></div>");
});
}
Next & Previous page
var page = 0;
renderItems(paginate(items, page));
$('#nextPage').on('click', function(){
$('#InventoryMe').empty();
page++;
renderItems(paginate(items, page));
});
$('#previousPage').on('click', function(){
$('#InventoryMe').empty();
page--;
renderItems(paginate(items, page));
});
}
Item selecting script
$( "#InventoryMe" ).on("click", ".item", function() {
var calculateP = fee / 100;
var itemName = $(this).find('.nameArea').text();
var itemPrice = $(this).find('.priceArea').text();
var newPrice = itemPrice * calculateP;
var jacobExe = parseInt(newPrice * 100)/100;
if($(this).closest(".item").hasClass("item-selected")){
$(this).last().removeClass("item-selected");
} else{
$(this).toggleClass("item-selected");
}
calculateTotal();
});

I assume you are selecting an item, and paginate away and back again, and the selection is then gone.
If that is the case, I would save the selection in a javascript variable, preferably by id.
$.getJSON("/Search.php", function(itemsList){
if(itemsList.items){
for(var i = 0;i < itemsList.items.length; i++){
pruice = itemsList.items[i].price;
prices[itemsList.items[i].name] = pruice;
items[i] = {
name: itemsList.items[i].name,
img: itemsList.items[i].img,
price: itemsList.items[i].price,
color: itemsList.items[i].color,
id: itemsList.items[i].id //add id
};
}
}
items.sort(function(a, b) {return b.price - a.price;});
OnFinished();
});
Add id to DOM
function renderItems(pageItems){
pageItems.forEach(function(item, index, arr){
$('#InventoryMe').append("<div data-id='"+item.id+"' class='item' style='background-image: url(https://steamcommunity-a.akamaihd.net/economy/image/"+item.img+"/116x116f)'> <span class='nameArea'>"+item.name+"</span><span class='priceArea' style='border: 1px solid #1f1e1e;border-bottom-color:"+item.color+"'>"+item.price+"</span></div>");
});
}
Then save id on click
var savedSelection;
$("#InventoryMe .item").click(function() {
savedSelection = $(this).data('id');
var calculateP = fee / 100;
var itemName = $(this).find('.nameArea').text();
var itemPrice = $(this).find('.priceArea').text();
var newPrice = itemPrice * calculateP;
var jacobExe = parseInt(newPrice * 100)/100;
if($(this).closest(".item").hasClass("item-selected")){
$(this).last().removeClass("item-selected");
} else{
$(this).toggleClass("item-selected");
}
calculateTotal();
});
Then in your renderItems, I would do
function renderItems(pageItems) {
pageItems.forEach(function(item, index, arr) {
$('#InventoryMe').append("<div class='item' style='background-image: url(https://steamcommunity-a.akamaihd.net/economy/image/"+item.img+"/116x116f)'> <span class='nameArea'>"+item.name+"</span><span class='priceArea' style='border: 1px solid #1f1e1e;border-bottom-color:"+item.color+"'>"+item.price+"</span></div>");
});
if (savedSelection) {
$('[data-id="'+savedSelection+'"]').click();
}
}
Don't forget to place savedSelection in a reachable scope of both functions.

Related

Solve mathematical function in javascript

I have created this function for a shopping basket. To be simple, i have x products with (2 checkbox + quantity input) for each.
The first checkbox add +3, second +8, to the product price. It's like (3+8+price)*quantity. At the end of the page, i have the total result (like product1 + product2...Etc)
The problem is: If i'm posting the form with new quantities, and going back to the page, i have the default result (total price) (of course, checkbox are checked + new quantity with PHP).
Here is my function:
$(function(){
var item_prices = 0;
var total_item_prices = 0;
var total_unique_item_prices = 0;
var total_price = 0;
$.each($('*[data-item-price]'), function(index, element) {
var item_price = $(element).data("item-price");
total_unique_item_prices = parseInt($('[data-total]').html()) + item_price;
total_price = total_unique_item_prices;
$('[data-total]').html(total_unique_item_prices);
})
$.each($('*[data-item-price]'), function(index, element) {
var item_price = $(element).data("item-price");
var price_of_options = 0;
var quantity = parseInt($(element).find('input[data-quantity]').val());
$.each($(element).children().find("[data-price]"), function(index, element2) {
$(element2).click(function(){
var actual_price = parseInt($('[data-total]').html());
var actual_item_price = parseInt($(element).find('[data-item-total]').html());
var price = $(element2).data("price");
if ($(element2).is(':checkbox') && $(element2).is(':checked')) {
price_of_options += price;
$(element).find('[data-item-total]').html(actual_item_price + (price * quantity));
$('[data-total]').html(actual_price + (price * quantity));
}
else {
price_of_options -= price;
$(element).find('[data-item-total]').html(actual_item_price - (price));
$('[data-total]').html(actual_price - (price * quantity));
}
})
})
$(element).find('input[data-quantity]').bind('keyup mouseup', function () {
var new_quantity = parseInt($(element).find('input[data-quantity]').val());
var quantity_dif = new_quantity - quantity;
var actual_price = parseInt($('[data-total]').html());
if (quantity_dif > 0) {
quantity_dif = new_quantity - quantity;
$(element).find('[data-item-total]').html((item_price + price_of_options) * new_quantity);
$('[data-total]').html(actual_price + ((item_price + price_of_options) * quantity_dif));
}
else {
quantity_dif *= -1;
quantity = parseInt($(element).find('[data-quantity]').val());
$(element).find('[data-item-total]').html((item_price + price_of_options) * new_quantity);
$('[data-total]').html(actual_price - ((item_price + price_of_options) * quantity_dif));
}
quantity = parseInt($(element).find('[data-quantity]').val());
})
})
})
As I said, code is working: https://jsfiddle.net/redbean/4dy38ye4/1/ Just not when i'm reloading the webpage.
I'm not english, be kind about this. I'm trying to do the best to be clear!

calculate total price from checkbox + select list

i want to calculate the total price from select list + checkboxes
this code showing the price of selected element from the list "tvs":
<script type="text/javascript">
$(document).ready(function () {
var cheap = false;
$('#tvs').change(function () {
var price = parseFloat($('.total').data('base-price')) || 0;
$('#tvs').each(function (i, el) {
price += parseFloat($('option:selected', el).data(cheap ? 'cheap' : 'price'));
console.log('x', price)
$('.total').val(price.toFixed(2) + '' + '$');
});
});
});
</script>
<input placeholder="0.00$" style=" width: 65px; border: 0px;" class="total" data-base-price="0" readOnly>
and this code showing the price of checked checkboxes:
<script type="text/javascript">
function update_amounts_modal() {
var sum = 0.0;
$('form').each(function () {
var qty = $(this).find('.qty').val();
var price = $(this).find('.price').val();
var isChecked = $(this).find('.idRow').prop("checked");
if (isChecked){
qty = parseInt(qty, 10) || 0;
price = parseFloat(price) || 0;
var amount = (qty * price);
sum += amount;
}
});
$('.total-modal').text(sum.toFixed(2) + ' ' + '$');
$().ready(function () {
update_amounts_modal();
$('form .qty, form .idRow').change(function () {
update_amounts_modal();
});
});
</script>
<div id="subusers" class="total-modal">0.00 $</div>
Hey #musa94 I assume you have a fairly large application and you separate your code into files and what to find a way to sum up two values from different chunks of code. A easy way without changing too much of your existing code is to define a shared data object that holds the values that you want to handle through out your application. You can sum the value from the list and checkbox up by accessing the values in the data object.
// define a data object that exists in your application
window.__ = window.__ || { data: { total: 0, modal: 0 } };
$(document).ready(function () {
var cheap = false;
$('#tvs').change(function () {
var total = getTvsTotal(cheap);
// update your view
$('.total').val(total.toFixed(2) + '' + '$');
// update your data object
__.data.total = total;
console.log('review data object', __);
});
});
$(document).ready(function () {
$('form .qty, form .idRow').change(function () {
var total = update_amounts_modal();
$('.total-modal').text(total.toFixed(2) + ' ' + '$');
__.data.modal = total;
console.log('review data object', __);
});
});
/**
* calculate your tvs total
* #param {boolean}
* #return {number}
*/
function getTvsTotal(cheap) {
var price = parseFloat($('.total').data('base-price')) || 0;
$('#tvs').each(function (i, el) {
price += parseFloat($('option:selected', el).data(cheap ? 'cheap' : 'price'));
console.log('list total:', price);
});
return price;
}
/**
* calculate your amounts modal
* #return {number}
*/
function update_amounts_modal() {
var sum = 0.0;
$('form').each(function () {
var qty = $(this).find('.qty').val();
var price = $(this).find('.price').val();
var isChecked = $(this).find('.idRow').prop("checked");
var amount;
if (isChecked){
qty = parseInt(qty, 10) || 0;
price = parseFloat(price) || 0;
amount = qty * price;
sum += amount;
}
console.log('sum:', sum);
});
return sum;
}

jQuery price calculator function is not working with tabs

I have been trying to get this working from last couple of days without any success. I have this price calculator function developed by a freelancer who is not reachable from last few weeks.
This function works fine without any JavaScript tabs but not quite right with them. I need to have tabs on page because there are tons of options in this calculator.
This is the jQuery function.
$(document).ready(function() {
// For tabs
var tabContents = $(".tab_content").hide(),
tabs = $("ul.nav-tabs li");
tabs.first().addClass("active").show();
tabContents.first().show();
tabs.click(function() {
var $this = $(this),
activeTab = $this.find('a').attr('href');
if (!$this.hasClass('active')) {
$this.addClass('active').siblings().removeClass('active');
tabContents.hide().filter(activeTab).fadeIn();
}
return false;
});
// For Calculator
function Cost_Calculator() {
var Currency = '$';
var messageHTML = 'Please contact us for a price.';
function CostFilter(e) {
return e;
}
//Calculate function
function calculate() {
//Blank!
var CalSaveInfo = [];
$('#cost_calc_custom-data, #cost_calc_breakdown').html('');
//Calculate total
var calCost = 0;
var calculate_class = '.cost_calc_calculate';
$('.cost_calc_active').each(function() {
//Calculation
calCost = calCost + parseFloat($(this).data('value'));
//Add to list
var optionName = $(this).attr('value');
var appendName = '<span class="cost_calc_breakdown_item">' + optionName + '</span>';
var optionCost = $(this).attr('data-value');
var appendCost = '<span class="cost_calc_breakdown_price">' + Currency + optionCost + '</span>';
if (optionCost != "0") {
var appendItem = '<li>' + appendName + appendCost + '</li>';
}
//hidden data
var appendPush = ' d1 ' + optionName + ' d2 d3 ' + optionCost + ' d4 ';
$('#cost_calc_breakdown').append(appendItem);
CalSaveInfo.push(appendPush);
});
//Limit to 2 decimal places
calCost = calCost.toFixed(2);
//Hook on the cost
calCost = CostFilter(calCost);
var CustomData = '#cost_calc_custom-data';
$.each(CalSaveInfo, function(i, v) {
$(CustomData).append(v);
});
//Update price
if (isNaN(calCost)) {
$('#cost_calc_total_cost').html(messageHTML);
$('.addons-box').hide();
} else {
$('#cost_calc_total_cost').html(Currency + calCost);
$('.addons-box').show();
}
}
//Calculate on click
$('.cost_calc_calculate').click(function() {
if ($(this).hasClass('single')) {
//Add cost_calc_active class
var row = $(this).data('row');
//Add class to this only
$('.cost_calc_calculate').filter(function() {
return $(this).data('row') == row;
}).removeClass('cost_calc_active');
$(this).addClass('cost_calc_active');
} else {
// Remove class if clicked
if ($(this).hasClass('cost_calc_active')) {
$(this).removeClass('cost_calc_active');
} else {
$(this).addClass('cost_calc_active');
}
}
//Select item
var selectItem = $(this).data('select');
var currentItem = $('.cost_calc_calculate[data-id="' + selectItem + '"]');
var currentRow = currentItem.data('row');
if (selectItem !== undefined) {
if (!$('.cost_calc_calculate[data-row="' + currentRow + '"]').hasClass('cost_calc_active'))
currentItem.addClass('cost_calc_active');
}
//Bring in totals & information
$('#cost_calc_breakdown_container, #cost_calc_clear_calculation').fadeIn();
$('.cost_calc_hide').hide();
$('.cost_calc_calculate').each(function() {
if ($(this).is(':hidden')) {
$(this).removeClass('cost_calc_active');
}
calculate();
});
return true;
});
$('#cost_calc_clear_calculation').click(function() {
$('.cost_calc_active').removeClass('cost_calc_active');
calculate();
$('#cost_calc_breakdown').html('<p id="empty-breakdown">Nothing selected</p>');
return true;
});
}
//Run cost calculator
Cost_Calculator();
});
You can see this working on jsfiddle without tabs. I can select options from multiple sections and order box will update selected option's price and details dynamically.
But when I add JavaScript tabs, it stop working correctly. See here. Now if I select option from different sections, order box resets previous selection and shows new one only.
I think the problem is with calculator somewhere.
You are removing the active class from hidden elements. This means that when you move to the second tab you disregard what you've done in the first.
line 120 in your fiddle:
if ($(this).is(':hidden')) {
$(this).removeClass('cost_calc_active');
}
I haven't taken the code in depth enough to tell if you can just remove this.

Create infinite loop with moving divs using javascript or jQuery

I need to simulate a band that moves from left to right continuously, carrying 15 divs on it and moving them in a circle, like a carousel. When they reach the right margin to appear from the left.
I have the code that works for 1 div(more or less), but Im having troubles making the loop that includes all 15 divs.
What am I missing?
Here's what I have so far:
HTML
<body>
<div id="fooObject0">1</div>
<div id="fooObject1">2</div>
....
<div id="fooObject13">14</div>
<div id="fooObject14">15</div>
</body>
CSS
body {
font:76% normal verdana,arial,tahoma;
width:600px;
height:600px;
position:relative;
margin:0 auto;
border:1px solid;
}
div {
position:absolute;
left:0px;
top:8em;
width:55px;
height:70px;
line-height:3em;
background:#99ccff;
border:1px solid #003366;
white-space:nowrap;
padding:0.5em;
}
javascript
var foo = null; // object
function doMove(id) {
foo = document.getElementById(id);
foo.style.left = parseInt(foo.style.left)+1+'px';
setTimeout(doMove(id),20); // call doMove in 20msec
if(foo.style.left == "600px") {
foo.style.left = 0;
}
}
function init() {
for(i=0;i<15;i++){
var foo = document.getElementById('fooObject' + i); // get the "foo" object
foo.style.left = -i*55+'px'; // set its initial position to 0px
doMove('fooObject' + i); // start animating
console.log('fooObject' + i);
}
}
window.onload = init;
Thank you in advance!
It call an invalid function setTimeout(doMove(id), 20);.
doMove(id) return undefined, or you use "shared var" (Orientad Object) or doMove need return other function.
Note: var foo = null; // object this variable causes conflict when using setTimeout or setInterval
Try this (read my comments in code):
function doMove(id) {
return function() {
var foo = document.getElementById(id);//Global variable causes conflict when using `setTimeout` or `setInterval`
foo.style.left = parseInt(foo.style.left)+1+'px';
setTimeout(doMove(id),20); //setTimeout need an valid function
if(foo.style.left == "600px") {
foo.style.left = 0;
}
}
}
function init() {
for(i=0;i<15;i++){
var foo = document.getElementById('fooObject' + i);
foo.style.left = -i*55+'px';
doMove('fooObject' + i)(); //doMove need "()", because not run "direct"
console.log('fooObject' + i);
}
}
I modified the code for effect "carousel" and fix the problem with "left" (In 1 to 5 div):
function carrousel() {
var numberOfItems = 15; //Change this if needed
var delay = 1; //Change delay time if needed
var limitArea = 599; //Size limit your carousel area (600px, used 599 for limit with `>`)
var sizeObject = 58; //Width size of objects
//Don't change
var index = 0; //Current index
var allItems = []; //Indexed objects
//Get and index all objects
for(index = 0; index < numberOfItems; index++){//user var
allItems[index] = document.getElementById("fooObject" + index);
}
//Convert position left for int and detect "NaN"
var getPosition = function(pos) {
pos = parseInt(pos);
if (isNaN(pos)) {
return parseInt(-(index * sizeObject));
} else {
return parseInt(pos + 1);
}
};
var doMoveAll = function() {
var foo, post;
for(index = 0; index < numberOfItems; index++){//user var
foo = allItems[index];//Current object
pos = getPosition(foo.style.left);//Get position
//Detect if object are in out area
if(pos > limitArea) {
var beforeItem;
//Get before object for effect carousel
switch(index + 1) {
case 1:
beforeItem = "fooObject" + (numberOfItems - 1);
break;
default:
beforeItem = "fooObject" + (index - 1);
}
//Get position again, but used before object
pos = (
getPosition(document.getElementById(beforeItem).style.left) - sizeObject
);
foo.style.left = pos + "px";
} else {
foo.style.left = pos + "px";
}
}
//Timeout delay
window.setTimeout(doMoveAll, delay);
};
doMoveAll();//Init
};
window.onload = carrousel;

Javascript Counter - How to add more values

I am new to javascript and have come up with this counter
Here is the JSFiddle - http://jsfiddle.net/ep6s616z/
and below is the javascript
I'm trying to figure out how i could add more values so that it goes up to 20 on the counter tool.
It must stay the same size, so how could i achieve this?
Would the new values be hidden and slide in?
Any help would be much appreciated!
var pages = ["1", "2", "3", "4", "5", "6", "7", "8"];
$(document).ready(function () {
var num = 1;
updatePrice(num);});
function updatePrice(num) {
pages.forEach(function(entry) {
if (entry == num) {
document.getElementById(entry).className = "page-selected page";
}
else {
document.getElementById(entry).className = "page";
}
document.getElementById('circle').innerHTML = num });
}
function addOne() {
var current = document.getElementsByClassName('page page-selected')[0].id
if (current < 8) {
current++;
updatePrice(current);
}
}
function takeOne() {
var current = document.getElementsByClassName('page page-selected')[0].id
if (current > 1) {
current--;
updatePrice(current);
}
}
I did a complete re-write as the events are far cleaner in pure jQuery. I also added an extra div to allow the scrolling you wanted:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/ep6s616z/18/
Simpler HTML:
<div id="circle">1</div>
<div id="calculator">
<div id="slider-left">-</div>
<div id="slider">
<div id="page-holder"></div>
</div>
<div id="slider-right">+</div>
</div>
Code:
// Generate n items
for (var i = 1; i <= 20; i++) {
$('#page-holder').append($('<div>').addClass('page').text(i));
}
// Outer slider - allows for scroller
var $slider = $('#slider');
// Actual page holding div
var $pageHolder = $('#page-holder');
// Listen for clicks on any .page divs
$pageHolder.on('click', 'div.page', function (e) {
var $div = $(this);
$('#page-holder .page').removeClass('page-selected');
$div.addClass('page-selected');
$('#circle').html($div.html());
// Ensure the element is visible - of not scrill it into view
var offset = $div.offset();
// Range of the visible area
var areaStart = $slider.scrollLeft();
var areaEnd = areaStart + $slider.width();
var x = ($div.index() + 1) * $div.outerWidth();
if (offset.left > areaEnd)
{
$slider.animate({scrollLeft: x}, 100);
}
else if (offset.left < areaStart)
{
$slider.animate({scrollLeft: x-$div.width()}, 100);
}
});
$('#slider-right').click(function () {
var $div = $('#page-holder .page-selected');
if (!$div.length) {
$div = $('#page-holder .page:first');
} else {
$div = $div.next();
}
$div.trigger('click');
});
$('#slider-left').click(function () {
var $div = $('#page-holder .page-selected');
if (!$div.length) {
$div = $('#page-holder .page').last();
} else {
$div = $div.prev();
}
$div.trigger('click');
});
Note: the scrolling to keep it in view is not perfect, as the range checks are not quite right, but you need something to do. :)
i guess this is what u wanted . i have added a button control for adding the more limits on the slider.
HTML code
<input id="add" name="addmore" type="button" value="AddMore" />
Jquery code:
$('#add').on('click', function () {
var divEle = '<div onClick="updatePrice(this.id)" id="' + count + '" class="page">' + count + '</div>';
$('#calculator').width($('#calculator').width() + sizeCounter);
$('#page-holder').width($('#page-holder').width() + sizeCounter);
$('#page-holder').append(divEle);
pages[count - 1] = count;
count++;
if (count == 10) sizeCounter += 4;
});
Live Demo :
http://jsfiddle.net/dreamweiver/ep6s616z/23/
Happy Coding :)

Categories