如何使用 ajax 在 laravel 的 blade 页面中显示来自控制器的 json 响应数据?

how to display json response data from controller in blade page in laravel using ajax?

我有数据 return 通过 ajax 响应以 json 格式从控制器编辑,我可以在控制台中打印数据。 但是我想在table中显示,怎么显示呢? 我搜索了解决方案,但所有解决方案都在控制器函数中写入 HTML 代码并将其 return 作为响应,所以还有其他解决方案吗

我的控制器

<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Category;

class CategoryCtrl extends Controller
{
    //function search - show categories live search
    public function index(Request $request)
    {
        if($request->ajax())
        {
            $categories = Category::where('name','LIKE','%'.$request->cat_search."%")->orderBY('created_at','DESC')->get();
            return Response()->json($categories);
        }
        else {
            $categories = Category::orderBY('created_at','DESC')->get();
            return view('backend.category.list')->withcategories($categories);
        }
    }

}

查看

@extends('backend.layouts.app')

@section('content')

    <input class="form-control" type="text" name="search" id="cat_search" placeholder="Category Search..." />
                   
    <table id="CategoriesTable" class="table table-striped table-vcenter js-dataTable-simple">
        <thead>
            <tr>
               <th class="text-center w-5">#</th>
               <th class="text-center">Name</th>
               <th class="text-center hidden-xs">Icon</th>
               <th class="text-center hidden-xs">Order</th>
               <th class="text-center w-15">Status</th>
               <th class="text-center">Created At</th>
               <th class="text-center">Last Update</th>
               <th class="text-center" style="width: 15%;">Actions</th>
            </tr>
        </thead>
        <tbody>

                    @foreach ($categories as $key=>$category)
                        <tr>
                            <td class="text-center">{{ $key+1 }}</td>
                            <td class="text-center text-capitalize">{{ $category->name }}</td>
                            <td class="text-center hidden-xs"><i class="{{ $category->icon }} fa-lg"></i></td>
                            <td class="text-center hidden-xs">{{ $category->order }}</td>
                            <td class="text-center text-capitalize"> 
                                <span class="btn btn-sm btn-pill @if($category->status == 'active') btn-primary @else btn-warning @endif">{{ $category->status }}</span> 
                            </td>
                            <td class="text-center">{{ $category->created_at->format("Y-m-d g:i a") }}</td>
                            <td class="text-center">{{ $category->updated_at->format("Y-m-d g:i a") }}</td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="{{ route('category.edit', ['id' => $category->id]) }}" class="btn btn-success">Edit</a>
                                    <button class="btn btn-app" data-toggle="modal" data-target="#{{ $category->name.$category->id }}">Delete</button>
                                </div>
                                <!-- Modal -->
                                <div class="modal fade" id="{{ $category->name.$category->id }}" tabindex="-1" role="dialog" aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-card-header">
                                                <div class="row">
                                                    <h4 class="col-md-10">Delete Category</h4>
                                                    <ul class="card-actions col-md-2">
                                                        <li>
                                                            <button data-dismiss="modal" type="button"><i class="ion-close"></i></button>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                            <div class="card-block">
                                                <p>Are you sure, you want to delete category (<span class="text-capitalize">{{ $category->name }}</span>) ?</p>
                                                <p>If you delete category, you will delete all category's products too.</p>
                                                <p>Notice: if you want to hide category, you can update it's status to not active instad of delete it.</p>
                                            </div>
                                            <div class="modal-footer">
                                                <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
                                                {!! Form::Open(['url' => route('category.delete', ['id' => $category->id]) ]) !!}
                                                    <button class="btn btn-app" type="submit" data-dismiss="modal"> Delete</button>
                                                {!! Form::Close() !!}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- End Modal -->

                            </td>
                        </tr>
                    @endforeach
                   
                </tbody>
            </table>
    
@endsection

ajax代码

<!--  Ajax code for category live search  -->
    <script type="text/javascript">
        $('#cat_search').on('keyup',function(){
            $value = $(this).val();
            $.ajax({
                type : 'get',
                url : '{{ route('category.index') }}',
                data:{'cat_search':$value},
                dataType: 'json',
                success:function(response){
                    //console.log(response);
                });

             });
        })
    </script>

我能告诉的最好方法是为那个 table 制作一个 blade 并像往常一样在控制器中为那个视图制作 return 数据:

 return view('your-blade-just-for-table')->with('data', $data);

并且在您的 blade 中再次像往常一样在您的 table

中使用 laravel 渲染数据

所以 laravel 将 return 一个 blade 到你的 ajax-success 方法

并且在您的成功方法中,您可以执行以下操作:

    success: function (data) {
            $('.list-group').html(data);
        },

无论你想让你的 table 放在什么地方,只要把这个 div:

<div class="list-group" id="myFileContainer"></div>