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

问题描述 投票:0回答: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>中显示。我该怎么做?

php jquery datatables laravel-5.2
2个回答
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'},
              ]

          });

0
投票

好像你想动态地在数据表中添加一列,所以你可以这样做,

    $datatables =  Datatables::of($products)->addColumn('total',$price * $quantity);
   return $datatables->make(true);
© www.soinside.com 2019 - 2024. All rights reserved.