douyanjing8287 2017-05-11 10:18
浏览 51
已采纳

使用Laravel 5.3上的数据表对数据进行排序

I'm using Datatables on my Laravel project to show stock of products, but when I tried to sort it, it's error

This is my script in view

@section('script')
    <script type="text/javascript">
        $(function () {
            var oTable = $('#stock-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    url: '{{ url("stock-data") }}'
                },columns: [
                    {data: 'updated_at', name: 'updated_at'},
                    {data: 'product_name', name: 'product_name'},
                    {data: 'unit_stock', name: 'unit_stock'},
                    {data: 'status', name: 'status'},
                ],
            });
        });
    </script>
@endsection

And this is my controller

public function stock()
    {
        return view('stock-report');
    }

    public function stockData()
    {
        $stock = Products::all();
        return Datatables::of($stock)
//            ->orderColumn('unit_stock $1')
            ->addColumn('status', function ($stock) {
                if ($stok->unit_stock == 0)
                    return '<span class="label label-danger">EMPTY</span>';
                else
                    return '<span class="label label-success">NOT EMPTY</span>';
            })
            ->make(true);
    }

It's error when I add ->orderColumn('unit_stock $1'), is there any solution, big thanks

  • 写回答

1条回答 默认 最新

  • douju2331 2017-05-11 11:25
    关注

    try to sort the Datatable adding : order

    $('#stock-table').DataTable( {
    .
    .
    .
           "order": [[ numberColum, "desc" ]],
    .
    .
    } );
    

    also you can try to order in the select:

    Products::orderBy('unit_stock', 'DESC')->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大