Using data on dispatched event and trigger div - laravel

I have a component that dispatches a browser event with an object
// Livewire Component Method
public function passToDashboard($dataId)
{
$data = Model::find($dataId);
$this->dispatchBrowserEvent('show-data', ['data' => $data]);
}
Now on my dashboard blade view i've got
<div class="some-classes" x-data="{dataDisplay:false}">
<div x-show="dataDisplay">
{{-- This is where i want to use the object --}}
{{ $data->title }}
</div>
</div>
<script>
window.addEventListener('show-data', data => {
console.log(data.detail.title); // outputs title just fine
})
</script>
The question is, how to 'unhide' the dataDisplay and how to show it with the passed data? Thanks!

You can listen for these events directly on the element using #event-name.window="dataDisplay = true"
To get the event data, you use the $event variable and it should be under $event.detail.data.title
Use x-text to get the text onto the element. See my full example below.
So in your case:
<div class="some-classes" x-data="{dataDisplay:false, title: ''}" #event-data.window="dataDisplay = true; title = $event.detail.data.title">
<div x-show="dataDisplay">
<h3 x-text="title"></h3>
</div>
</div>
The documentation for this can be found here: https://laravel-livewire.com/docs/2.x/events#browser
Do notice I changed the event name, because it does apparently not work if you start the event name with "show". When I changed to "event-data" or anything else it started working again.

Related

Pass variable from blade to blade

I'm new to uusing Laravel blade templating.
I have a bootstrap modal that I need to show on button click, also, I need to pass some values on that click event.
For example.
On my parent blade: (btw, its a nested modal blade)
modal_1.blade.php
<div class="modal" id="modal1">
...
#foreach($templates as $key => $val)
<button onclick="previewItem($templates[$key]['color'])">
</button>
#endforeach
</div>
<script>
const previewItem = (color) => {
// how to pass this `color` to the child modal blade
}
</script>
and the child blade modal
modal_2.blade.php
<div class="modal" id="modal2">
// how to access passed variable from modal1 ?
</div>
you can create a component and inject here your variables:
#component('components.updateMyUserModal' , ['modal_id' => $account->id , 'modal_title' => $account->title , 'modal_body' => $aContext, ] )
#endcomponent
one file needs to be inside the other, after that, you need to use cookie, to store the variable's value in the previous modal, when you click on the button to select the value, make by javascript a cookie that saves this value and on the next blade you recover that value and uses.
How to use:
https://www.w3schools.com/js/js_cookies.asp

How to make alert with SweetAlert in Laravel

I would like to use SweetAlert to display my data.
I did my function
public function registration()
{
Gate::authorize('admin-level');
$url = URL::signedRoute(
'register',
now()->addMinutes(20));
return redirect('users')->with('success','$url');
}
and route that goes with it
Route::get('registration', [App\Http\Controllers\UserController::class, 'registration'])->name('registration');
The problem is with message, since I downloaded SweetAlert with composer I should probably got everything working, but then when I try to execute my class with button threw the route:
<button type="button" class="btn btn-outline-primary">{{ __('Registration link') }}</button>
#if(session('succes_message'))
<div class= "alert alert-succes">
{{session('succes_message')}}
</div>
#endif
Nothing pops up(when it should)
What might be wrong with it?
When you use ->with() it means, store items in the session for the next request.
return redirect('users')->with('success', '$url');
Here comes the question. What do you do after this?
Create a notification information or an alert (popup with SweetAlert)?
If it will be used as a notification, your code has no problem. If you want to make alert (popup with SweetAlert), your understanding is wrong.
Just because the class you are using uses the name alert, doesn't mean it make an alert with SweetAlert.
To use SweetAlert, you can add JavaScript in the header or before the </body> tag:
<script>
#if($message = session('succes_message'))
swal("{{ $message }}");
#endif
</script>
Or to use SweetAlert2 :
<script>
#if($message = session('succes_message'))
Swal.fire(
'Good job!',
'{{ $message }}',
'success'
)
#endif
</script>
If you are confused about placing the script in a specific blade view, please read my answer here.

Vue.js Calling function on a rendered component

