【问题标题】:How to perform arithmetic calculations in datatables server side processing using Laravel 5.2?如何使用 Laravel 5.2 在数据表服务器端处理中执行算术计算?
【发布时间】:2017-01-01 09:51:42
【问题描述】:

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

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>

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

【问题讨论】:

标签: php jquery datatables laravel-5.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'},
              ]

          });

【讨论】:

  • 它只返回 NaN。也许在那里使用了演员表操作员?
  • 它说 Uncaught TypeError: Cannot read property 'price' of undefined
【解决方案2】:

您似乎想即时向数据表添加一列, 所以你可以这样做,

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

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2016-02-12
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多