Button in Laravel Datatable not rendering - php

I'm using Laravel 5.4 and Yajra datatable, below is my code working properly but in the 2nd action I've created, The button is not displaying but instead it display the text itself "Add Price" What am I missing ?
public function getProductDatatable()
{
$Product = Product::query();
return Datatables::eloquent($Product)
->addColumn('action', function($row) {
return 'Edit';
})
->addColumn('add_price', function($row) {
return 'Add Price';
})
->make(true);
}
Frontend Part
<script type="text/javascript">
$(function() {
$('#product-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url('product/get_product_datatable') }}',
columns : [
{data: 'id', name: 'id'},
{data: 'product_code', name: 'product_code'},
{data: 'action', searchable: false, orderable: false},
{data: 'add_price', searchable: false, orderable: false},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'}
]
});
});
</script>

You need to define rawColumns :
public function getProductDatatable()
{
$Product = Product::query();
return Datatables::eloquent($Product)
->addColumn('action', function($row) {
return 'Edit';
})
->addColumn('add_price', function($row) {
return 'Add Price';
})
->rawColumns(['add_price', 'action'])
->make(true);
}

I found this issue on github, try adding rawColumns
Datatables::eloquent($Product)
->addColumn(..)
->rawColumns(['add_price']);

Related

Sorting on chaining query using laravel datatables server side function

I'm trying to sorting the column named agency_name. But its not working. The full_name is perfectly working while the agency_name column is not working. So basically I trying to access the data which in the user_work_experiences table, so the way I access them is through user table and get current user work experiences. Im using yajra/datatables package. So here my table structure looks: -
Student Hostels
user_id
Users:
id
User Work Experiences
user_id
agency_name
Here the example code:
Controller:
public function getHostels()
{
$hostels = StudentHostel::with('user', 'user.userProfile', 'user.currentWorkExperience', 'status')->limit(100)->select('student_hostels.*');
return Datatables::of($hostels)
->addIndexColumn()
->editColumn('user.full_name', function (StudentHostel $hostel) {
return $hostel->user->full_name;
})
->editColumn('user.userProfile.address_1', function(StudentHostel $hostel) {
return $hostel->user->userProfile ? $hostel->user->userProfile->address_1 . " " . $hostel->user->userProfile->address_2 : null;
})
->addColumn('action', 'backEnd.hostel.datatables_action')
->orderColumn('user.full_name', function ($query, $order) {
$query->whereHas('user', function ($q) use($order) {
$q->orderBy('full_name', $order);
});
})
->orderColumn('user.current_work_experience.agency_name', function ($query, $order) {
$query->whereHas('user', function ($q) use ($order) {
$q->whereHas('currentWorkExperience', function ($q) use ($order) {
$q->orderBy('agency_name', $order);
});
});
})
->orderByNullsLast()
->make(true);
}
Views:
$('#hostel_datatable').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('hostel.application.list') }}",
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false},
{data: 'user.full_name', name: 'user.full_name'},
{data: 'user.current_work_experience.agency_name', name: 'user.current_work_experience.agency_name'},
{data: 'user.userProfile.address_1', name: 'user.userProfile.address_1', orderable: false, searchable: false},
{data: 'status.base_setup_name', name: 'status.base_setup_name', orderable: false, searchable: false},
{
data: 'action',
name: 'action',
orderable: true,
searchable: true
},
]
});
I hope you guys can help me out. Thank you so much :)

Sorting by eloquent relationship in Laravel using DataTables