I want to create an interactive scrumboard using Laravel and Vue.js containing multiple columns and within those columns multiple tickets.
These tickets are vue components with some nice edit / delete / (un)assign developer functionality and is used on other pages as well.
I have multiple columns defined like this:
<div id="scrumboard">
<div class="scrumboard__column">
<div class="scrumboard__title">Open</div>
<div class="scrumboard__tickets_wrapper" data-status="open">
#if( $sprint->hasTicketsOfStatus("open") )
#foreach( $sprint->getTicketsByStatus("open") as $ticket )
<ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
#endforeach
#endif
</div>
</div>
<div class="scrumboard__column">
<div class="scrumboard__title">In progress</div>
<div class="scrumboard__tickets_wrapper" data-status="progress">
#if( $sprint->hasTicketsOfStatus("progress") )
#foreach( $sprint->getTicketsByStatus("progress") as $ticket )
<ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
#endforeach
#endif
</div>
</div>
<div class="scrumboard__column">
<div class="scrumboard__title">Finished</div>
<div class="scrumboard__tickets_wrapper" data-status="closed">
#if( $sprint->hasTicketsOfStatus("closed") )
#foreach( $sprint->getTicketsByStatus("closed") as $ticket )
<ticket :data="{{ $ticket->getJsonData(true) }}"></ticket>
#endforeach
#endif
</div>
</div>
</div>
And as you can see it renders a ticket component for each ticket it finds for each column.
No i have turned the scrumboard__tickets_wrapper div's into jquery ui sortable lists which allows you to swap the tickets between columns.
<script>
$(document).ready(function(){
$(".scrumboard__tickets_wrapper").sortable({
connectWith: '.scrumboard__tickets_wrapper',
receive: function(event, ui){
console.log("Switched columns");
console.log(event);
console.log(ui);
var target = $(event.target);
target.css("background-color", "#ff0000");
}
});
</script>
Everything is working so far, now my question is: how do I dynamically call the "updateStatus()" function on a ticket component once the ticket is dropped into another list?
As you can see I can get the specific element being dropped and the sortable list it's been dropped into. So I know what the new status is by grabbing the data-status property of the wrapper + I know which element was dropped.
But how do I grab the instance of the ticket component in question and call the updateStatus function to save the new status?
Thanks in advance!
Screenshot of the scrumboard
Thanks David for pointing me in the right direction. The solution to my problem was proper component nesting.
The solution was to create 3 components with proper child-component inheritence. And declaring the child-components within the template of it's parent.
This way I end up only declaring "" and the magic happens :D.
So I have made 3 components:
- scrumboard > takes scrumboardColumn as component
- scrumboardColumn > takes ticket as component
- ticket
The root vue instance also loads the ticket component since the ticket component is also used on the backlog page.
I haven't completely finished the final product but I got the sortable working by calling it from within the ready function of the scrumboardColumn component like David suggested.
Hope this helps someone in the future!

Infinite Scroll on ajax loaded content

I'm doing a WP_Query on let's say page-a.php, that page has a div called target where page-b.php is being loaded into. Page-a is a custom template and page-b is an archive page.
The structure on page A as example:
<body>
<div id="wrap">
<div class="target">
while
<div class="post">
<h1>Title</h1>
<p>Description</p>
</div>
endwhile
<div class="pagination"></div>
</div>
</div>
</body>
On page-b I only include the < post > and < pagination > divs within a regular wp loop.
Now the jQuery:
$(window).load(function() {
$('.target').infinitescroll({
navSelector : ".navigation",
// selector for the paged navigation (it will be hidden)
nextSelector : ".navigation a.next",
// selector for the NEXT link (to page 2)
itemSelector : ".post",
// selector for all items you'll retrieve
debug : true,
loading: {
finishedMsg: '<div class="alert alert-info" style="margin-top:50px"><p class="center">All posts were loaded</p></div>',
img: '',
msg: null,
msgText: "<p style='text-align:center; margin-top:50px;'><i style='font-size:60px; color:#babfc8'class='fa fa-cog fa-spin'></i></p>"
}
}, function(arrayOfNewElems){
$('.post').animate({"opacity":"1","max-height":"150px","padding":"15px 8px"},800, "jswing");
}
);
});
-If I call that script on page-a.php will only work on it, once page-b content is placed on
-If I call on page-a.php and page-b.php it works on first, then when first ajax content is loaded and then (if I apply a new filter) it won't work.
-If I call the script on my ajax response function it will work once, then if I apply another filter and target div refreshes content, I get the
Uncaught TypeError: Cannot call method 'appendTo' of null
div classes called (.target and .post) are present on the page.
What the hell am I doing wrong here. Thanks.
PS: If you need to take a look at the real scripts let me know and I will provide.
I think you need this : http://www.247techblog.com/implement-infinite-scroll-functionality-wordpress-wp-ajax-function/
Just need to call a wp ajax function

laravel 4 how to show result at the same page and how to solve circle invoke

I am totally new to laravel, I am now want to use laravel 4.
Suppose I have a page A.php, and it contains a form & a submit button. After I submit the post request to B.php, and in B.php I query data from database.
My question is I want to show my result on B.php , that it is to say the same page of A.php request, how do I write in routes.php.
My code:
master.blade.php
<!DOCTYPE HTML>
<html>
<head>
<metacharset="UTF-8">
<title>course query</title>
</head>
<body>
<div class="container">
#yield('container')
</div>
<h1 class="ret">The result is:
#yield('ret')
<input id="result" type="text"/>
</h1>
</body>
</html>
A.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::open(array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::submit('Submit')}}
{{Form::close()}}
#endsection
routes.php
Route::get('course', function(){
return View::make('course.B');
});
Route::post('csOp', function(){
// do something
//$inputCourse = Input::get('course');
//$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
//return View::make('csOp', $records);
});
As you can see, in A.php I have a form and request to csOp
Form::open(array('url' => 'csOp')
csOp is B.php, and in B.php I query data from db, and now I got the results,
but how can I put result to the page (B.php) itself? That it is to say I want to put the result to
<input id="result" type="text"/>
You know in jquery is easy, how do I use it in laravel 4 ?
And if return to csOp, absolutlly will get an error, it is in a circle. So how can I solve it ?
Thanks very much.
If you want to populate a form based on model contents, i,e. populate form using database data.
So in laravel you can use Form Model Binding. To do so, use the Form::model method. so in your case
Route::post('csOp', function(){
// do something
$inputCourse = Input::get('course');
$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
return View::make('csOp')->with('records',$records);
});
And your csOp.blade.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::model($records,array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::close()}}
#endsection
Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named course, the Courses model's course attribute would be set as the value.

Resources