Laravel | same button with different functions - laravel

I have two buttons that will open a modal, but I don't know how to differentiate them to open the right modal.
It's an accept button and a reject button.
<a id='aceitar' href="{{route('despesas.modal', $item)}}">
<i class="fas fa-check text-info mr-1"></i>
</a>
<a href="{{route('despesas.modal2', $item)}}">
<i class="fas fa-ban text-danger mr-1"></i>
</a>
Modal acept
<?php
if (#$id != "") {
echo "<script>$('#modalaceitar').modal('show');</script>";
}
?>
modal reject
<?php
if (#$id != "") {
echo "<script>$('#modalrejeita').modal('show');</script>";
}
?>
Do I have any way to differentiate them? Because both check if the ID is different from empty
if (#$id != "")

You could use a data attribute to target the required modal.
<button class="open-modal" data-target="modal-a">Modal A</button>
<button class="open-modal" data-target="modal-b">Modal B</button>
<div id="modal-a" class="hidden">Modal A</div>
<div id="modal-b" class="hidden">Modal B</div>
The .hidden class applied above simply sets the css display property to none.
.hidden {
display: none;
}
Then using some JavaScript, you can apply click event handlers to each button by using querySelectAll to find all button elements with the given class name and then when a button is clicked, read the data-target dataset and toggle (or remove) the display property for that modal.
let buttons = document.querySelectorAll('.open-modal');
buttons.forEach(button => {
button.addEventListener('click', function (event) {
document.getElementById(event.target.dataset.target).classList.toggle('hidden');
});
});
Example JSFiddle here

Related

to get data in a modal in Laravel

Is there any way to get the data inside a modal? I know that it is possible to work with this through Jquery, but I would like to know if there is any way to work with Laravel itself. In a view, we can do this:
return view ('admin/users/index', ['datas1' => $ datas1, 'datas2' => $datas2];
but for a modal, is it possible to do something like this? to send my data through the controller and within my modal I check if this data are coming empty or filled, as this answer I just want to show different things, like this:
$dep = User::query()->where('dep_link', $id)->get();
 return['status' => 'success', 'dep' => $dep];
only my validation in the modal:
<div class="row" style="padding: 28px 20px;">
<div class="col-12">
#if(empty($dep))
<div class="position-relative">
<label for="rg_titular">Selecione o RG do titular (.png .jpg .pdf)</label><br>
<input type="file" name="rg_titular" class="file-input"/>
<button type="button" class="file-button">Selecionar arquivo</button>
<span class="text-arquivo">Arquivo selecionado!</span>
</div>
#else
<a href="link" target="_blank">
Visualizar RG do titular
</a>
#endif
</div>
</div>
Is there any way I can get this data inside the modal window and work with them? I just want to check if it's coming empty or not, that's all.
You can write a view composer for the modal blade file.
https://laravel.com/docs/5.8/views#view-composers
In your service provider (app, or a custom one you make)
public function boot()
{
view()->composer('myBladeFileName', function ($view) {
$activeUsers = \App\User::where('active', true)->get();
$view->with(compact('activeUsers'));
});
}

My function is deleting a different row to the one I intended, Laravel 5.4?

When I press remove, it removes the last charity in the database that belongs to the current user.
View (Button that deletes):
<a href="#"> <button class="btn btn-danger pull-right btnPopover" data-
toggle="popover" data-placement="top"> Remove </button> </a>
View (JS that is called):
function ConfirmDelete()
{
true;
}
$(document).ready(function()
{
$('.btnPopover').popover(
{
html: 'true',
title: '<strong> Are you sure you want to Remove? </strong>',
content: '<form style="display: inline;" action="{{
URL::to('remove', array($favourite->id)) }} "> <button class = "btn
btn-success glyphicon glyphicon-ok" onclick="return
ConfirmDelete()"> Yes </button> </form> <button class = "btn btn-
danger glyphicon glyphicon-remove"> No </button> '
});
$('.btnPopover').on('click', function (e)
{
$('.btnPopover').not(this).popover('hide');
});
Route:
Route::get('remove/{id}', 'HomeController#removeFavourite');
Controller (removeFavourite function):
public function removeFavourite($id)
{
Favourites::where('id', $id)->delete();
Session::flash('flash_message', 'Removed successfully!');
return back();
}
The strange thing is, is that I am using the exact same function and JS call in another part of the application and it is working fine!
The problem is, that it deletes the last record belonging to that user in the database.
Thanks
I don't know exactly what your code looks like, but the problem is that your JS is only valid for the last iteration of the loop, which sounds like what you're experiencing.
To fix it, you can move the form inside the popover button so that you can get the correct form link:
Example (moving the form):
<button class="btn btn-danger pull-right btnPopover" data-toggle="popover" data-placement="top" data-content='<form style="display: inline;" action="{{ URL::to('remove', array($favourite->id)) }} "> <button class = "btn btn-success glyphicon glyphicon-ok" onclick="return ConfirmDelete()"> Yes </button> </form> <button class = "btn btn-danger glyphicon glyphicon-remove"> No </button> '> Remove </button>
JS:
$(document).ready(function()
{
$('.btnPopover').popover(
{
html: 'true',
title: '<strong> Are you sure you want to Remove? </strong>',
});
$('.btnPopover').on('click', function (e)
{
$('.btnPopover').not(this).popover('hide');
});
....
This could be a little cleaner if you could access the parent button from the popover, but I couldn't find a way to do that in the API.
Check whether the $id you retrieve in your program(I assume there is some code missing in the question which finds the $id) is the one you want to delete.
Level official documentation says to use following code for deleting models:
$flight = App\Flight::find(1);
$flight->delete();
Find retrieves single model, and where returns a collection, so that might be the issue.
Also make sure that you pass the specific $id of the favourite that needs to be deleted, if not you'll always be deleting the last $id in your loop.
Official Docs

Header Cart information will change to default when click on addtocart button in opencart

I have change Header cart in formation like remove cart icon and add
HTML tag on it.
language/english/common/cart.php
$_['text_items'] = '<p>Mycart</p><span><em>%s</em> item(s) - %s</span>';
common/cart.tpl
<button type="button" data-toggle="dropdown" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-inverse btn-block btn-lg dropdown-toggle"><!-- <i class="fa fa-shopping-cart"></i> --><span id="cart-total"><?php echo $text_items; ?></span></button>
It work fine but when i click on addtocart button it load using Ajax
and the previous modified cart tags reset to default. when refresh it
again work. any one knew about this.
It is happened due to when we click on addtocart button on product
detial page it will call call function on click event and load
checkout/cart using ajax so we need to make synchronize common/cart
information and chekout/cart infourmation
I solve the issues as
follows:
in product.tpl or other page where you add addtocart
inside
$('#button-cart').on('click', function() {
we will find
if (json['success']) {
$('.breadcrumb').after('<div class="alert alert-success">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart > button').html('<!--<i class="fa fa-shopping-cart"></i>--> ' + json['total']);//here you can get the cart icon and comment out he icon code
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
for add other tag inside cart info modify on
language/english/checkout/cart.php
$_['text_items'] = '<p>Mycart</p><span><em>%s</em> item(s) - %s</span>';
and it work for me

i have a div in an a,but how can i trigger event on div and prevent the event on <a>?

this is my first question on stack,i am not good at English,sorry for that.
my code can't stop jumping when i click the div.
list is a div contains some details.
my js:
function clickList(e)
{
var listAll=e.parentNode.childNodes;
var list=listAll[1];
console.log(list);
if (list.style.display == "none")
{
list.style.display = "block";
}
else
{
list.style.display = "none";
}
e.stopPropagation();
}
my html:
<a href="an address">
<div class="pull-right" onclick="clickList(this)">
<i class="iconfont"></i>
</div>
</a>
As you are stopping the event propagation, you can just have javascript:void() as below in href instead of address:
<a href="javascript:void(0);">
<div class="pull-right" onclick="clickList(this)">
<i class="iconfont"></i>
</div>
</a>
Also Remove e.stopPropagation(); in clickList as this will be the div object and not an event object.

Bootstrap menu collapse in ajax site. How can i close menu on every click?

my website uses Ajax and i want to close the Bootstrap's collapsed menu every time a user click click on every menu item, like this code. But this code doesn't work!! Damn!
$('.menu-item').click(function() {
$('.collapse').collapse('hide');
});
How can I fix it?
(The code of menu is a simply bootstrap 3.0.0):
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
And the code of menu:
<div class="col-md-6 downmy">
<div class="navbar-collapse in" style="height: auto;">
<div class="menuTop"><ul id="menu-primary" class="nav nav-pills"><li id="menu-item-605" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-605">Home</li>
<li id="menu-item-314" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-314">My Posts</li>
<li id="menu-item-315" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-315">Guestbook</li>
</ul>
</div>
</div>
</div>
When an element with class '.collapse' collapse or become visible the '.collapse' class will be replace by the .in class. So you will have to apply your hide on the .in class too.
$('.menu-item').click(function() {
$('.in').collapse('hide');
});
Your menu items contain an anchor, when click this a new page will open... so i don't understand your question. To prevent this add an event.preventDefault (http://api.jquery.com/event.preventDefault/) to your code:
$('.menu-item').click(function(event) {
event.preventDefault();
$('.in').collapse('hide');
});
The following query worked fine for me :
//The ":not('.dropdown-toggle')" is for the drop down menu (like a language menu) because
// I didn't want my navigation to close when clicking sub menu
$(".nav li a:not('.dropdown-toggle')").on('click',function(){
$('.navbar-collapse.in').collapse('hide');
});
You can add it to your page or in the $(document).ready(function(){/*...*/}); method

Resources