How To Load data using livewire instead of ajax? - php

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>

Related

Laravel - check if aria-selected = true

in total I have 3 files which contain the following functionality:
I got a navigation bar which toggles 3 different areas.
I got a table which contains data
A file which include file 1 and 2 (master file)
So now if I open the master file I can go through all 3 areas but every are should contain different data in the table.
File 1
<div class="manage-budget-nav-wrapper text-left mx-lg mx-sm-4 mx-2">
<ul class="nav nav-tabs border-0" role="tablist">
<li class="nav-item">
<a class="nav-link px-0 mr-sm-5 mr-3 ml-0 active" id="home-tab" data-toggle="tab" href="#home-1" role="tab"
aria-controls="home-1" aria-selected="true">Offenes Gesamtguthaben</a>
</li>
<li class="nav-item">
<a class="nav-link px-0 mr-sm-5 mr-3 ml-0" id="profile-tab" data-toggle="tab" href="#profile-1" role="tab"
aria-controls="profile-1" aria-selected="false">Spenden</a>
</li>
<li class="nav-item">
<a class="nav-link px-0 mr-sm-5 mr-3 ml-0" id="profile-tab" data-toggle="tab" href="#redeem-1" role="tab"
aria-controls="redeem-1" aria-selected="false">Gutschein</a>
</li>
</ul>
<hr class="mt-0">
</div>
file 2
<div class="table-responsive">
<table class="table table-striped table-hover mb-4">
<thead>
<tr>
#if (\Request::is('admin/locals/donations'))
<th scope="col">Erwerbsdatum</th>
#elseif (\Request::is('admin/locals/dashboard'))
<th scope="col">Buchungstag</th>
#endif
</tr>
</thead>
<tbody>
{{-- #foreach transaction --}}
#if (\Request::is('admin/locals/donations') class_exist)
<tr>
{{-- date of acquisition in d.m.Y --}}
<td>{24.12.2020}</td>
</tr>
#elseif (\Request::is('admin/locals/dashboard'))
<tr>
{{-- booking day in d.m.Y --}}
<td>{24.12.2020}</td>
</tr>
#endif
{{-- #endforeach transaction --}}
</tbody>
</table>
</div>
I already got some if statement but how can I check if e.g. area one is active or got class active. Inside the navigation to check #if (aria-selected="true").
how can I check if e.g. area one is active or got class active.
Inside the navigation to check #if (aria-selected="true")
I think you misunderstood the purpose of Blade Engine. Blade files, alongside their directives are parsed by the PHP on server, not by the browser. Therefore you can't check a HTML attribute, or select them using Blade the way you do with javascript.
The question which rise is: why do you want to check if aria-selected is true? If it is to insert another tag, you should do that in javascript/jQuery (which again -- is a different layer than Blade):
$('div [aria-selected="true"]').map(function(element){
$(element).append('<p>html code</p>')
})
Blade directives = server
Javascript = browser

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>

Selecting First Tab - Laravel

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

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

Laravel: Calling method of a controller class from blade template

How can I call a method of a controller from blade template and receive the data is returned by the method. For Example:
MeetingRoomController
class MeetingRoomController extends \BaseController
{
public function index()
{
//get all the meeting room
$meetingRoomCountry = MeetingRoom::select('country')->distinct()->orderBy('country')->get();
return View::make('index')->with('meetingroom', $meetingRoomCountry);
}
public function findLocationForNavBar( $country )
{
$meetingRoomLocation = MeetingRoom::select('location')
->distinct()
->where('country', '$country')
->orderBy('country')
->get();
return View::make('index')- >with('meetingroomLocation', $meetingRoomLocation);
}
}
View - index.blade.php
<div id="dropdown-lvl1" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
#foreach($meetingroom as $key => $value)
<li class="panel panel-default" id="dropdown">
<a data-toggle="collapse" href="#dropdown-lvl2">
<span class="glyphicon glyphicon-off"></span> {{$country = $value->country}} <span class="caret"></span>
</a>
<div id="dropdown-lvl2" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
<li>Location</li>
</ul>
</div>
</div>
</li>
#endforeach
</ul>
</div>
</div>
I want to collect the location based on corresponding country. According to my logic, First of all I collected the distinct country then searched the location by findLocationForNavBar method in MeetingRoomController.
If you're using Laravel 5.1, you could try a Service Injection from your blade template.
http://laravel.com/docs/5.1/blade#service-injection
Example from the docs:
#inject('metrics', 'App\Services\MetricsService')
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
You can do all this stuff in the index() method and send location and country to view.

Categories