我有数据返回从控制器ajax响应json格式,我可以在控制台打印数据。但我想把它显示在表格中,我该怎么显示呢?我做了一个解决方案的搜索,但所有的解决方案写HTML代码在控制器函数和返回它作为响应,所以有任何其他的解决方案
我的控制器
<?php
namespace AppHttpControllersbackend;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppCategory;
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>
我能告诉你的最好的方法是为那个表创建一个刀片,然后在控制器中像往常一样返回数据给那个视图:
return view('your-blade-just-for-table')->with('data', $data);
和往常一样,在你的刀片中,你用laravel在你的表中渲染你的数据
所以laravel将返回一个刀片到你的ajax-success方法
在你的success方法中你可以这样做:
success: function (data) {
$('.list-group').html(data);
},
在你想要的表格位置放这个div:
<div class="list-group" id="myFileContainer"></div>