用ajax和laravel搜索

时间:2018-04-03 11:47:38

标签: jquery ajax laravel laravel-5.5 laravel-5.6

我有一个名为缩进的表。我需要通过输入客户名称来搜索并获取详细信息。 当我按下一个按钮时,它应该返回数据表中的客户详细信息,我正在使用ajax和laravel。当它工作时,它只是在我尝试搜索名称

时才返回最后一个数据

索引文件

       <script>

        $(document).ready(function () {
            $('#search_data').on('click', function () {
                $value = $(this).val();
                $.ajax({
                    type: 'post',
                    url: '{{\Illuminate\Support\Facades\URL::to('search')}}',
                    data: {'search': $value},
                    success: function (data) {
                        $('#edpinfo').html(data);

                    }
                })

            })
        })
    </script>
    <script>
        $.ajaxSetup({headers: {'csrftoken': '{{ csrf_token() }}'}});
    </script>
    <br>
    <div class="row">
        <div class="col-12">
            <div class="card m-b-30">
                <div class="card-body">

                    <h4 class="mt-0 header-title">Indents</h4>

                    <input type="text" id="search" class="pull-right form-rounded">
                    <a href="{{route('edp')}}">
                        <button class="btn btn-sm btn-primary pull-left">Back</button>
                    </a>
                    <button id="search_data" class="btn btn-danger btn-sm pull-right">search</button>

                    <br>
                    <br><br>
                    <table id="datatable" class="table table-bordered  table-responsive-lg">
                        <thead>
                        <tr>
                            <th>Slno</th>
                            <th>Customer Name</th>
                            <th>Customer Phone Number</th>
                            <th>DateOfDelivery</th>
                            <th>Delivery At</th>
                            <th>Show</th>

                        </tr>
                        </thead>
  <tbody id="edpinfo">
                    @foreach($indents as $indent)
                        <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$indent->customer_name}}</td>
                            <td>{{$indent->phone_no}}</td>
                            <td>{{$indent->date_of_delivery}}</td>
                            <td>{{$indent->delivery_at}}</td>
                            <td><a href="{{route('edp.show',$indent->id)}}"><img src="assets/images/select.jpg"
                                                                                 class="imgsize"></a></td>

                        </tr>
                    @endforeach
                    </tbody>
                </table>
        </div>
    </div>
    </div>

控制器文件

    public function search(Request $request)
    {

        if ($request->ajax()) {

            $output = "";
            $indents = indents::table('indents')
                ->where('customer_name', 'LIKE', '%' . $request->search . '%')->get();
            foreach ($indents as $key => $indent) {

                        $output = '<tr>' .
                        '<td>' . $indent->id . '</td>' .
                        '<td>' . $indent->customer_name . '</td>' .
                        '<td>' . $indent->phone_no . '</td>' .
                        '<td>' . $indent->date_of_delivery . '</td>' .
                        '<td>' . $indent->delivery_at . '</td>' .
//                        '<td>'.'.<a href="{{route(\'edp.show\',$indent->id)}}">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
                        '</tr>';

            }
            return Response($output);
        }

    }

1 个答案:

答案 0 :(得分:0)

您的问题是您始终覆盖$output = '<tr>'变量

而不是$output .= '<tr>'

pointer-events(注意点)