如何使用Laravel 5.2在数据表服务器端处理中执行算术计算?

时间:2017-01-01 09:51:43

标签: php jquery datatables laravel-5.2

我试图弄清楚如何对使用数据表服务器端处理返回的数据执行算术运算。这很容易使用客户端数据表执行,但如果使用服务器端处理,怎么会我这样做这是我的代码。

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>

显示上面的代码,我想将价格乘以数量,然后在<th>Total</th>中显示。我该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试使用Render function

它允许在将数据显示在表格之前操纵数据。

$('#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'},
              ]

          });

答案 1 :(得分:0)

似乎你想动态地向数据表中添加一列, 所以你可以这样做,

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