I'm trying to use DataTables to display list of Application model.
Application model has operations:
public function operations(){
return $this->hasMany('\App\Operation', 'application_id', 'id');
}
Operations model has state:
public function state(){
return $this->belongsTo('App\State', 'status_id', 'id');
}
and operation author (user):
public function user(){
return $this->hasOne('App\User', 'id', 'user_id');
}
To enable datatable i'm using this code:
$(function() {
$('#applications-table').DataTable({
language: {
"url": "/js/Polish.json"
},
processing: true,
serverSide: true,
ajax: '{!! route('applications.data') !!}',
columns: [
{ data: 'number', name: 'number', className: "vertical_middle", searchable: true},
{ data: 'operations.0.correspondence', name: 'operations.correspondence', className: "vertical_middle", orderable: false, searchable: false},
{ data: 'operations.0.comment', name: 'operations.comment', className: "vertical_middle", orderable: false, searchable: true},
{ data: 'operations.0.prognosed', name: 'operations.prognosed', className: "vertical_middle", searchable: false},
{ data: 'operations.0.state.name', name: 'operations.state.name', className: "vertical_middle", searchable: false},
{ data: 'operations.0.created_at', name: 'operations.created_at', className: "vertical_middle", type: "date", searchable: true},
]
});
});
ApplicationController is serving data using this code
$model = Application::with(array(
'operations' => function ($query) {
$query->orderByDesc('operations.created_at')->with('state')->with('user');
}
));
return Datatables::of($model)->make(true);
And finally a problem - table is rendering properly, but when I try to sort by another column than the first one I receive warning:
DataTables warning: table id=applications-table - Requested unknown parameter 'operations.0.correspondence' for row 0.
Honestly, I have no idea how to make it work.
Thank you in advance.
Fixed by using DB::table
$applications = DB::table('applications as a')
->join('operations as o', function($join){
$join->on('o.application_id', '=', 'a.id')
->on('o.id', '=', DB::raw("(SELECT max(id) from operations WHERE operations.application_id = a.id)"));
})
->join('users as u', 'o.user_id', '=', 'u.id')
->join('states as s', 'o.status_id', '=', 's.id')
->select(['a.id as a_id','a.number','o.*', 'u.name as u_name', 'u.surname as u_surname', 's.name as s_name']);
return Datatables::of($applications)->make(true);

Export Image View in Datatable PDF and Excel

I'm working with a Laravel Blade file. I want to export the table to PDF, and it seems to work correctly. However, there is a problem while displaying the image. It only shows the image path, not the image after exporting.
exportOptions: {
stripHtml: false,
Added but not working
$(document).ready(function() {
var buttonCommon = {
extend : 'pdfHtml5',
exportOptions: {
stripHtml: false,
columns: 'th:not(:last-child)',
exportOptions: {
stripHtml: false,
//columns: 'th:not(:nth-child(2))',
format: {
body: function ( data, row, column, node ) {
// Strip $ from salary column to make it numeric
return column === 4 ?
data.replace( /[$,]/g, '' ) :
data;
}
}
}
}
};
//$.noConflict();
$('.datatable').DataTable({
processing: true,
serverSide: true,
//"oSearch": { "bSmart": false, "bRegex": true },
ajax: '{{ route('customers.getdata') }}',
columns: [
{data: 'id', name: 'id'},
{data: 'profilepic', name: 'profilepic',render: function( data, type, full, meta ) {
if(data==0) { return "<img src=\"/customers/default.png" + "\" height=\"50\"/>"; } else { return "<img src=\"/customers/" + data + "\" height=\"50\"/>"; }
}},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'phone', name: 'phone'},
{data: 'address', name: 'address'},
{data: 'created_by', name: 'created_by'},
{data: 'status', name: 'status',render: function( data, type, full, meta ) {
if(data==1) { return "Active"; } else { return "Inactive"; }
}},
{data: 'action', name: 'action', orderable: false, searchable: false}
],
dom: 'Bfrtip',
buttons: [
$.extend( true, {}, buttonCommon, {
extend: 'copyHtml5',
} ),
$.extend( true, {}, buttonCommon, {
extend: 'excelHtml5',
} ),
$.extend( true, {}, buttonCommon, {
extend: 'pdfHtml5',
} )
]
});
});
I need an image view rather than an image path.

jquery-uniform radio button not working yajra-laravel-datatable package (laravel 5.6)

