How can i disable a column without disabling the action buttons? - php

By clicking the row 1. with Institute Name, the Lab of this Institute appears on the right size. So everywhere I am clicking the system appears the Labs on the right. So if I click the edit or delete button, the Labs tab continues to pop up, and after a few seconds, the Institute's edit tab opens. Any ideas?
#section('javascript')
<script>
var laboratories = '{{htmlspecialchars(json_encode($laboratories), ENT_QUOTES, 'UTF-8')}}';
laboratories = laboratories.replace( /"/g, '"' );
laboratories = JSON.parse(laboratories);
// console.log(laboratories);
function getLaboratories(id)
{
$("table#laboratories").empty();
var table = "<tr>" +
"<th>ID</th>" +
"<th>{{ __('Laboratory\'s Name') }}</th>" +
"<th>{{ __('Laboratory\'s Acronym') }}</th>" +
"<th>{{ __('Actions') }} </th>" +
"</tr>";
for(var i=0; i<laboratories[id].length; i++)
{
table += '<tr><td>'+id+'.'+(i + 1)+'</td><td>'+laboratories[id][i].name+'</td><td>'+laboratories[id][i].acronym+'</td>';
table += '<td><a alt="{{ __('Delete Laboratory')}}" title="{{ __('Edit Laboratory')}}" class="btn btn-sm btn-default" href="laboratories/'+laboratories[id][i].id+'/edit"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>' +
'<form action="laboratories/delete/'+laboratories[id][i].id+'/" method="post" class="form-delete2">{{csrf_field()}}{{method_field('DELETE')}}'+
'<button alt="{{ __('Delete Laboratory')}}" title="{{ __('Delete Laboratory')}}" class="btn btn-sm btn-default delete" id="delete_modal2" name="delete_modal2"><span class="glyphicon glyphicon-trash"></span></button></form></td></tr>';
}
$("table#laboratories").append(table);
// EXTRA
var href = $("#create_lab").attr('href');
// console.log(href);
let positiion = href.search("institute_id");
href = href.substring(0,positiion)+"institute_id="+id;
// console.log(href);
$("#create_lab").attr('href', href)
$("#create_lab").attr('disabled', false);
}
</script>
<div class="container-fluid">
<div class="row">
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading">
<span class="glyphicon glyphicon-plus"></span> {{ __('Create New Institute') }}
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered table-hover" data-toggle="dataTable" data-form="deleteForm">
<tr>
<th>ID</th>
<th>{{ __('Institute\'s Name') }}</th>
<th>{{ __('Institute\'s Acronym') }}</th>
<th>{{ __('Actions') }}</th>
</tr>
#if(count($institutes) > 0)
#foreach($institutes as $institute)
<tr onclick="getLaboratories({{$institute->id}});">
<td>{{$institute->id}}</td>
<td>{{$institute->name}}</td>
<td>{{$institute->acronym}}</td>
<td>
<a alt="{{ __('Edit Institute')}}"title=" {{ __('Edit Institute')}}" class="btn btn-sm btn-default" href="{{route('institutes.edit', $institute->id)}}"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
<form action="{{route('institutes.destroy', $institute->id)}}" method="post" class="form-delete">
{{csrf_field()}}
{{method_field('DELETE')}}
<button alt="{{ __('Delete Institute')}}" title="{{ __('Delete Institute')}}" class="btn btn-sm btn-default delete" id="delete_modal" name="delete_modal">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td>
</tr>
#endforeach
#else
<tr>
<td>{{ __('There are no institutes available.') }}</td>
</tr>
#endif
</table>
</div>
</div>
</div>
</div>

I suspect this is a javascript issue rather than a php/laravel problem, anyways: on the click handlers for the buttons you call 'stopPropagation' to stop the click event on the buttons bubbling up to fire the click event on the row. What happens is that a click on the button is ALSO a click on the row, so it reacts to both.
See this example html:
<table>
<tr><td>the row<td> <span ><input type="button" value="I am a button"/> </span></td></td></tr>
</table>
<div id="messages">
</div>
and this javascript:
$('tr').on('click', function(){
$('#messages').append('<div>You clicked the row</div>')
})
$('span').on('click', function(e){
$('#messages').append('<div>You click the button</div>')
})
As you can see. It fires the event for both the button and the row when you click the button.
if you to this instead:
$('tr').on('click', function(){
$('#messages').append('<div>You clicked the row</div>')
})
$('span').on('click', function(e){
e.stopPropagation();
$('#messages').append('<div>You click the button</div>')
})
(notice the 'e.stopProgration();') it will only fire the event for the button.

Related

Delete button not work bootstrap modal and Laravel

I created one table that list items, and I added two buttons(edit and delete) so, the button edit is working, but the button delete is not working, and I'm making me carzy because I don't find the error. this is the table:
<table id="datatable" class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th>text</th>
<th class="disabled-sorting" width="10%"></th>
<th class="disabled-sorting" width="10%"></th>
</tr>
</thead>
<tbody>
#foreach($Items as $item)
<tr>
<td>{{ $item->text }}</td>
<td><a href="{{ route('ItemsController.edit', $item->id) }}" class="btn btn-primary btn-fab btn-icon btn-round" title="Edit">
<i class="fa fa-edit"></i></a>
</td>
<td>
<a href="#" class="btn btn-primary btn-fab btn-icon btn-round btn-delete" title="Delete" data-id="{{ $item->id }}" data-toggle="modal" data-target="#modal-default" data-route="{{ route('ItemsController.destroy', $item->id) }}" data-title="{{ $item->id }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
the modal is:
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form class="" action="" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger" name="button">yes, delete</button>
</form>
</div>
</div>
</div>
</div>
javascript data:
<script>
$(document).on('click', '.btn-delete', function(){
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
})
</script>
and controller function destroy
public function destroy(Items $item) {
$item->delete();
return redirect()->route('items.index')->with('success', 'Deleted Success');
}
I checked it line by line, and I don't know what I'm wrong, please help me
Many times thanks for getting into this.
You need to trigger your modal from JS.
If it is bootstrap 4 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
$("#modal-default").modal();
If it is bootstrap 5 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
modal = new bootstrap.Modal(document.getElementById("modal-default"));
modal.show();

Make bootstrap modal create an object to be added to list

In my Laravel project, I have the following models:
Product ('name' string, 'amount' integer and 'price' decimal)
Cart ('cost' decimal)
CartProd ('unit_price' decimal, 'amount' integer, 'discount' decimal, 'total_cost' decimal and foreign keys to a product and a cart)
The idea is that, when you want to create a new cart, the CartController method creates a list of all the products, a brand new cart and an empty list in which the selected products for the cart will be added, and sends them all through the view's compact.
public function create(Request $request)
{
$product_list = Product::all();
$empty_cart = new Cart();
$selected_products = array();
return view('models.carts.create', compact('product_list','empty_cart','selected_products'));
}
public function store(Request $request){}
The view where this happens has the list where the selected products will be shown and a button to add a product to the list.
create.blade.php:
#extends('adminlte::page')
#section('content')
<div class="container"><div class="row justify-content-center"><div class="col-md-8"><div class="card">
<div class="card-header">
<h3 class="card-title"> New Cart </h3>
<div class="card-tools">
<a class="btn btn-success btn-tool" href="#" data-toggle="modal" data-target="#add">
<i class="fas fa-plus"></i> Add Product
</a>
</div>
</div><div class="card-body">
<table class="table table-striped projects table-sm">
<thead><tr><th>#</th><th>Name</th><th>Unit Cost</th><th>Amount</th><th>Discount</th><th>Final Cost</th><th></th></tr></thead>
<tbody>
#forelse($selected_products as $prodCart)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $prodCart->product->name }}</td>
<td>{{ $prodCart->product->price }}</td>
<td>{{ $prodCart->amount }}</td>
<td>{{ $prodCart->discount }}</td>
<td>{{ $prodCart->cost }}</td>
<td>
<a class="btn btn-danger btn-circle btn-sm" href="#" data-target="#delete">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
#empty
<tr><td colspan="7">Empty Cart</td></tr>
#endforelse
</tbody></table></div>
<div class="card-footer">
#if(!empty($selected_products))
<button class="btn btn-success" href="send $selected_products to CartController's store method" > Buy Cart </button>
#endif
</div></div></div></div></div>
#include('models.carts.add')
#endsection
#section('scripts')
#stop
The 'Add Product' button toggles a modal which has the displayed list of products, plus the 'amount' and 'discount' number inputs to create a CartProd with, and the button to actually add them to the list.
add.blade.php:
<!--Modal add -->
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog modal-lg"><div class="modal-content">
<div class="modal-header bg-dark">
<h5 class="modal-title" id="exampleModalLabel">Add Product to Cart</h5>
<span class="badge badge-danger" class="close" data-dismiss="modal" aria-label="Close">
<i class="fas fa-times fa-lg" style="color:#fff"></i>
</span>
</div>
<div class="modal-body">
<table class="table table-striped projects table-sm">
<thead><tr><th>#</th><th>Name</th><th>Amount</th><th>Price</th><th>Added</th><th>Discount</th><th>Amount</th><th>Add</th></tr></thead>
<tbody>
#forelse($product_list as $product)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->amount }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->created_at }}</td>
<td><input type="number" name="discount" id="discount" min="0" max="100" size="3" value="0"></td>
<td><input type="number" name="amount" id="amount" min="1" max="{{$product->amount}}" size="3" value="1"></td>
<td>
<a class="btn btn-info btn-circle btn-sm" data-datos="{{$product}}">
<i class="fa fa-plus"></i>
</a>
</td>
</tr>
#empty
<tr>
<td>No Products Available.</td>
</tr>
#endforelse
</tbody>
</table>
</div></div></div></div>
Here are the issues:
I haven't found the way to create this new CartProd instance within the modal, and send it back to the list of selected products.
How to delete said CartProd if adding it was a mistake.
How to update the selected product list every time a new product is added/deleted.
The 'Buy Cart' button to send me to the store method of the controller (where all the fancy stuff will be done to save the list of products.)
Thank you so much for your help!
You can create this model with livewire if you are familiar which has all the answers to your issues
First, wrap the modal fields with a form element
//Your modal
<form action="{{route('your_route'}}" method="post">
#csrf
<input type="number" name="discount" id="discount" min="0" max="100" size="3" value="0">
<input type="number" name="amount" id="amount" min="1" max="{{$product->amount}}" size="3" value="1">
<a class="btn btn-info btn-circle btn-sm" data-datos="{{$product}}" type="submit">
<i class="fa fa-plus"></i>
</a>
</form>
In the controller method you can redirect back to your create.blade.php with a message and the list will refresh.
public function store(Request $request)
{
// your saving logic
return redirect()->route('your_create_route')
->with('success', 'Added successfully');
}
As your question is too broad you can trigger a delete action with actioning a button by passing the product ID to delete. This is a custom approach from this. Normally you delete with the laravel's delete method #method('delete') and a POST method.
// delete link
<a href="{{route('delete_route', $product->id)}}">
Delete
</a>
// web.php
Route::get('delete/{id}','YourController#destroy')->name('delete_route');

Trying to delete a row in a table which is not the first one with jquery

I have a question regarding jquery and modal in my laravel project (here it's a blade file). I have a table of several artist, each row reference an artist. There is also a column delete. For each row of artist, when I click on delete, the modal appears asking if i am sure to cancel etc... I am able to delete the first artist but if I select another one, it doesn't work. I think I have to take the id of the artist but I don't know how, someone can help ?
Thanks
<div class="col-md-8">
<table class="table table-bordered table-striped table-condensed css-serial">
<div class="btn pull-right col-md-4">
<a class="btn btn-primary" href="{{ route('create-artist') }}" role="button">Create a new Artist or Band</a>
</div>
<thead>
#if(!$artists->isEmpty())
<h1>ARTISTS & BANDS:</h1>
<tr>
<th>#</th>
<th>Artist</th>
<th>Music genre</th>
<th>Picture</th>
<th>Edition</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($artists as $artist)
<form>
<tr>
<td></td>
<td>{{ $artist->artist_name}}</td>
<td>{{ $musicgenres[$artist->music_genre]}} </td>
<td><img src="../images/uploads/pictures_band/{{$artist->picture}}" style="width:80px; height:80px; border-radius:50%;" ></td>
<td>Show</td>
<td><a class="btn btn-danger" id="delete_id">Remove</a></td>
</tr>
</form>
<div id="delete-confirm" class="modal" data-backdrop="static" data-keyboard="true">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title text-center">Warning</h4>
</div>
<div class="modal-body">
<p class="text-center">Are you sure you want to delete this Artist ? It will delete Tour and concert belong to this artist !</p>
<form action="{{ route('delete-artists', ['id' => $artist->id] )}}">
<div class="text-center" >
<button type="submit" class=" btn btn-primary " > Okay</button>
<button type="button" class=" btn btn-primary " data-dismiss="modal" > Cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endforeach
#else
<h3>'There is no artist or band yet !'</h3>
#endif
</tbody>
</table>
</div>
<script>
$('#delete_id').on('click',function(){
//var id = $(this).attr('id')
$('#delete-confirm').modal('show');
});
</script>
your remove button has an id, but the problem is that (most likely) you will have many buttons. id should have to one specific element. In my previous experience it broke some things (about modals), so i suggest you to change it into a class. after that in you have to modify your js to show the correct modal which is connected to your artist's button.

Twig table get id outside for loop

I'm trying to access to the user id outside my for loop in twig
Table
{% set userid = '' %}
<table class="display dataTable" id="manageusertable" cellspacing="0" width="100%">
<thead>
<tr>
<th>Image</th>
<th>Login</th>
<th>Nom / Prenom</th>
<th>Email</th>
<th>Activé</th>
<th>Actions</th>
</tr>
</thead>
<tfoot>
</tfoot>
<tbody>
{% for user in users %}
{% set userid = user.id %}
<tr class="getid">
<td>{{ user.UserImage }}</td>
<td>{{ user.username }}</td>
<td>{{ user.UserLastname }} / {{ user.UserFirstName }}
</td>
<td>{{ user.email }}</td>
{% if user.enabled == 1 %}
<td><i class="fa fa-check" aria-hidden="true"></i></td>
{% else %}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
{% endif %}
<td>
<i class="fa fa-search" aria-hidden="true"></i>
<button type="button" class="btn btn-add btn-sm" data-
toggle="modal" data-target="#update"><i class="fa fa-
pencil"></i></button>
<button type="button" class="deleteuser btn btn-danger
btn-sm" data-toggle="modal" data-target="#deleteuser"><i
class="fa fa-trash-o"></i> </button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Modal div when button data-model=#delete is clicked (outside loop)
<div class="modal fade" id="deleteuser" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header modal-header-primary">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3><i class="fa fa-user m-r-5"></i> Delete User</h3>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<form class="form-horizontal">
<fieldset>
<div class="col-md-12 form-group user-form-group">
<label class="control-label">Delete User</label>
<div class="pull-right">
<button type="button" class="btn btn-danger btn-sm">NO</button>
<button " class="btn btn-add btn-sm"><a style="color:#fff;" href="{{ path('lemon_user_deleteuser', {'id': userid}) }}">YES</a></button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
The line
<button " class="btn btn-add btn-sm"><a style="color:#fff;" href="{{ path('lemon_user_deleteuser', {'id': userid}) }}">YES</a></button>
always returns the last id (15 if i have 15users in my DB) no matter on which line the button is clicked
i know how to do this with jquery/ajax and from scratch php but since im new to symfony/twig im kinda lost
In your case you have to make an array with the whole of ids e.g.
{{ set userIds = {} }}
{% for user in users %}
...
{% set userIds = userIds|merge({id : user.id} %}
...
{% endfor %}
And then redo the "for loop" for each delete button and paths setup

Can't change title of modal using Laravel

I've created a resource database using Laravel, and have the option to (flag, edit, and/or delete) a resource. When I hit the 'flag' button a modal pops up for you to add input as to why you're flagging the resource. I'd like for the title of the modal to show which resource was clicked, and I have it working, but in this instance I cannot change the title of the modal regardless of which resource I flag. Here's some of my code, hopefully it'll be more clear.
Resource View
#foreach($resources as $resource) #foreach ($resource->locations as $location)
<tr>
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
<td>{{ $resource->Description }}</td>
<td>{{ $location->Address }}</td>
<td>{{ $location->City }}</td>
<td>{{ $location->Zip_Code }}</td>
<td>{{ $location->County }}</td>
<td>
<button type="button" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;">Edit
</button>
<button type="button" class=" msgBtn2 btn btn-default" id="flagged" data-toggle="modal" data-target="#flagResource" style="display:inline; margin-right:auto;">Flag
</button>
<button type="button" class=" msgBtn3 btn btn-default" style="display:inline; margin-right:auto;">Delete
</button>
</td>
</tr>
#endforeach
#endforeach
</tbody>
So, when the middle button is clicked, it brings up the modal which looks like this
Modal View
<div class="modal fade" id="flagResource"
tabindex="-1" role="dialog"
aria-labelledby="flagResourceLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"
id="flagged" style="text-align:center;"> {{ $resource->Name }}
</h4>
</div>
<div class="modal-body">
{!! Form::open(['method' => 'POST', url('FlagsController#addFlag'), $resource->id ]) !!}
<div class="form-group">
{!! Form::label('reason', 'Reason for Flag: ') !!}
{!! Form::textarea('Reason', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('comments', 'Other Comments: ') !!}
{!! Form::text('Comments', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
data-dismiss="modal">Close</button>
<span class="pull-right">
<button type="button" class="btn btn-primary" style="margin-left:5px;">
Submit
</button>
</span>
</div>
</div>
</div>
</div>
So whenever I click flag on ANY resource, It shows this:
modalExample
I can't figure out why it keeps showing up that as my title. Any help would be great, thanks!
The modal uses this data:
$resource->Name
$resource->id
You probably have the modal somewhere after your foreach in your code, so $resource is filled with data from the last row in this loop:
#foreach($resources as $resource) #foreach ($resource->locations as $location)
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
#endforeach
What you should do is send more data to the modal so it can show the correct information. So in your loop you should attach more data for every row.
I am assuming you are using bootstrap for the modal. In version 3.2.0 you can do something like this to add this data:
Flag
And use javascript to get the variables and set them in your modal instead of using $resource->Name.
$('#my_modal').on('show.bs.modal', function(e) {
var resourceId = $(e.relatedTarget).data('resource-id');
var resourceName = $(e.relatedTarget).data('resource-name');
$(e.currentTarget).find('#resourceId').val(resourceId);
$(e.currentTarget).find('#resourceName').val(resourceName);
});

Categories