Question: How to perform arithmetic calculations in datatables server side processing using Larvel 5.2?

Question

How to perform arithmetic calculations in datatables server side processing using Larvel 5.2?

Answers 2
Added at 2017-01-01 10:01
Tags
Question

I'm stuck trying to figure out how to perform arithmetic operations to data returned using the datatables server-side processing.This is just easy to perform using the client side datatables but if using the server side processing, how would I do this. Here are my codes. Thank you for the help.Happy new year everyone!

ProductController.php:

public function anyData()
{
    $conditionTxt = "Medical and Lab Supplies";

    $products = Product::where('category', 'ILIKE', '%'.$conditionTxt.'%')
                        ->orderBy('created_at', 'desc')
                        ->get();

    return Datatables::of($products)->make(true);

}

route.php

Route::get('datatables', 'Product\ProductController@index');
Route::get('postproductsdata', 'Product\ProductController@anyData');

javascript:

$('#table-prod-contents').DataTable({
              processing: true,
              serverSide: true,
              ajax: $.fn.dataTable.pipeline( {
                  url: '{{ url("postproductsdata") }}',
                  pages: 6000 // number of pages to cache
              } ),
              columns: [
                  {data: 'id', name: 'id'},
                  {data: 'category', name: 'category'},
                  {data: 'pharmaceutical', name: 'pharmaceutical'},
                  {data: 'description', name: 'description'},
                  {data: 'type', name: 'type'},
                  {data: 'unit', name: 'unit'},
                  {data: 'price', name: 'price'},
                  {data: 'quantity', name: 'quantity'},
                  {data: 'created_at', name: 'created_at'},
              ]

          });

html:

<table id="table-prod-contents" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th>Category</th>
            <th>Product Name</th>
            <th>Description</th>
            <th>Type</th>
            <th>Unit</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Total</th>
            <th>Created at</th>
        </tr>
    </thead>


</table>

Showing the code above, I wanted to multiply price to quantity then display it in the <th>Total</th>. How would I do this? Please help.

Answers
nr: #1 dodano: 2017-01-01 15:01

Try using Render function

it let's manipulate the data before showing it in the table.

$('#table-prod-contents').DataTable({
              processing: true,
              serverSide: true,
              ajax: $.fn.dataTable.pipeline( {
                  url: '{{ url("postproductsdata") }}',
                  pages: 6000 // number of pages to cache
              } ),
              columns: [
                  {data: 'id', name: 'id'},
                  {data: 'category', name: 'category'},
                  {data: 'pharmaceutical', name: 'pharmaceutical'},
                  {data: 'description', name: 'description'},
                  {data: 'type', name: 'type'},
                  {data: 'unit', name: 'unit'},
                  {data: 'price', name: 'price'},
                  {data: 'quantity',
                       render:function(data,type,row){
                        return (parseInt(data) * parseFloat(row[6]));
                            },
                  {data: 'created_at', name: 'created_at'},
              ]

          });
nr: #2 dodano: 2017-01-05 07:01

seems you want to add a column to datatable on the fly, so you can do like this,

    $datatables =  Datatables::of($products)->addColumn('total',$price * $quantity);
   return $datatables->make(true);
Source Show
◀ Wstecz