简体   繁体   中英

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

i have data returned from conntroller by ajax response in json format and I can print data in console. but I want to display it in a table, how can I display it? i made a search for solution but all solution write HTML code in controller function and return it as response, so is there any other solution

my controller

<?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);
        }
    }

}

view

@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 code

<!--  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>

the best way i can tell is to make a blade for that table and in controller just as usual return data to that view:

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

and in your blade again as usual you render your data with laravel in your table

so laravel will return a blade to your ajax-success method

and in your success method you can do something like:

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

and wherever you want your table to be just put this div:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM