更新和过滤 Laravel 数据表

问题描述 投票:0回答:1

我想返回一个包含数组中集合的数据表,我可以从浏览器的网络检查器中看到响应和预览选项卡,所有数据都显示在那里,但由于某种原因,如果我尝试,该表会显示为空在 foreach 循环内返回数据表仅返回一个结果,但显示在表上

我有两个问题,

  1. 如何获取必须在数据表上显示的数据。
  2. 当填写日期输入并单击过滤器按钮时,如何按日期过滤数据

这是将订单带到页面的功能

  public function anyData(){
        
        $orders = Order::simplePaginate(10);
        foreach ($orders as $order){
      
            $id = $order->purchaser_id;
            $user = \App\Models\User::where('id', '=', $id)->first();
            $user_category = \App\Models\UserCategory::where('user_id', $id)->value('category_id');
            $category = \App\Models\Category::where('id', $user_category)->value('name');
            $referrer = \App\Models\User::where('id', '=', $user->referred_by)->first();
            $order_item = \App\Models\OrderItem::where('order_id', $order->id)->first();
            $quantity = $order_item->qantity;
            $product = \App\Models\Product::where('id', $order_item->product_id)->first();
            $price = $product->price;
            $order_total = $price * $quantity;
            
            if ($referrer) {
                $referred_distributors = \App\Models\User::where('referred_by', $referrer->id)
                    ->where('enrolled_date', '<', $order->order_date)
                    ->count();
            
                if ($referred_distributors < 5) {
                    $percentage = 5;
                } elseif ($referred_distributors >= 5 && $referred_distributors <= 10) {
                    $percentage = 10;
                } elseif ($referred_distributors >= 11 && $referred_distributors <= 20) {
                    $percentage = 15;
                } elseif ($referred_distributors >= 21 && $referred_distributors <= 30) {
                    $percentage = 20;
                } elseif ($referred_distributors >= 31) {
                    $percentage = 30;
                }
              
            }else{
                $referred_distributors = '0';
                $percentage = '0';
            }

            $datas[] = collect([
                ['order' => $order],
                ['user' => $user],
                ['referrer' => $referrer],
                ['referred_distributors' => $referred_distributors],
                ['percentage' => $percentage],
                ['commision' => ($percentage * $order_total)],
               
            ]);
            
        }

      
        return app('datatables')->collection($datas)
                ->addIndexColumn()
                ->addColumn('action', function($row){
                    $actionBtn = '<a href="#invoice" class="invoice more" data-toggle="modal"
                    data-target="#invoice" data-id="$order->id">View Item</a>';
                    return $actionBtn;
                })
                ->rawColumns(['action'])
                ->make(true);
      
     
        

       // return Datatables::of(User::query())->make(true);
    }

这个表格显示了当前运行良好的数据

  <table class="table table-bordered" id="users-table">
                            <div class="col-md-3 mb-5">
                                <h2>Date from</h2>
                                <input type="date" class="form-control" id="min" name="min"
                                    data-date-split-input="true">
                            </div>
                            <div class="col-md-3 mb-5">
                                <h2>Date to</h2>
                                <input type="date" class="form-control" id="max" name="max"
                                    data-date-split-input="true">
                            </div>

                            <thead>
                                <tr>
                                    <th scope="col">Invoice</th>
                                    <th scope="col">Purchaser</th>
                                    <th scope="col">Distributor</th>
                                    <th scope="col">Referred Distributors</th>
                                    <th scope="col">Order Date</th>
                                    <th scope="col">Percentage</th>
                                    <th scope="col">Commission</th>
                                    <th scope="col"></th>
                                </tr>
                            </thead>
                        </table>

                        @push('scripts')
                            <script>
                                $(function() {
                                    $('#users-table').DataTable({
                                        processing: true,
                                        serverSide: true,
                                        ajax: "{{ route('datatables.data') }}",


                                        columns: [{
                                                data: 'order.id',
                                                name: 'order'
                                            },
                                            {
                                                data: 'user.first_name',
                                                name: 'user.first_name'
                                            },
                                            {
                                                data: 'referrer.first_name',
                                                name: 'referrer.first_name'
                                            },
                                            {
                                                data: 'referred_distributors',
                                                name: 'referred_distributors'
                                            },
                                            {
                                                data: 'order.order_date',
                                                name: 'order.order_date'
                                            },

                                            {
                                                data: 'percentage',
                                                name: 'percentage'
                                            },
                                            {
                                                data: 'commision',
                                                name: 'commision'
                                            },

                                        ]
                                    });
                                });
                            </script>
                        @endpush

如果我最终得到要在表格上显示的数据,这现在让我想到第二个问题,当单击过滤器按钮时如何使用日期输入进行过滤。

谢谢

jquery mysql ajax laravel datatables
1个回答
1
投票

我建议使用数据表。

https://datatables.net/

或者更好的是,使用 Laravel 数据表:https://datatables.yajrabox.com/

我还一直在开发一个简化范围过滤的库:https://laraveldocs.itulbuild.com/documentation/slickFilters (仍在进行中)

上面的 slickFilter.js 文档中概述了您需要了解的有关如何处理列范围过滤的所有信息(即使您不使用该库)。

----- 编辑 ----

我注意到你的 javascript 列定义看起来不正确。

这可能更接近你想要的

columns: [{
        data: 'invoice',
        name: 'order.id'
    },
    {
        data: 'purchaser',
        name: 'user.first_name'
    },
    {
        data: 'distributer',
        name: 'referrer.first_name'
    },
    {
        data: 'referred_distributors',
        name: 'referred_distributors'
    },
    {
        data: 'order_date',
        name: 'order.order_date'
    },

    {
        data: 'percentage',
        name: 'percentage'
    },
    {
        data: 'commision',
        name: 'commision'
    },
    {
        data: 'action',
        filterable: false,
        sortable: false
    }
]
© www.soinside.com 2019 - 2024. All rights reserved.