I use yajra-laravel-datatable (laravel 5.6).
I have put jquery-uniform radio button in datatable but that is not working.
I use $.uniform.update() but it is not working.
$(".styled_color_checkbox").uniform({
radioClass: 'choice',
wrapperClass: 'border-indigo-600 text-indigo-800'
});
Blade file code
var oTable = $('.finance_payment_datatable').DataTable({
processing: false,
serverSide: true,
ajax: {
url: '{!! route('admin.finance.invoice.datatable') !!}',
data: function (d) {
var customer_id = $(".customer_select option:selected").val();
if (typeof customer_id == 'undefined'){
customer_id = '0';
}
if (customer_id !== '0') {
d.customer_id = customer_id;
}
}
},
columns: [
{data: 'maincheck', sortable: false, orderable: false, searchable: false},
{data: 'invoice_number', name: 'invoice_number', sortable: true},
{data: 'created_at', name: 'created_at', sortable: true},
{data: 'outstanding_price', name: 'outstanding_price', sortable: true},
],
order: [[2, 'desc']],
"pagingType": "full_numbers",
"language": {
"emptyTable": "No invoice to display",
"search": '<span>Filter:</span> _INPUT_',
"lengthMenu": '<span>Show:</span> _MENU_',
},
autoWidth: false,
'fnDrawCallback': function( oSettings ) {
$.uniform.update();
}
});
Controller file code
public function invoiceDatatable(Request $request) {
$raw = Invoice::with('user')->select('invoices.*');
if ($request->has('customer_id')) {
$raw->where('invoices.user_id', $request->get('customer_id'));
}
$raw->where('invoices.outstanding_price', '>=', '1');
$datatable = app('datatables')->of($raw)
->addColumn('maincheck', function ($raw) {
return '<label><input type="radio" name="invoice" class="styled_color_checkbox outstanding_price" data-amount="'.$raw->outstanding_price.'" value="'.$raw->id.'"></label>';
})
->editColumn('created_at', function ($raw) {
return date('Y/m/d', strtotime($raw->created_at));
})
->editColumn('outstanding_price', function ($raw) {
return '$'.$raw->outstanding_price;
});
return $datatable->make(true);
}

Laravel DataTables does not render HTML

I'm using Laravel Datatables 7, but my table is not rendering HTML code. It was rendering HTML before, but when I updated to new Laravel DataTables to 7 from 6, it stopped rendering HTML in column.
http://prntscr.com/e11n84
This is with Laravel DataTables 6 - http://prntscr.com/e11ph0
$(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '{{ route("admin.access.user.get") }}',
type: 'post',
data: {status: 1, trashed: false}
},
columns: [
{data: 'id', name: '{{config('access.users_table')}}.id'},
{data: 'name', name: '{{config('access.users_table')}}.name', render: $.fn.dataTable.render.text()},
{data: 'email', name: '{{config('access.users_table')}}.email', render: $.fn.dataTable.render.text()},
{data: 'confirmed', name: '{{config('access.users_table')}}.confirmed'},
{data: 'roles', name: '{{config('access.roles_table')}}.name', sortable: false},
{data: 'created_at', name: '{{config('access.users_table')}}.created_at'},
{data: 'updated_at', name: '{{config('access.users_table')}}.updated_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
order: [[0, "asc"]],
searchDelay: 500
});
});
Add the column with html as shown on other answers and after all add this ->rawColumns(['html_column', 'another_html_column']) before ->toJson() to render all the html column as sent by the server
Try to feed data as JSON from the controller by using toJson() method.
$data= User::all();
return datatables()->of($data)
->addColumn('action', function ($row) {
$html = 'Edit ';
$html .= '<button data-rowid="'.$row->id.'">Del</button>';
return $html;
})->toJson();
Ref: https://laravelarticle.com/laravel-yajra-datatables
//im also using yajra data tables..
//it's easy to render html from controller...
example:
$query = Appointment::all();
$table = Datatables::of($query);
$table->editColumn('user_id', function ($row) {
return $row->user_id ? '<span style="color: green;">ACCEPTED</span>' :
'<span style="color: red;">PENDING</span>';
});
//then you need to add the column you want to render html
$table->rawColumns(['user_id']);

Categories