Only the first row of the table is changing
HTML table
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $entrepreneur->name }}</td>
<td>{{ $entrepreneur->contact }}</td>
<td>{{ $entrepreneur->address }}</td>
<td>{{ $entrepreneur->business_name }}</td>
<td>{{ $entrepreneur->business_address }}</td>
<td>
<div id="approval">
<button class="check-btn" id="check-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#checkModal-{{ $entrepreneur->id }}"><i class="fa fa-check"></i></button>
<button class="delete-btn" id="delete-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#rejectModal-{{ $entrepreneur->id }}"><i class="fa fa-close"></i></button>
</div>
</td>
</tr>
Ajax Function
$.ajax({
type: 'post',
url: "/changeAccepted/"+entrepreneur_id,
success: function(res) {
toastr["success"]('Entrepreneur accepted succesfully');
$('#checkModal-'+entrepreneur_id).modal('hide');
$('#approval').html('Accepted');
} });
I am trying to change the two buttons on 'id=approval', right now it is changing for only the first row. I also want to change for other rows when it is accepted.
Each html element on same page has to have unique id property.
As you are using table I think you have multiple rows and each row has its own buttons placed in <div id="approval"></div> element.
When you use $('#approval') selector, Jquery gets only first one.
If you want to select all and change every element you have to use any selector but id.
The simplest way is to use .class selector.
Example & Solution:
Html:
...
<div class="approval_class">
<button class="check-btn" id="check-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#checkModal-{{ $entrepreneur->id }}"><i class="fa fa-check"></i></button>
<button class="delete-btn" id="delete-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#rejectModal-{{ $entrepreneur->id }}"><i class="fa fa-close"></i></button>
</div>
...
Javascript (Jquery): (to change all rows as accepted)
$('.approval_class').html('Accepted');
Javascript (Jquery): (to change only that row which accepted)
$('#delete-btn-' + entrepreneur_id).closest('div').html('Accepted');
Related
At the most basic understanding, I've been trying to match the route and the form action. I think that I am doing it right but I wonder why the error keeps on showing. I may have missed something anywhere but I just really couldn't find it. Please help. With a very tight schedule, I need to complete this project.
When I submit the form it goes to this address.
index.blade.php
<tbody>
#foreach($payments as $payment)
<tr>
<td>{{ $payment->order->first_last_name }}</td>
<td>{{ $payment->order->business_name }}</td>
<td>{{ $payment->order->business_type }}</td>
<td>{{ $payment->order->phone_number }}</td>
<td>{{ $payment->order->postal_code }}</td>
<td>{{ $payment->order->email }}</td>
<td>{{ $payment->order->address }}</td>
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.send', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
</td>
<td>
#php $status = $payment->status #endphp
<span class="{{ $status['bs_class'] }}">
{{ $status['label'] }}
</span>
</td>
<td>{{ $payment->accounting_code }}</td>
</tr>
#include('Admin.layouts.modal')
#endforeach
</tbody>
js
<script>
$('#send').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var orderId = button.data('id');
var modal = $(this);
modal.find('form').attr('action', action);
document.getElementById("orderId").value = orderId;
});
</script>
modal.blade.php
<div class="modal fade" id="send" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="send" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ route('payments.accounting', $payment->id) }}" method="post">
#csrf
#method('PUT')
<input type="hidden" id="orderId" value="">
<div class="modal-header">
<h5 class="modal-title" id="sendTitle">send</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="accounting_code" class="form-label">کد accounting_code</label>
<input type="text" class="form-control" id="accounting_code" name="accounting_code">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">save</button>
</div>
</form>
</div>
</div>
</div>
web.php
Route::put('/payments/accounting/{payment}', [App\Http\Controllers\Admin\PaymentController::class,'accounting'])->name('payments.accounting');
PaymentController.php
public function accounting(Payment $payment, Request $request)
{
dd('hello');
$data = [
'accounting_code' => $request->post('accounting_code')
];
$payment->update($data);
return back();
}
Because your js code modal.find('form').attr('action', action) replaced the form action when showing the modal. Try to change the data-action from route payments.send to payments.accounting for the send button:
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.accounting', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
Did you try it without naming the route?
delete this:
->name('payments.accounting')
and then in HTML form:
action="/payments/accounting"
I think you are mixing POST, PUT and GET Request here. Make sure you send your form with the correct method.
I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.
In my Controller I added this $competitions = Competition::latest()->paginate(1);
Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();
And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.
EDIT:
Controller function:
public function index()
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Loop in blade:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}
dd(Competition::all()) returns the 4 records I have
Can you please tell me what I did wrong? Any and all help gratefully received.
$competitions = Competition::latest()->paginate(1) this command will return you one result and then pagination will take 1 per page so it won't be shown at all.
Try something like this in your controller
$competitions = Competition::paginate(1)
return view(...., ['competitions' => $competitions]);
and in your blade add this under the foreach loop where you display data
{{ $competitions->links() }}
Read more here
HERE IS HOW TO MAKE PAGINATION
BLADE
#foreach($paginate as $p)
{{ $p->name }} <br>
#endforeach
{{ $paginate->links() }}
CONTROLLER
return view('backend.test', [
'paginate' => Product::paginate(5)
]);
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');
enter #foreach ($colors as $color)
<tr>
<th scope="row">{{ $color->code }}</th>
<td>{{ $color->name }}</td>
<td>
<div class="col-md-6 cor"></div>
<style>
.cor {
background-color: {{ $color->code }};
}
</style>
</td>
<td><span class="badge badge-success">Active</span></td>
<td>
<a href="{{ URL::route('edit.customer', array('customer'=>$color->id_color)) }}" class="text-success mr-2">
<button class="nav-icon i-Pen-2 font-weight-bold btn btn-warning m-0" type="button" data-toggle="tooltip" data-placement="top" title="Editar Cliente"></button>
</a>
<a href="{{ URL::route('delete.customer', array('customer'=>$color->id_color)) }}" class="text-danger mr-2">
<button class="nav-icon i-Close-Window font-weight-bold btn btn-danger m-0" type="button" data-toggle="tooltip" data-placement="top" title="Eliminar Cliente"></button>
</a>
</td>
</tr>
#endforeach here
I want to load color codes from the database, I want the color that is recorded to appear to the user, this foreach loads me the last color in all
You're generating the CSS rule
.cor {
background-color: {{ $color->code }};
}
multiple times, but with a different colour each time. So whatever the last definition is, it just overrides all the previous versions. Think about it - if you declare something with the same name multiple times, how do you expect the browser to tell them apart or know which one to apply to which element? It can't do that. It can only use one version, so it just uses the last one you created.
Since the colour will be unique to each copy of the div, you'd be better off just writing
<div class="col-md-6" style="background-color: {{ $color->code }}"></div>
instead.
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.