2017-01-01 3 views
0

私は、データテーブルのサーバー側の処理を使用して返されたデータに対して算術演算を実行する方法を理解しようとしていません。クライアント側のデータ型を使用して実行するのは簡単ですが、サーバー側の処理を使用する場合、 。ここに私のコードがあります。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\[email protected]'); 
Route::get('postproductsdata', 'Product\[email protected]'); 

の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>に表示量に価格を乗算したかったです。どうすればいい?

+0

http://bobby-tables.com/ – Sherif

+0

どのように操作を行いますので、あなたはこのように行うことができます 、あなたはその場でのDataTableに列を追加したいと思われます返されたデータについて – Eli

答えて

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

NaNだけを返します。そこにキャストオペレータが使われているのでしょうか? – Eli

+0

Uncaught TypeError:未定義のプロパティ 'price'を読み取ることができません – Eli

+0

私の答えを編集しました、今すぐ試してください。@ Eli –

0

$datatables = Datatables::of($products)->addColumn('total',$price * $quantity); 
    return $datatables->make(true); 
関連する問題