put functional buttons for editing in serverside datatable - Laravel - laravel

The project is in Laravel. I have to pull a lot of records, so I opted for serverside... but, I can't put functional buttons to edit the records
index.blade
#extends('layouts.app')
#section('content')
<section class="section">
<div class="section-header">
<h3 class="page__heading">Siniestros</h3>
</div>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">Dashboard</li>
<li class="breadcrumb-item">Siniestros</li>
<!-- <li class="breadcrumb-item active" aria-current="page">Index</li> -->
</ol>
</nav>
<div class="section-body">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
#can('crear-siniestro')
<a class="btn btn-primary btn-sm" href="{{ route('siniestros.create') }}">Nuevo</a>
#endcan
<table class="table table-sm m-1 p-1 table-bordered table-hover table-striped tablita" style="width:100%">
<thead style="background-color:hsl(213, 99%, 49%)">
<th style="color:#fff;">Siniestro</th>
<th style="color:#fff;">Fecha IP</th>
<th style="color:#fff;">Nro Corto</th>
<th style="color:#fff;">Dirección</th>
<th style="color:#fff;">Localidad</th>
<th style="color:#fff;">Patente</th>
<th style="color:#fff;">Fecha IP</th>
<th style="color:#fff;">Estado</th>
<th style="color:#fff;">Coordinador</th>
<th style="color:#fff;">Fecha ingreso</th>
<th style="color:#fff;">Fecha gestión</th>
<th style="color:#fff;">Modalidad</th>
<th style="color:#fff;">Cliente</th>
<th style="color:#fff;">Dirección</th>
<th style="color:#fff;">Localidad</th>
<th style="color:#fff;">Inspector</th>
<th style="color:#fff;">Motivo</th>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
#endsection
<!-- DataTables JS -->
<script src="{{ asset('assets/js/jquery.min.js') }}"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.11.5/b-2.2.2/b-colvis-2.2.2/b-html5-2.2.2/b-print-2.2.2/r-2.2.9/datatables.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js" integrity="sha512-6PM0qYu5KExuNcKt5bURAoT6KCThUmHRewN3zUFNaoI6Di7XJPTMoT6K0nsagZKk2OB4L7E3q1uQKHNHd4stIQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
#section('javas')
<script>
$(document).ready(function() {
$('.tablita').DataTable({
ajax: 'datatable/users',
processing: true,
serverSide: true,
pageLength: 100,
columns: [
{"data": "siniestro"},
{"data": "fechaip"},
{"data": "nrocorto"},
{"data": "direccion"},
{"data": "localidad"},
{"data": "patente"},
{"data": "fechaip"},
{"data": "estado"},
{"data": "coordinador"},
{"data": "created_at"},
{"data": "updated_at"},
{"data": "modalidad"},
{"data": "cliente"},
{"data": "direccion"},
{"data": "localidad"},
{"data": "inspector"},
{"data": "motivo"},
]
});
});
function editData(id){
$.ajax({
type:"get",
dataType:"json",
url:"/teacher/edit/"+id,
success: function(data){
$('#id').val(data.id);
console.log(data);
}
})
}
</script>
#endsection
SiniestroController
public function registros(){
$registros = Siniestro::all();
return Datatables::of($registros)->toJson();
return $registros;
}
web.php
Route::get('datatable/users', [SiniestroController::class, 'registros']);
In other views I have the button that I will leave next, to edit each record, which takes me to a blade view. I would like to be able to agree with this one if possible:
<a class="btn btn-outline-success btn-lg" href="{{ route('siniestros.edit',$siniestro->id) }}"><i class="fa-solid fa-pen-to-square fa-xl"></i></a>

Related

how to reload table when certain row is created or delete?

