Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Filter data by category

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'));
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!