-
Notifications
You must be signed in to change notification settings - Fork 0
Add files via upload #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: js-12
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
body { | ||
padding-top: 80px; | ||
} | ||
|
||
.show-cart li { | ||
display: flex; | ||
} | ||
.card { | ||
margin-bottom: 20px; | ||
} | ||
.card-img-top { | ||
width: 200px; | ||
height: 200px; | ||
align-self: center; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[{ | ||
"name": "Orange", | ||
"description": "Orange", | ||
"img": "http://www.azspagirls.com/files/2010/09/orange.jpg", | ||
"nasklade": "73", | ||
"price": "0.5", | ||
"data-price": "0.5" | ||
|
||
}, { | ||
"name": "Banana", | ||
"description": "Banana", | ||
"img": "http://images.all-free-download.com/images/graphicthumb/vector_illustration_of_ripe_bananas_567893.jpg", | ||
"nasklade": "49", | ||
"price": "1.22", | ||
"data-price": "1.22" | ||
|
||
}, { | ||
"name": "Lemon", | ||
"description": "Lemon", | ||
"img": "https://3.imimg.com/data3/IC/JO/MY-9839190/organic-lemon-250x250.jpg", | ||
"nasklade": "100", | ||
"price": "5", | ||
"data-price": "5" | ||
|
||
}] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> | ||
<link rel="stylesheet" href="1.css"> | ||
</head> | ||
<body> | ||
<!-- Nav --> | ||
<nav class="navbar navbar-inverse bg-inverse fixed-top bg-faded"> | ||
<div class="row"> | ||
<div class="col"> | ||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#cart">Cart (<span class="total-count"></span>)</button><button class="clear-cart btn btn-danger">Clear Cart</button></div> | ||
</div> | ||
</nav> | ||
|
||
<div class="container"> | ||
<div class="row"></div> | ||
</div> | ||
|
||
<!-- Modal --> | ||
<div class="modal fade" id="cart" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> | ||
<div class="modal-dialog modal-lg" role="document"> | ||
<div class="modal-content"> | ||
<div class="modal-header"> | ||
<h5 class="modal-title" id="exampleModalLabel">Cart</h5> | ||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
</div> | ||
<div class="modal-body"> | ||
<table class="show-cart table"> | ||
|
||
</table> | ||
<div>Total price: $<span class="total-cart"></span></div> | ||
</div> | ||
<div class="modal-footer"> | ||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> | ||
<button type="button" class="btn btn-primary">Order now</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
<script src="main.js"></script> | ||
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
|
||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', `1.json`, true); | ||
xhr.send(null); | ||
xhr.onload = () => { | ||
const data = JSON.parse(xhr.responseText); | ||
console.log(data); | ||
renderList(data); | ||
} | ||
|
||
|
||
function renderList(SetList) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Форматирование кода! |
||
SetList.forEach(Set => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Переменные с маленькой буквы называйте |
||
const ul = document.querySelector('.container .row'); | ||
ul.insertAdjacentHTML('beforeend', ` | ||
<div class="col"> | ||
<div class="card" style="width: 20rem;"> | ||
<img class="card-img-top" src="${Set.img}" alt="Card image cap"> | ||
<div class="card-block"> | ||
<h4 class="card-title">${Set.name}</h4> | ||
<p class="card-text">Price: $${Set.price}</p> | ||
<p>${Set.description}</p> | ||
<p>${Set.nasklade}</p> | ||
<a href="#" data-name="${Set.name}" data-price="${Set.price}" class="add-to-cart btn btn-primary">Add to cart</a> | ||
</div> | ||
</div> | ||
`); | ||
|
||
}) | ||
var shoppingCart = (function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const / let |
||
// ============================= | ||
// Private methods and propeties | ||
// ============================= | ||
cart = []; | ||
|
||
// Constructor | ||
function Item(name, price, count) { | ||
this.name = name; | ||
this.price = price; | ||
this.count = count; | ||
} | ||
|
||
// Save cart | ||
function saveCart() { | ||
localStorage.setItem('shoppingCart', JSON.stringify(cart)); | ||
} | ||
|
||
// Load cart | ||
function loadCart() { | ||
cart = JSON.parse(localStorage.getItem('shoppingCart')); | ||
} | ||
if (localStorage.getItem("shoppingCart") != null) { | ||
loadCart(); | ||
} | ||
|
||
|
||
// ============================= | ||
// Public methods and propeties | ||
// ============================= | ||
var obj = {}; | ||
|
||
// Add to cart | ||
obj.addItemToCart = function(name, price, count) { | ||
for(var item in cart) { | ||
if(cart[item].name === name) { | ||
cart[item].count ++; | ||
saveCart(); | ||
return; | ||
} | ||
} | ||
var item = new Item(name, price, count); | ||
cart.push(item); | ||
saveCart(); | ||
} | ||
// Set count from item | ||
obj.setCountForItem = function(name, count) { | ||
for(var i in cart) { | ||
if (cart[i].name === name) { | ||
cart[i].count = count; | ||
break; | ||
} | ||
} | ||
}; | ||
// Remove item from cart | ||
obj.removeItemFromCart = function(name) { | ||
for(var item in cart) { | ||
if(cart[item].name === name) { | ||
cart[item].count --; | ||
if(cart[item].count === 0) { | ||
cart.splice(item, 1); | ||
} | ||
break; | ||
} | ||
} | ||
saveCart(); | ||
} | ||
|
||
// Remove all items from cart | ||
obj.removeItemFromCartAll = function(name) { | ||
for(var item in cart) { | ||
if(cart[item].name === name) { | ||
cart.splice(item, 1); | ||
break; | ||
} | ||
} | ||
saveCart(); | ||
} | ||
|
||
// Clear cart | ||
obj.clearCart = function() { | ||
cart = []; | ||
saveCart(); | ||
} | ||
|
||
// Count cart | ||
obj.totalCount = function() { | ||
var totalCount = 0; | ||
for(var item in cart) { | ||
totalCount += cart[item].count; | ||
} | ||
return totalCount; | ||
} | ||
|
||
// Total cart | ||
obj.totalCart = function() { | ||
var totalCart = 0; | ||
for(var item in cart) { | ||
totalCart += cart[item].price * cart[item].count; | ||
} | ||
return Number(totalCart.toFixed(2)); | ||
} | ||
|
||
// List cart | ||
obj.listCart = function() { | ||
var cartCopy = []; | ||
for(i in cart) { | ||
item = cart[i]; | ||
itemCopy = {}; | ||
for(p in item) { | ||
itemCopy[p] = item[p]; | ||
|
||
} | ||
itemCopy.total = Number(item.price * item.count).toFixed(2); | ||
cartCopy.push(itemCopy) | ||
} | ||
return cartCopy; | ||
} | ||
|
||
// cart : Array | ||
// Item : Object/Class | ||
// addItemToCart : Function | ||
// removeItemFromCart : Function | ||
// removeItemFromCartAll : Function | ||
// clearCart : Function | ||
// countCart : Function | ||
// totalCart : Function | ||
// listCart : Function | ||
// saveCart : Function | ||
// loadCart : Function | ||
return obj; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://codepen.io/Dimasion/pen/oBoqBM |
||
})(); | ||
|
||
|
||
// ***************************************** | ||
// Triggers / Events | ||
// ***************************************** | ||
// Add item | ||
$('.add-to-cart').click(function(event) { | ||
event.preventDefault(); | ||
var name = $(this).data('name'); | ||
var price = Number($(this).data('price')); | ||
shoppingCart.addItemToCart(name, price, 1); | ||
displayCart(); | ||
}); | ||
|
||
// Clear items | ||
$('.clear-cart').click(function() { | ||
shoppingCart.clearCart(); | ||
displayCart(); | ||
}); | ||
|
||
|
||
function displayCart() { | ||
var cartArray = shoppingCart.listCart(); | ||
var output = ""; | ||
for(var i in cartArray) { | ||
output += "<tr>" | ||
+ "<td>" + cartArray[i].name + "</td>" | ||
+ "<td>(" + cartArray[i].price + ")</td>" | ||
+ "<td><div class='input-group'><button class='minus-item input-group-addon btn btn-primary' data-name=" + cartArray[i].name + ">-</button>" | ||
+ "<input type='number' class='item-count form-control' data-name='" + cartArray[i].name + "' value='" + cartArray[i].count + "'>" | ||
+ "<button class='plus-item btn btn-primary input-group-addon' data-name=" + cartArray[i].name + ">+</button></div></td>" | ||
+ "<td><button class='delete-item btn btn-danger' data-name=" + cartArray[i].name + ">X</button></td>" | ||
+ " = " | ||
+ "<td>" + cartArray[i].total + "</td>" | ||
+ "</tr>"; | ||
} | ||
$('.show-cart').html(output); | ||
$('.total-cart').html(shoppingCart.totalCart()); | ||
$('.total-count').html(shoppingCart.totalCount()); | ||
} | ||
|
||
// Delete item button | ||
|
||
$('.show-cart').on("click", ".delete-item", function(event) { | ||
var name = $(this).data('name') | ||
shoppingCart.removeItemFromCartAll(name); | ||
displayCart(); | ||
}) | ||
|
||
|
||
// -1 | ||
$('.show-cart').on("click", ".minus-item", function(event) { | ||
var name = $(this).data('name') | ||
shoppingCart.removeItemFromCart(name); | ||
displayCart(); | ||
}) | ||
// +1 | ||
$('.show-cart').on("click", ".plus-item", function(event) { | ||
var name = $(this).data('name') | ||
shoppingCart.addItemToCart(name); | ||
displayCart(); | ||
}) | ||
|
||
// Item count input | ||
$('.show-cart').on("change", ".item-count", function(event) { | ||
var name = $(this).data('name'); | ||
var count = Number($(this).val()); | ||
shoppingCart.setCountForItem(name, count); | ||
displayCart(); | ||
}); | ||
|
||
displayCart(); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Используйте классы колоночной верстки