Skip to content

Commit

Permalink
Products pagination based on ajax calls was added
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgefortzz committed Apr 30, 2017
1 parent 5815e40 commit dd9f48f
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 85 deletions.
8 changes: 6 additions & 2 deletions app/Http/Controllers/SubcategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\Subcategory;
use Illuminate\Http\Request;

class SubcategoryController extends Controller
{
public function showAllProducts(Subcategory $subcategory){
public function showAllProducts(Subcategory $subcategory, Request $request){

$categories = Category::all();
$category = $subcategory->category;
$products = $subcategory->products;
$products = Product::where('subcategory_id', $subcategory->id)->paginate(30);

if ($request->ajax()) {
return view('products.products_list', compact('products'))->render();
}
return view('subcategories.subcategory_products', compact('categories','category','subcategory','products'));
}
}
2 changes: 1 addition & 1 deletion database/seeds/ProductsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function run()

// $subcategories = Subcategory::all();
// foreach ($subcategories as $subcategory) {
// factory(Product::class, 3)->create([
// factory(Product::class, 120)->create([
// 'subcategory_id' => $subcategory->id
// ]);
// }
Expand Down
31 changes: 23 additions & 8 deletions public/css/app.css

Large diffs are not rendered by default.

Binary file added public/images/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions public/js/pagination_ajax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
$(function() {

$('.pagination a').on('click', function (e) {
e.preventDefault();

var url = $(this).attr('href');
getProducts(url);
})

});

function getProducts(url) {

$.ajax({
type: 'GET',
url: url,

beforeSend: function() {
$('#products').css('opacity', '0.1');
$('#products').append('<img id="loading" style="position: absolute; left: 0; top: 0; z-index: 100000;" src="/images/loading.gif" />');
},
complete: function(){
$('#products').css('opacity', '1');
$('#looading').remove();
},
success: function(data){
$('#products').html(data);

// Set active class to grid when go to another page with ajax calls
$('#grid').addClass('active');
$('#list').removeClass('active');

// Attach localstorage - without page reload

if ($('#is_session').val() == ('1')){
if (localStorage.length > 0){
for (var i = 0; i < localStorage.length; i++){

var id = localStorage.getItem(localStorage.key(i));
$('#add_' + id).hide();
$('#remove_' + id).show();
}
}
else
localStorage.clear();
}
else {
localStorage.clear();
}

// Add listeners

// Again to paginate

$('.pagination a').on('click', function (e) {
e.preventDefault();

var url = $(this).attr('href');
getProducts(url);
});

// Add to cart

$('.add_to_cart').on('click', function (e) {
e.preventDefault();

var url = $(this).attr("href");

$.ajax({
type: 'POST',
url: url,

success: function(data) {
$('#removed_from_cart').hide();
$('#added_to_cart').show();
$('#added_to_cart').delay(7000).fadeOut('slow');

var $badge = $('.badge'),
count = Number($badge.text());

$badge.text(count + 1);

$('#add_' + data.id).hide();
$('#remove_' + data.id).show();

localStorage.setItem('product_' + data.id, data.id);
}
})
});

// Remove from cart

$('.remove_from_cart').on('click', function (e) {
e.preventDefault();

var url = $(this).attr("href");

$.ajax({
type: 'DELETE',
url: url,

success: function(data) {
$('#added_to_cart').hide();
$('#removed_from_cart').show();
$('#removed_from_cart').delay(7000).fadeOut('slow');

var $badge = $('.badge'),
count = Number($badge.text());

$badge.text(count - 1);

$('#remove_' + data.id).hide();
$('#add_' + data.id).show();

localStorage.removeItem('product_' + data.id);
}
})
});
}
});
}
33 changes: 26 additions & 7 deletions resources/assets/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,16 @@ body{
// Text area to write reviews

.textarea_review{
border: 2px solid black;
border-radius: 8px;
width: 70%;
height: 45px;
}
border: none;
overflow: auto;
outline: none;
resize: none;

.textarea_review:focus{
outline:none;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
width: 100%;
height: 45px;
}

.vertical-space{
Expand Down Expand Up @@ -302,4 +304,21 @@ body{
font-size: 30px;
color: #780014;
background-color: white;
}

.info{
color: darkred;
font-size: 10px;
}

// Pagination

.pagination-centered {
text-align: center;
}

.pagination > li >a, .pagination>li>span {
border: none;
border-radius: 50%;
margin: 0 5px;
}
48 changes: 33 additions & 15 deletions resources/views/product_details.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,39 @@
@if(Auth::check())
@if(!$isGiven)
<div id="write_review">
<form role="form" method="POST" action="{{ url('/products/'.$product->id.'/reviews/add')}}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('review') ? ' has-error' : '' }}" style="margin-bottom: 0">
<label for="review_text_area"><span class="glyphicon glyphicon-pencil"></span>Write your review below:</label>
<br>
<textarea id="review_text_area" name="review" class="textarea_review" placeholder="Write here!!" required></textarea>
@if ($errors->has('review'))
<span class="help-block">
<strong>{{ $errors->first('review') }}</strong>
</span>
@endif
<div class="row">
<div class="col-sm-1">
<div class="thumbnail">
<b><i>You</i></b>
</div>
</div>
<button class="btn btn-info" type="submit">Post</button>
</form>

<div class="col-sm-5">
<div class="panel panel-review panel-default">
<div class="panel-heading panel-heading-review">
<label for="review_text_area"><span class="glyphicon glyphicon-pencil"></span>Write your review below</label>
<br>
<u><b class="info">Please write truth to help others make a good purchase</b></u>
</div>
<div class="panel-body">
<form role="form" method="POST" action="{{ url('/products/'.$product->id.'/reviews/add')}}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('review') ? ' has-error' : '' }}" style="margin-bottom: 0">
<br>
<textarea id="review_text_area" name="review" class="textarea_review" placeholder="Write here!!" required></textarea>
@if ($errors->has('review'))
<span class="help-block">
<strong>{{ $errors->first('review') }}</strong>
</span>
@endif
</div>
<button class="btn btn-info" type="submit">Post</button>
</form>
</div>
</div>
</div>
</div>
</div>
@else
<div id="review_given">
Expand All @@ -108,7 +126,7 @@
@foreach($reviews as $review)
<div class="row">
<div class="col-sm-2">
<div class="thumbnail thumbnail-user">
<div class="thumbnail">
<i>{{$review->user->name}}</i>
</div>
</div>
Expand Down
53 changes: 53 additions & 0 deletions resources/views/products/products_list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{-- Pagination links --}}
{{ $products->links() }}

<div id="load" style="position: relative;">
@foreach($products as $product)

<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="group list-group-image" src="/images/{{$product->image}}" width="200" height="200"/>
<div class="caption">
<h2 class="group inner list-group-item-heading">
<b>
{{$product->name}}
<a href="{{ url('/products/'.$product->id) }}" style="font-size: 12px">
<span class="glyphicon glyphicon-triangle-right"></span>See details
</a>
</b>
</h2>

<div class="row" style="margin-top: 50px">
<div class="col-xs-12 col-md-4 group_div">
<p><b>Buy now:</b></p>
<p class="lead_sub"><b>{{$product->price}}$</b></p>
</div>
<div class="col-xs-12 col-md-6 group_div">
<a id="add_{{$product->id}}" class="add_to_cart" href="{{ url('/cart/add/'.$product->id) }}">
<div id="add_to_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-shopping-cart"></span><b>Add to cart</b>
</div>
</a>

{{-- Hidden link - dynamically change--}}
<a id="remove_{{$product->id}}" class="remove_from_cart" href="{{ url('/cart/delete/'.$product->id) }}" hidden>
<div id="remove_from_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-remove"></span><b>Remove</b>
</div>
</a>

{{--For autheniticated users--}}
@if(Auth::check())
<a class="give_review" href="{{ url('/products/'.$product->id) }}">
<div id="give_review_btn">
<span class="glyphicon glyphicon-comment"></span><b>Give a review</b>
</div>
</a>
@endif
</div>
</div>
</div>
</div>
</div>
@endforeach
</div>
57 changes: 5 additions & 52 deletions resources/views/subcategories/subcategory_products.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,68 +48,21 @@
</div>

<div class="col-sm-10 col-md-10">

{{--List of products from subcategory--}}

<div id="products" class="row list-group">

@foreach($products as $product)

<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="group list-group-image" src="{{$product->image}}" width="200" height="200"/>
<div class="caption">
<h2 class="group inner list-group-item-heading">
<b>
{{$product->name}}
<a href="{{ url('/products/'.$product->id) }}" style="font-size: 12px">
<span class="glyphicon glyphicon-triangle-right"></span>See details
</a>
</b>
</h2>

<div class="row" style="margin-top: 50px">
<div class="col-xs-12 col-md-4 group_div">
<p><b>Buy now:</b></p>
<p class="lead_sub"><b>{{$product->price}}$</b></p>
</div>
<div class="col-xs-12 col-md-6 group_div">
<a id="add_{{$product->id}}" class="add_to_cart" href="{{ url('/cart/add/'.$product->id) }}">
<div id="add_to_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-shopping-cart"></span><b>Add to cart</b>
</div>
</a>
{{--List of products from subcategory--}}
@include('products.products_list')

{{-- Hidden link - dynamically change--}}
<a id="remove_{{$product->id}}" class="remove_from_cart" href="{{ url('/cart/delete/'.$product->id) }}" hidden>
<div id="remove_from_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-remove"></span><b>Remove</b>
</div>
</a>

{{--For autheniticated users--}}
@if(Auth::check())
<a class="give_review" href="{{ url('/products/'.$product->id) }}">
<div id="give_review_btn">
<span class="glyphicon glyphicon-comment"></span><b>Give a review</b>
</div>
</a>
@endif
</div>
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
</div>

{{-- AJAX Scripts--}}
{{-- AJAX Scripts --}}

<script src="{{ asset('js/add_to_cart_ajax.js') }}"></script>
<script src="{{ asset('js/delete_from_cart_view_products_ajax.js') }}"></script>
<script src="{{ asset('js/pagination_ajax.js') }}"></script>

{{-- Change view (grid, list)--}}
<script src="{{ asset('js/change_view.js') }}"></script>
Expand Down
Loading

0 comments on commit dd9f48f

Please sign in to comment.