Selecting First Tab - Laravel - php

i want to have my first tab selected when page loads or refreshes. In my code, the content of the first tab shows alright but the tab doesn't show it is selected although its content is showing.
Why is this happening please?
<input type="hidden" name="currentTab" value="0"/>
<div class="row">
<div class="col-md-9">
<div class="nav-tabs-custom" id="tabs">
<ul class="nav nav-tabs">
#foreach($students as $student)
<li ><a href="#tab_{{ $student->id }}" data-toggle="tab" >{!!$student->name!!}</a></li>
#endforeach
</ul>
<div class="tab-content">
#foreach($stduents as $key => $student)
<div class="tab-pane" id="tab_{{ $student->id }}">
//content
</div>
#endforeach
</div>
</div>
</div>
</div>
<script>
$( document ).ready(function() {
$("#tabs").tabs({active: document.tabTest.currentTab.value});
$('#tabs a').click(function(e) {
var curTab = $('.ui-tabs-active');
curTabIndex = curTab.index();
document.tabTest.currentTab.value = curTabIndex;
});
})
</script>

Here is a jQuery solution for that:
$(document).ready(function(){
$('.nav-tabs').find('li > a:first').click(); // **
})
** This will activate the first tab when page loads or refreshes.
If you want to do a quick test, just select any other tab than 1st tab, and execute this line in your browser's console $('.nav-tabs').find('li > a:first').click();

I see that an active class is not being set on page load to the first nav
list element.
This should set the active class to first list element.
#foreach($students as $key => $student)
<li {{ $key == 0 ? 'class="active"' : '' }}><a href="#tab_{{ $student->id }}" data-toggle="tab" >
{!!$student->name!!}</a></li>
#endforeach

Related

How To Load data using livewire instead of ajax?