I am just learning Vuejs with laravel. and my Delete is Working correctly but when it redirect to index page after row is deleted that row is not removed until it is loaded..
And for When Category is added new data is not shown until browser is loaded...
This is my Categories Component `
<form role="form" method="POST" enctype="multipart/form-data" #submit.prevent="createCategory" v-if="adding">
<div class="card-header">
<h3 class="card-title">Create New Category</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Category Name</label>
<input type="text" class="form-control" id="category_name" v-model="category_name" placeholder="Enter Category Name">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<button #click.prevent="cancel" class="btn btn-danger">Cancel</button>
</div>
</form>
<div class="card-body" v-else>
<div class="card-header" >
<h3 class="card-title"><a class="btn btn-block btn-outline-secondary btn-lg" #click.prevent="add()"> Create New Category</a></h3>
</div>
<table id="example2" class="table table-bordered table-hover" >
<head>
<tr>
<th>ID</th>
<th>Category Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(category,index) in model" :category="category" v-bind:key="category.id">
<td>{{category.id}}</td>
<td>{{category.category_name}}</td>
<td>Edit | <a #click="deleteCategory(category.id,index)" class="btn-outline-
danger">Delete</a></td>
</tr>
</body>
</table>
</div>
</div>
`
<script>
export default {
props:['model'],
data(){
return {
model:[],
section_id: 0,
sections: [],
parent_id: 0,
categories: [],
adding:false,
category_name:'',
}
},
methods:{
add(){
this.adding=true;
},
cancel(){
this.adding=false;
},
createCategory(){
axios.post(`categories `,{
category_name:this.category_name,
}).catch(error => {
console.log('Error')
})
.then(res=>{
this.adding=false;
})
},
deleteCategory(id,index){
if(confirm('Are You Sure ?')){
axios.delete('categories/'+id)
.then(resp => {
this.model.splice(index, 1);
})
.catch(error => {
console.log(error);
})
}
}
},
}
</script>
<table id="example2" v-if="flag" class="table table-bordered table-hover" >
<head>
<tr>
<th>ID</th>
<th>Category Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(category,index) in model" :category="category" v-bind:key="category.id">
<td>{{category.id}}</td>
<td>{{category.category_name}}</td>
<td>Edit | <a #click="deleteCategory(category.id,index)" class="btn-outline-
danger">Delete</a></td>
</tr>
</body>
</table>
in data add flag:false,
data(){
return {
model:[],
section_id: 0,
sections: [],
parent_id: 0,
categories: [],
adding:false,
category_name:'',
flag:false
}
},
in your functions make your flag false in case of success response
deleteCategory(id,index){
this.flag=false;
if(confirm('Are You Sure ?')){
axios.delete('categories/'+id)
.then(resp => {
this.model.splice(index, 1);
this.flag=true;
})
.catch(error => {
console.log(error);
this.flag=false;
})
}
}
I think that you are overcomplicate this. Just create an API that retrieve all the categories. So after each addition or deletion, then just call that API.

Datatable not showing properly in the laravel blade file

I'm getting one issue in the datable. I added datatable in my laravel blade file, but when I clicked on the menu (call blade file). Then it shows a normal table, but when I refresh the same page again and then it shows datatable properly. My question is when I click on the menu(call blade file), then why it is not showing datatable properly. It shows a normal table only. How to fix this.
please check imagesthis image is before refresh
this image is after refresh
and this is my blade file
#extends('backEnd.layout')
<!--page level css starts-->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<!--end of page level css-->
#section('content')
<div class="padding">
<div class="box">
<div class="box-header dker">
<h3>Product List</h3>
<small>
{{ trans('backLang.home') }} /
Product List
</small>
</div>
<table class="table table-bordered" id="table" >
<thead class=" primary">
<tr >
<th class="text-center">Category</th>
<th class="text-center">Title</th>
<th class="text-center">Make</th>
<th class="text-center">Rate</th>
<th class="text-center">Status</th>
</tr>
</thead>
<tbody>
#foreach($records as $record)
<tr>
<td class="text-center">{{$record->section_title}}</td>
<td class="text-center">{{$record->title_en}}</td>
<td class="text-center">{{$record->make}}</td>
<td class="text-center">{{$record->rate}}</td>
<td class="text-center">#if($record->status != 1) Used
#else New #endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
#section('footerInclude')
<script type="text/javascript">
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
$("#action").change(function () {
if (this.value == "delete") {
$("#submit_all").css("display", "none");
$("#submit_show_msg").css("display", "inline-block");
} else {
$("#submit_all").css("display", "inline-block");
$("#submit_show_msg").css("display", "none");
}
});
</script>
<script>
$(document).ready(function() {
$('#table').DataTable();
} );
</script>
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
#endsection
any help will be appreciated.
Thanks in advance.
Your Blade file should be like this.
You should add script files before you write your script.
#extends('backEnd.layout')
<!--page level css starts-->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<!--end of page level css-->
#section('content')
<div class="padding">
<div class="box">
<div class="box-header dker">
<h3>Product List</h3>
<small>
{{ trans('backLang.home') }} /
Product List
</small>
</div>
<table class="table table-bordered" id="table" >
<thead class=" primary">
<tr >
<th class="text-center">Category</th>
<th class="text-center">Title</th>
<th class="text-center">Make</th>
<th class="text-center">Rate</th>
<th class="text-center">Status</th>
</tr>
</thead>
<tbody>
#foreach($records as $record)
<tr>
<td class="text-center">{{$record->section_title}}</td>
<td class="text-center">{{$record->title_en}}</td>
<td class="text-center">{{$record->make}}</td>
<td class="text-center">{{$record->rate}}</td>
<td class="text-center">#if($record->status != 1) Used
#else New #endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#table').DataTable();
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
$("#action").change(function () {
if (this.value == "delete") {
$("#submit_all").css("display", "none");
$("#submit_show_msg").css("display", "inline-block");
} else {
$("#submit_all").css("display", "inline-block");
$("#submit_show_msg").css("display", "none");
}
});
} ;
</script>
#section('footerInclude')
#endsection

