I have a javascript function that filters categories. There is also interleaving of lists in php.blade.
The problem is that alternation only works when filtering has All, and when we select a specific category, alternation does not work. How can this be fixed?
JavaScript
$('.category-filter_item').click(function(){
$('.category-filter_item').removeClass('active')
$(this).addClass('active')
var dataFilter = $(this).attr('data-filter');
$('.blog-list').hide()
$(dataFilter).show()
})
php.blade
@extends('layouts.app')
@section('content')
<div class="container">
<div class="category-filter" id="filter">
<div class="category-filter_item active" data-filter="*">All</div>
@foreach($categories as $category)
<div class="category-filter_item" data-filter=".category_{{$category->id}}">{{ $category->title }}</div>
@endforeach
</div>
@foreach ($blogs as $index => $blog)
<div class="blog-list">
@if ($index % 2 === 1) //Alternation
<div class="blog blog--left" >
<h2 class="blog_title">{{ $blog->title }}</h2>
</div>
@else
<div class="blog blog--right">
<h2 class="blog_title">{{ $blog->title }}</h2>
</div>
@endif
</div>
@endforeach
</div>
@endsection
Controller
public function index()
{
$blogs = Blog::all();
$categories = Category:all();
return view('blog', compact('blogs', 'categories'));
}
CodePudding user response:
You can use :nth-child(odd)
and :nth-child(even)
to select the relevant .post-list
container, and then hide the relevant side:
.post-list:nth-child(even) .post-left,
.post-list:nth-child(odd) .post-right {
display: none;
}
<div class="post-list">
<div class="post post-left">
<h2 class="post_title">L1</h2>
</div>
<div class="post post-right">
<h2 class="post_title">R1</h2>
</div>
</div>
<div class="post-list">
<div class="post post-left">
<h2 class="post_title">L2</h2>
</div>
<div class="post post-right">
<h2 class="post_title">R2</h2>
</div>
</div>
<div class="post-list">
<div class="post post-left">
<h2 class="post_title">L3</h2>
</div>
<div class="post post-right">
<h2 class="post_title">R3</h2>
</div>
</div>
CodePudding user response:
You don't need to create two separate div
for left and right posts
just make a single div
and style that as below
.post { /* right post styling here */ }
.post:nth-child(odd) { /* left post styling here */ }
You can change the left and right position according to your need
.post {
background: green;
text-align: right;
}
.post:nth-child(odd) {
background: red;
text-align: left;
}
<div class="post-list">
<div class="post" >
<h2 class="post_title">post 1</h2>
</div>
<div class="post">
<h2 class="post_title">post 2</h2>
</div>
<div class="post">
<h2 class="post_title">post 3</h2>
</div>
<div class="post" >
<h2 class="post_title">post 4</h2>
</div>
<div class="post">
<h2 class="post_title">post 5</h2>
</div>
<div class="post">
<h2 class="post_title">post 6</h2>
</div>
</div>