i have this perfectly working example using ajax !
>>>>ajax example ..
im using this code to load data whenever click on the nav bar :
$('#tabs li #go').click(function() {
$('#containermenu').html('<img src="preloader.svg" >').show();
$('#tabs li a.activetabnow').removeClass('activetabnow');
$(this).addClass('activetabnow');
$('#containermenu').load('http://127.0.0.1:8000/events', function() {
$('#containermenu').fadeIn('slow');
});
is it possible to do the same actions using livewire laravel ?
the goal is to navigate through the menu and show data in the div whenever i click one of the nav-buttons !
if it is possible ill be happy with a suggetion or should i just go with ajax ??
Component
Update the livewire component like the following Sample
<?php
namespace Sample\Http\Livewire\Demo;
use Livewire\Component;
class Demo extends Component
{
public $activeTab = 0;
public function updateActiveTab($val){
// You can add your things when clicking tabs
$activeTab = $val;
}
Blade
Change the blade file like the following sample.
<ul class="tabs text-center w-full overflow-hidden flex">
<li class=" border-t border-b text-sm text-gray-800 w-full text-center #if($this->activeTab === 0) active #endif">Tab 1</li>
<li class=" border-t border-b text-sm text-gray-800 w-full text-center #if($this->activeTab === 1) active #endif">Tab 2</li>
<li class=" border-t border-b text-sm text-gray-800 w-full text-center #if($this->activeTab === 2) active #endif">Tab 2</li>
</ul>
<div class="container mx-auto px-4">
#if($this->activeTab === 0)
<!--
Add your first tab content here
-->
#elseif($this->activeTab === 1)
<!--
Add your second tab content here
-->
#elseif($this->activeTab === 2)
<!--
Add your third tab content here
-->
#endif
</div>

Laravel: Dynamic tabs don't show the correct data?

I am trying to use dynamic tabs in laravel and iam kinda new to it. Iam stuck in it for like 2 days.
I have two variables that getting data from tables in mysql.
$stations = Station::select('id', 'name')->get();
// return $staions;
$queues = Queue::withTrashed()
->oldest()
->get();
// return $queues;
The data in $schedules:
[
{
"id":1,
"name":"siinay"
},
{
"id":2,
"name":"jigjiga"
},
{
"id":3,
"name":"Idaacada"
},
{
"id":4,
"name":"Xero Awr"
}
]
And the data in the $queues:
So the problem is here in dynmaic tabs: I have wanted to display station names in the nav-tabs and queues in the tab-content. So i did it like this.
<ul class="nav nav-tabs">
#forelse ($stations as $station)
<li >
<a data-toggle="tab" href="#tab-{{ $station->id }}" >
{!! $station->name !!}
</a>
</li>
#empty
#endforelse
</ul>
<div style="margin-top: 15%" class="tab-content">
#foreach($queues as $queue)
#if($queue)
<div id="tab-{{ $queue->station_id}}" class="tab-pane fade">
<p>{!! $queue->bus_number !!}</p>
</div>
#else
<div>empty</div>
#endif
#endforeach
</div>
when i run the above code, it only showing the last queue bus_number per each sttions. every stations queue
Oldest is indeed the oldest, it's the opposite to first. Try this to get a collection:
$queues = Queue::withTrashed()
->orderBy('id', 'desc')
->get();
In the view (simplified):
<ul class="nav nav-tabs">
#foreach ($stations as $station)
<li>
<a data-toggle="tab" href="#tab-{{ $station->id }}">
{{ $station->name }}
</a>
</li>
#endforeach
</ul>
<div style="margin-top: 15%" class="tab-content">
#foreach($queues as $queue)
<div id="tab-{{ $queue->station_id }}" class="tab-pane fade">
<p>{{ $queue->bus_number }}</p>
</div>
#endforeach
</div>
No need to check for empty. Each one is a table record. Unless you want to check on bus_number or station_id.

Filter is not working when I click on pagination in laravel and mongo db

I have a filter functionality with pagination. Filter is working initially. When I click on pagination next button result is not showing based on filter.
eg: I have three countries in filter box. I chosen two then click on filter button then result is showing correct on first page but When I click on pagination next button all three countries result is showing not appending filter.
Thank you any help would be appreciated
Framework: laravel 5.5
Database: MongoDB
route.php
Route::match(['get', 'post'], '/search', 'SearchController#index')->name('search');
SearchController.php
public function index(SearchRequest $request)
{
$data = $request->all();
$cabin = Cabin::select('_id', 'name', 'country', 'region', 'interior', 'sleeping_place', 'height', 'other_details', 'interior')
->where('is_delete', 0)
->where('other_cabin', "0");
if(isset($request->cabinname)){
$cabin->where('name', $request->cabinname);
}
if(isset($request->country)){
$cabin->whereIn('country', $data['country']);
}
//dd($cabin->count());
if(isset($request->region)){
$cabin->whereIn('region', $data['region']);
}
$cabinSearchResult = $cabin->simplePaginate(5);
return view('searchResult', ['cabinSearchResult' => $cabinSearchResult]);
}
search.blade.php
<form action="{{ route('search') }}" method="POST" class="navbar-form navbar-left" id="search-nav-home">
{{ csrf_field() }}
<div class="form-group navbar-form navbar-left" id="prefetch">
<input type="text" class="form-control-home typeahead" name="cabinname" id="cabinname" placeholder="Search Cabin">
</div>
<ul class="nav navbar-nav" id="filter-home">
#if($services->country())
<li class="dropdown">
<!-- Dropdown in Filter -->
Country <span class="caret"></span>
<ul class="dropdown-menu dropdown-menu-home">
#foreach($services->country() as $land)
<li class="check-it-list-home"><input type="checkbox" class="check-it-home" name="country[]" value="{{ $land->name }}"> {{ $land->name }}</li>
#endforeach
</ul>
</li>
#endif
#if($services->regions())
<li class="dropdown">
Region<span class="caret"></span>
<ul class="dropdown-menu dropdown-menu-home drop-height">
#foreach($services->regions() as $region)
<li class="check-it-list-home"><input type="checkbox" name="region[]" value="{{ $region->name }}" class="check-it-home"> {{ $region->name }}
</li>
#endforeach
</ul>
</li>
#endif
</ul>
<div class="form-group navbar-form navbar-right" id="navbar-right-filter-home">
<button type="submit" class="btn btn-default-home btn-filter-home">Filter Cabins</button>
</div>
</form>
In the result View you may need to append the query link to the generated paginate link:
First in your controller, send the result of the search to the view e.g.:
....
return view('searchResult', ['cabinSearchResult' => $cabinSearchResult, 'next_query' => $data]);
Then in your View, use appends and link() method on $searchResult where you display the paginated link instead of only link() or render(), like this:
....
{{ $searchResult->appends($next_query)->links() }}
This should generate links with the query string that contains your old search query on all the pages generated.
Read more about pagination in Laravel Docs

How to active a tab when a pagination links is clicked in that tab?

When user accesses "http://proj.test/user/profile?user=1" he goes to a page like below that has 3 tabs, one for the user to edit his acccount, other (My Tickets) for the user to access information about his registrations in congresses and other (My Congresses) for the user to access the information of congresses created by him. The default tab when this page is acessed his the "Edit user account".
The tab1 content is for the user to edit his profile. When user click in "Update" the code goes to the UserController to update the user info.
When the user click in the tab2 (My Tickets) the content of this tab is like below. It shows the registrations in congresses of the user. Inside this tab content it appears also 2 tabs for the user to show his registrations in congresses that already finish and other tab to show his registrations in congresses that have not yet been performed.
Then there is the pagination if there are more than 5 results, when the user change to link "2" he goes to "http://proj.test/user/profile?page=2".
When the user click in the tab3 (My Congresses) the content of this tab is like below. It shows the congresses created by the user. Inside this tab content it appears also 3 tabs, one (Draft) for the user to show the congresses created by him that have the column "status" as "D", other (Published) to show the congresses created by the user that have the column "status" as "P" and other (Archived) to show the congresses created by the user that already finished.
Then there is the pagination if there are more than 5 results, when the user change to link "2" he goes to "http://proj.test/user/profile?page=2".
Doubt:
The issue with this context is that when a pagination link is clicked in some tab, for example if the user is in the tab "Draft" inside "My Congresses" tab, and clicks in the pagination link "2", the url changes to "http://proj.test/user/profile?page=2" but the tab that become active his the first one, the "Edit user account" tab. Then if the user clicks in "My Congresses" it appears the results of the page 2, but the user needs to manually click in the "My Congresses".
Do you know how to turn, in this case, the "My Congresses" tab active when a pagination page link is clicked in the "My Congresses" tab?
Full code on this page to show all the tabs:
<ul class="nav nav-pills" role="tablist">
<li class="">
<a class="nav-link active" href="#updateUserInfo" data-toggle="tab" role="tab">Edit user account</span></a>
</li>
<li class="disabled">
<a class="nav-link" href="#myCongresses" data-toggle="tab" role="tab">My Congresses</span></a>
</li>
<li class="disabled">
<a class="nav-link" href="#myTickets" data-toggle="tab" role="tab">My Tickets</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active clearfix" id="updateUserInfo" role="tabpanel" aria-labelledby="home-tab">
<form method="post" action="{{route('user.updateUserInfo')}}" class="clearfix">
{{csrf_field()}}
<div class="form-row">
<div class="form-group">
<label for="name">Name</label>
<input type="text" value="{{$user->name}}" name="name" class="form-control" id="name" >
</div>
<!--other form fields-->
<input type="submit" class="btn btn-primary btn" value="Update"/>
</form>
</div>
<div class="tab-pane clearfix fade" id="myCongresses" role="tabpanel" aria-labelledby="contact-tab">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active border" href="#draftCongresses" data-toggle="tab"
role="tab">Draft</a>
</li>
<li class="nav-item">
<a class="nav-link border" href="#publishedCongresses" data-toggle="tab"
role="tab">Published</a>
</li>
<li class="nav-item">
<a class="nav-link border" href="#archivedCongresses" data-toggle="tab"
role="tab">Archived</a>
</li>
</ul>
<div class="tab-content bg-white" id="myTabContent">
<div class="tab-pane fade active show clearfix" id="draftCongresses" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group">
#foreach($draftCongresses as $draftCongress)
#if(!empty($draftCongress))
<li class="list-group-item">
<p>{{$draftCongress->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{$draftCongress->name}}</h5>
Edit Congress
</li>
#endif
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$draftCongresses->links(("pagination::bootstrap-4"))}}
</div>
</div>
<div class="tab-pane fade show clearfix" id="publishedCongresses" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group">
#foreach($publishedCongresses as $publishedCongress)
#if(!empty($publishedCongress))
<li class="list-group-item">
<p>{{$publishedCongress->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{$publishedCongress->name}}</h5>
<a href="{{route('congress.edit', ['id' => $publishedCongress->id])}}"
class="btn btn-outline-primary">Edit Congress</a>
</li>
#endif
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$publishedCongresses->links(("pagination::bootstrap-4"))}}
</div>
</div>
<div class="tab-pane fade show clearfix" id="archivedCongresses" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group">
#foreach($archivedCongresses as $archivedCongress)
#if(!empty($archivedCongress))
<li class="list-group-item">
<p> {{$archivedCongress->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{$archivedCongress->name}}</h5>
<a href="{{route('congress.edit', ['id' => $archivedCongress->id])}}"
class="btn btn-outline-primary">Edit Congress</a>
</li>
#endif
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$archivedCongresses->links(("pagination::bootstrap-4"))}}
</div>
</div>
</div>
</div>
<div class="tab-pane clearfix fade" id="myTickets" role="tabpanel" aria-labelledby="contact-tab">
<div class="d-flex mb-3">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active border" id="nextCongresses" href="#nextCongresses" data-toggle="tab" role="tab">Next Congresses</a>
</li>
<li class="nav-item">
<a class="nav-link border" id="pastCongresses" href="#pastCongresses" data-toggle="tab" role="tab">Past Congresses</a>
</li>
</ul>
</div>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="nextCongresses" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group" id="nextCongressesContainer">
#foreach($nextRegistrations as $nextRegistration)
#if(!empty($nextRegistration->congress || !empty($nextRegistration->congress->start_date)))
<li class="list-group-item">
<p>{{optional($nextRegistration->congress)->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{optional($nextRegistration->congress)->name}}</h5>
<p> Registration in {{optional($nextRegistration->congress)->created_at }}</p>
</li>
#endif
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$nextRegistrations->links(("pagination::bootstrap-4"))}}
</div>
</div>
<div class="tab-pane fade show" id="pastCongresses" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group" id="pastCongressesContainer">
#foreach($pastRegistrations as $pastRegistration)
#if(!empty($pastRegistration->congress || !empty($pastRegistration->congress->start_date)))
<li class="list-group-item">
<p>{{optional($pastRegistration->congress)->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{optional($pastRegistration->congress)->name}}</h5>
<p> Registration in {{$pastRegistration->created_at}}</p>
</li>
#endif
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$pastRegistrations->links(("pagination::bootstrap-4"))}}
</div>
</div>
</div>
</div>
</div>
UserController:
class UserController extends Controller
{
public function index(Request $request){
$pageLimit = 5;
$user = $request->user();
$pastRegistrations = $user->registrations()->with(['congress' => function ($query) {
$query->where('end_date', '<', now());
}])->paginate($pageLimit);
$nextRegistrations = $user->registrations()->with(['congress' => function ($query) {
$query->where('end_date', '>', now());
}])->paginate($pageLimit);
$draftCongresses = $user->congresses()->where('status','D')->paginate($pageLimit);
$pastCongresses = $user->congresses()->where('end_date','<', now())->paginate($pageLimit);
$publishedCongresses = $user->congresses()->where('status','P')->paginate($pageLimit);
return view('users.index',
compact('user', 'pastRegistrations','nextRegistrations', 'draftCongresses', 'pastCongresses', 'publishedCongresses'));
}
}
Example of the issue
If pagination link "2" is clicked here:
Instead of appear the page with the "My Congresses" tab active like the image above but with the pagination link "2" active instead of the pagination link "1" active, it appaers the page with the default active tab "Edit user account":
Since you're using Bootstrap tabs, you need to append the right fragment to your pagination links in order to maintain the active tab.
For exemple you can do something like this
{{ $draftCongresses->fragment('myCongresses')->links("pagination::bootstrap-4") }}
You can see the doc here
Change these:
{{$draftCongresses->links(("pagination::bootstrap-4"))}}
{{$publishedCongresses->links(("pagination::bootstrap-4"))}}
{{$archivedCongresses->links(("pagination::bootstrap-4"))}}
{{$nextRegistrations->links(("pagination::bootstrap-4"))}}
{{$pastRegistrations->links(("pagination::bootstrap-4"))}}
..to these: (i.e. add the corresponding link fragment)
{{$draftCongresses->fragment('draftCongresses')->links(("pagination::bootstrap-4"))}}
{{$publishedCongresses->fragment('publishedCongresses')->links(("pagination::bootstrap-4"))}}
{{$archivedCongresses->fragment('archivedCongresses')->links(("pagination::bootstrap-4"))}}
{{$nextRegistrations->fragment('nextCongresses')->links(("pagination::bootstrap-4"))}}
{{$pastRegistrations->fragment('pastCongresses')->links(("pagination::bootstrap-4"))}}
Then add this JavaScript/jQuery script to the page:
<script>
jQuery( function( $ ){
// List of tab IDs.
var tabs = {
// PARENT_TAB_ID: [ 'ID_OF_DIRECT_CHILD_TAB', 'ID_OF_DIRECT_CHILD_TAB', ... ]
updateUserInfo: [],
myCongresses: [ 'draftCongresses', 'publishedCongresses', 'archivedCongresses' ],
myTickets: [ 'nextCongresses', 'pastCongresses' ]
};
// Default tab's ID.
var default_tab = 'updateUserInfo';
function showTab( parent_id, child_id ) {
$( 'a[data-toggle="tab"][href="#' + parent_id + '"]' ).first().click();
if ( child_id ) {
$( 'a[data-toggle="tab"][href="#' + child_id + '"]' ).first().click();
}
}
$( window ).on( 'load hashchange', function(){
var tab_id = location.hash || '';
// Remove the hash (i.e. `#`)
tab_id = tab_id.substring(1);
if ( tab_id && tabs[ tab_id ] ) {
showTab( tab_id );
} else if ( tab_id ) {
$.map( tabs, function( child_ids, parent_id ){
if ( $.inArray( tab_id, child_ids ) > -1 ) {
showTab( parent_id, tab_id );
}
} );
} else {
showTab( default_tab );
}
} );
} );
</script>

PHP or Blade - Organized Columns depending on the number of data

I'm blocked for organize my sub-menu of my menu. As you can see,
it is decomposed into several blocks (i've use paint for delimited the block in black).
currently my code is:
<li class="dropdown yamm-fw">
{{ $category->nom }} <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<div class="yamm-content">
<div class="row">
<div class="col-sm-3">
<h5>test</h5>
<ul>
<?php $count=0 ?>
#foreach ($types->whereIn('id', $category->produit->unique('type_id')->pluck('type_id')) as $type)
#if ($count <= 6)
<li>{{$type->nom}}</li>
<?php $count++ ?>
#endif
#endforeach
</ul>
</div>
<div class="col-sm-3">
<h5>teste 2</h5>
<ul>
<li>Trainers</li>
<li>Sandals</li>
<li>Hiking shoes</li>
<li>Casual</li>
</ul>
</div>
I do not know how to do that after 7 items in the actual div col-sm-3, create another div to continue the foreach and so on.
You can use the $loop variable:
#if ($loop->iteration === 7)
// Create another div here
#endif

Categories