I have a table in a Laravel application and on the basis of a cell content I want to change the colour of the row.
Part of my datatables javascript is
ajax:"{{ route('propertiesData') }}",
columns: [
{ data: 'address', name: 'address' },
{data: 'town', name: 'town'},
{data: 'postcode', name: 'postcode'},
{data: 'units',name: 'units'},
{ data: 'examination', name: 'examination' },
{data: 'priority', name:'priority', searchable: false},
{data: 'completed', name:'completed'},
{data: 'action', name: 'action', orderable: false, searchable: false},
],
"createdRow": function( row, data, index ) {
if ( data[6] == "1" )
{
$(row).addClass( 'redRow' );
}
},
The priority field is either 1 or 0.
The table works but the background colour is always white.
data is most likely an object so you have to access the values differently, try this:
"createdRow": function (row, data, index) {
if (data.priority === "1") {
$(row).addClass('redRow');
}
}
Related
I use laravel/datatables to list data.
I manipulated my datalist with addColumn function.
However I am not able to use search in view for added columns.
Because search is working according to database rows.
Datatables works fine but in the view search area is not working. Because there isnt any fullname row in database table.
Here you can find my codes (I want to search for fullname but I can't)
Order Model
public function getFullname()
{
return json_decode($this->getAttribute('delivery_adress'))->fullname;
}
OrderController.php
if ($request->ajax()) {
return datatables()->of(Order::query())
->addColumn('fullname', function (Order $order) {
return $order->getFullname();
})->addColumn('city', function (Order $order) {
return $order->getCity();
})->addColumn('product_id', function (Order $order) {
return $order->product->title;
})->make(true);
}
Orders Blade / Datatables code
"pageLength": 10,
processing: true,
serverSide: true,
ajax: 'orders',
columns: [
{ data: 'id', name: 'id'},
{ data: 'fullname', name: 'fullname', defaultContent: '-' , orderable: false },
{ data: 'city', name: 'city', defaultContent: '-', orderable: false },
{ data: 'product_id', name: 'product_id', className: 'd-none d-sm-table-cell', defaultContent: '-' },
{ data: 'totalPrice', name: 'totalPrice', className: 'd-none d-sm-table-cell', defaultContent: '-' },
],
edit:
Finally, I found solution
{data: 'added_column', name: 'actual_column_name'}
https://github.com/yajra/laravel-datatables/issues/139#issuecomment-275326787
Finally, I found solution
{data: 'added_column', name: 'actual_column_name'}
https://github.com/yajra/laravel-datatables/issues/139#issuecomment-275326787
Make sure to have relationship into Order model like this:
public function user()
{
return $this->belongsTo('App\User');
}
Try to update like this :
...
->addColumn('fullname', function (Order $order) {
return $order->user->fullname;
})
...
change status
serverSide : false
"pageLength": 10,
processing: true,
serverSide: false,
ajax: 'orders',
columns: [
{ data: 'id', name: 'id'},
{ data: 'fullname', name: 'fullname', defaultContent: '-' , orderable: false },
{ data: 'city', name: 'city', defaultContent: '-', orderable: false },
{ data: 'product_id', name: 'product_id', className: 'd-none d-sm-table-cell', defaultContent: '-' },
{ data: 'totalPrice', name: 'totalPrice', className: 'd-none d-sm-table-cell', defaultContent: '-' },
],
A question regarding AJAX and Laravel. I'm using jquery datatables plugin. Right now everything is working fine and my data is returned as expected please. This is my columns section
columns: [
{data: 'id', name: 'id'},
{data: 'slug', name: 'slug'},
{data: 'title',
render: function (data, type, row, meta) {
return ''+data+''
}, name: 'title' },
{ data: 'body' , name: 'body' },
{ data: 'author' , name: 'author'},
{ data: 'created_at', name: 'created_at' },
]
My question is how do I add the slug to the url of the title?
I assume you're using datatables jquery plugin and you can use row parameter for access the other columns..
render: function (data, type, row, meta) {
return ''+data+''
}, name: 'title' },
I am getting data of invoice table but i want to sort data in desc
This is my code:
var oTable = $('#ks-datatable').DataTable({
'processing': false,
'serverSide': true,
"searching": false,
/*"bLengthChange": false,*/
'ajax': '{{route('invoice.index')}}',
columns: [
{data: 'id', name: 'id'},
{data: 'client_name', name: 'client_name'},
{data: 'invoice_number', name: 'invoice_number'},
{data: 'date', name: 'date'},
{data: 'created_at', name: 'created_at'},
{data: 'amount', name: 'amount'},
{data: 'action', name: 'action', searchable: false},
]
});
use order in datatable :
"ordering":'true'
"order": [columnNumber, 'orderType'],
Example
....
"ordering":'true'
"order": [0, 'desc'],
Your code will be:
var oTable = $('#ks-datatable').DataTable({
'processing': false,
'serverSide': true,
"searching": false,
/*"bLengthChange": false,*/
'ajax': '{{route('invoice.index')}}',
"ordering":'true',
"order": [0, 'desc'],
columns: [
{data: 'id', name: 'id'},
{data: 'client_name', name: 'client_name'},
{data: 'invoice_number', name: 'invoice_number'},
{data: 'date', name: 'date'},
{data: 'created_at', name: 'created_at'},
{data: 'amount', name: 'amount'},
{data: 'action', name: 'action', searchable: false},
]
});
I have three tables i am able to use relation between 2 tables easily. But when i am trying to access values from third table i mean trying to use hasManyThrough relation it is showing me always error. Do you have any idea how i can get value of third table column using middle table relation with first and last table.
System details
Linux
PHP 7
Laravel 5.3
Laravel-Datatables Latest Version
$('#mms-table').DataTable({
processing : true,
serverSide: true,
lengthMenu: [10,25,50,100],
order: [[1,'desc']],
ajax: '{!! url(LaravelLocalization::getCurrentLocale()."/admin/contact-management/contact-data") !!}',
columns: [
{ data: 'merchant.bank.name', name: 'merchant.bank_id', orderable: true },
{ data: 'merchant.merchant_id', name: 'merchant.merchant_id', orderable: true },
{ data: 'merchant.merchant_name', name: 'merchant.merchant_name', orderable: true },
{ data: 'contact.name', name: 'contact.name', orderable: true },
{ data: 'contact.email', name: 'contact.email', orderable: true },
{ data: 'city', name: 'city', orderable: true },
{ data: 'agent.name', name: 'agent.name', orderable: true },
{ data: 'action', name: 'action', orderable: false, searchable: false},
],
dom: 'Blfrptip',
buttons: [
{
extend: 'colvis',text: 'Show/Hide Columns'
}
],
oLanguage: {
sProcessing: "<img height='80' width='80' src='{{ url('public/images/loading.gif') }}' alt='loader'/>"
},
initComplete: function () {
this.api().columns().every(function () {
var column = this;
var input = document.createElement("input");
$(input).appendTo($(column.footer()).empty())
.on('change', function () {
column.search($(this).val(), false, false, true).draw();
});
});
}
});
I have the following issue in Laravel DataTables (yajra/laravel-datatables
):
I can't search (filter) in a column generated with DB::Raw.
Also I can't search with Global search function.
In my Laravel Controller:
$inputs = Input_::join('companies', 'companies.id', '=', 'inputs.company_id')
->leftjoin('inputs_details', 'inputs_details.input_id', '=', 'inputs.id')
->select(array('inputs.id as id',
'inputs.created_at as created_at',
'inputs.updated_at as updated_at',
'inputs.input_at as input_at',
'companies.name',
'inputs.documents as documents',
DB::raw("SUM(inputs_details.quantity*inputs_details.price_input*(1+inputs_details.VAT/100)) as input_total")
))
->groupBy('inputs.id')
->orderBy('inputs.input_at', 'DESC');
return Datatables::of($inputs)
->remove_column('id')
->make(true);
In my View:
$('#inputs-table').DataTable({
processing : true,
serverSide : true,
ajax: '{{ URL::to('employee/inputs/data/') }}',
columns: [
{data: 'created_at', name: 'inputs.created_at', orderable: false},
{data: 'updated_at', name: 'inputs.updated_at', orderable: false},
{data: 'input_at', name: 'inputs.input_at', orderable: false},
{data: 'name', name: 'companies.name', orderable: false},
{data: 'documents', name: 'inputs.documents', orderable: false},
{data: 'input_total', name: 'input_total', orderable: false, searchable: true},
{data: 'reports', name: 'reports', orderable: false, searchable: false},
{data: 'actions', name: 'actions', orderable: false, searchable: false}
],
initComplete: function () {
this.api().columns().every(function () {
var column = this;
var input = document.createElement("input");
$(input).appendTo($(column.footer()).empty())
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex($(this).val());
column.search(val ? val : '', true, false).draw();
});
});
} });
You have to put the filterColumn action in your datatable.
$inputs = Input_::join('companies', 'companies.id', '=', 'inputs.company_id')
->leftjoin('inputs_details', 'inputs_details.input_id', '=', 'inputs.id')
->select(array('inputs.id as id',
'inputs.created_at as created_at',
'inputs.updated_at as updated_at',
'inputs.input_at as input_at',
'companies.name',
'inputs.documents as documents',
DB::raw("SUM(inputs_details.quantity*inputs_details.price_input*(1+inputs_details.VAT/100)) as input_total")
))
->groupBy('inputs.id')
->orderBy('inputs.input_at', 'DESC');
return Datatables::of($inputs)
->remove_column('id')
->filterColumn('input_total', function($query, $keyword) {
$query->whereRaw("SUM(inputs_details.quantity*inputs_details.price_input*(1+inputs_details.VAT/100)) = ?", $keyword);
})
->make(true);