我使用的是laravel 5.8,有两天我在做一些搜索,以及如何在datatables中检索和显示数据,但没有结果
//这是我的控制器文件的一部分代码
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Datatables;
use Auth;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user =Datatables::of(User::query())->make(true);
return view('manage_users.index', ['users' => $user]);
}
/**
和html页面和脚本的代码。
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script
src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<link href="styles/vendor/AdminLTE/css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<link href="bower_components/datatables-responsive/css/dataTables.responsive.css" rel="stylesheet" type="text/css" />
<table id="table" class="table table-bordered table-striped dataTable">
<thead>
<tr>
<th class="th-sm">ID</th>
<th class="th-sm">Name</th>
<th class="th-sm">Email</th>
<th class="th-sm">Type user</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Id
</th>
<th>Name
</th>
<th>Email
</th>
<th>Type user
</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$('#table').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "../server_side/scripts/server_processing.php"
} );
} );
</script>
我得到了带有ajax错误的空表,如下所示: DataTables warning: table id=table - Ajax error。有关此错误的更多信息,请参阅http://datatables.net/tn/7
发布于 2019-04-17 03:50:50
你做错了几件事。
上的列
请按此操作。
在控制器上
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Datatables;
use Auth;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user =User::get();
return view('manage_users.index', compact('user'));
}
public function returnAjaxData()
{
$users =User::get();
return Datatables::of($users)->make(true);
}
/**
Ajax”F220
按照前面提到的方式创建一条路由,示例如下:
Route::get('user', array('as' => 'get.user', 'uses' => 'UserController@returnAjaxData'));
将ajax url替换为"ajax":"/user",
https://stackoverflow.com/questions/55714362
复制相似问题