Data not rendering in the table using datatables

I am implementing datatable. I can see data is coming from database in xhr using developer tools but it is not showing in the table. Please tell me what is the problem with my code.
#section Body{
<div class="row">
<div class="col-md-9">
<div class="block">
<div class="content">
<table cellpadding="0" cellspacing="0" width="100%" class="table" id="myDbTable">
<thead>
<tr>
<th data-hide>ID</th>
<th data-hide >Name</th>
<th data-hide >E-mail</th>
<th data-hide>Phone</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
}
#section Scripts{
<script>
var DataColumnsCount = 4;
//***Start Fatching Data for datatable *** start //
function fetchEmployeeTableList() {
$('#myDbTable').DataTable({
"columns": [
{"data" : "Id"},
{ "data": "Name" },
{ "data": "Email" },
{ "data": "Phone" }
]
}
);
}
$.ajax({
type: 'GET',
url: "#Url.Action("GetEmployees","Employee")",
dataType: 'json',
data: "{}",
dataSrc: 'DataSet',
success: fetchEmployeeTableList});
</script>
}
Please help me in this regard.

update my database and discard an element from table

i want to delete an item from database and remove it from table
Entity product
i want to select all the products and after a click on button x i want it to update name and remove it from table
Any help ??
my twig :
<script>
$("document").ready(function () {
$(".btn-group-vertical").click(function () {
$.ajax({
type:'get',
url:'http://localhost/symfony/nouveau%20dossier/web/app_dev.php/'+$(this).val(),
beforeSend: function () {
},
success: function(data){
var x = $("produit").val(data.approuver);
console.log(x);
$("tt").find('td').fadeOut(1000,function(){
$("tt").remove();
});
}
});
});
});
</script>
{% endblock script %}
{% block body %}
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Produit en attente</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>Produit de</th>
<th>Libelle Produit</th>
<th>Quantite stock</th>
<th>etat</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr class="tt">
<td align="center">{{ product.seller }}</td>
<td align="center">{{ product.libelle }}</td>
<td align="center">{{ product.quantiteStock }}</td>
<td><button
type="button"
class="btn btn-group-vertical btn-danger btn-xs"
id=1>Decliner</button>
<button type="button"
class="btn btn-group-vertical btn-sucess btn-xs"
id=2>Approuver</button>
</td>
</tr>
{% endfor %}
<tfoot>
<tr>
<th>Produit de</th>
<th>Libelle Produit</th>
<th>Quantite stock</th>
<th>etat</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
<!-- ./wrapper -->
my controller :
public function approuverAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$produit = $em->getRepository('ProductBundle:Product')
->findOneBy(array('id'=>$id));
$produit->setApprouver('Approuver');
$respones = new JsonResponse();
return $em->merge($produit);
$em->flush();
}
Any help please
I want to delete an item from database and remove it from table Entity
product
To remove a product:
$em->remove($produit);
$em->flush();

knockoutjs $parent undefined

I have a problem with KnockoutJS, where the $parent binding context is undefined.
Code
<div class="row">
<div class="col-md-4">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">Products Data</h2>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered table-condensed" data-bind="with: ProductsViewModel">
<thead>
<tr>
<th>ProductID</th>
<th>Product Name</th>
<th>Units In Stock</th>
<th>Action</th>
</tr>
</thead>
<tbody data-bind="foreach: products">
<tr>
<td data-bind="text: $data.ProductID"></td>
<td data-bind="text: $data.ProductName"></td>
<td data-bind="text: $data.UnitsInStock"></td>
<td><button class="btn btn-danger btn-xs" data-bind="click: $parent.RemoveProduct">[x] delete</button></td>
</tr>
</tbody>
</table>
<button class="btn btn-large" data-bind="click: GetProducts()">Load Data</button>
</div>
</div>
</div>
</div>
and:
/// <reference path="../Scripts/knockout-3.4.0.js" />
/// <reference path="../Scripts/jquery-1.10.2.js" />
function ProductsViewModel() {
var self = this;
self.products = ko.observableArray([]);
self.RemoveProduct = function () {
bootbox.alert({ message: 'Pressed !!', size: 'small' });
}
self.GetProducts = function () {
self.products.removeAll();
$.getJSON('/api/products', function (data) {
$.each(data, function (key, value) {
self.products(data);
});
});
}
}
$(document).ready(function () {
ko.applyBindings(ProductsViewModel);
});
All the binding work correctly in the table, except the click event binding on the button, where the $parent is undefined.
If I remove the button control all the data binds correctly in the table element.
Any ideas how to fix this ?? It's all standard Knockout code to my knowledge.
Thanks in advance.
bax2097
I've fixed this by removing the $parent altogether. All working now.
Problems
In your Html you bind your table into a with: ProductsViewModel. Which is not needed.
In your Load data button you did not pass the actual function, instead you execute it right away, so just remove ().
See this JsFiddle for demo

Resources