Php 如何使用ajax获取分页链接(Laravel4.2)
你好 如何传递Php 如何使用ajax获取分页链接(Laravel4.2),php,jquery,ajax,laravel-4,laravel-pagination,Php,Jquery,Ajax,Laravel 4,Laravel Pagination,你好 如何传递例如:$transactions->links()在laravel上执行ajax调用时 这是我的控制器在正常情况下的请求: $transactions = DB::table('x_general_transactions') ->whereBetween('x_general_transactions.date_at', array($startDate,$endDate)) ->paginate(30); 看法 根据ajax请求 if (Reque
例如:$transactions->links()代码>在laravel上执行ajax调用时
这是我的控制器在正常情况下的请求:
$transactions = DB::table('x_general_transactions')
->whereBetween('x_general_transactions.date_at', array($startDate,$endDate))
->paginate(30);
看法
根据ajax请求
if (Request::ajax())
{
//$divs is my data container
$res = [$divs,$transactions->links()];
return Response::json($res);
}
因此,当我尝试在ajax代码上使用.load
时
$.ajax({
url: href,
type: 'get',
dataType: 'json',
success: function(response){
$('.pagination').load(response[1]);
});
});
但是什么也没发生,我也试过了
console.log(response[1]);
ajax响应:
[[[{"gt_xid":1230,"gts_xid":1231,"xid":4728,"rnr":4,"code":"OR#","link_code":"CI#","link_rnr":6,"g_type":25,"account_name":"Cash on Hand","debit":50.5,"credit":0,"description":"","date_at":"2015-10-25 16:25:19"},{"gt_xid":1230,"gts_xid":1231,"xid":4729,"rnr":4,"code":"OR#","link_code":"CI#","link_rnr":6,"g_type":25,"account_name":"Accounts Receivable - Trade","debit":0,"credit":50.5,"description":"","date_at":"2015-10-25 16:25:19"}]],{}]
结果:
对象{}`为空。我希望这有帮助
控制器响应(使用render呈现html并发送响应)
ajax代码
希望此代码对您有所帮助
路线:
控制器:
视图(home.ajax.data):
为TestController中的加载数据创建php文件
<div id="content">
<table>
<tr>
<td>id</td>
<td>transaction</td>
<td>customer</td>
</tr>
@foreach($transaction as $key => $val )
<tr>
<td>{{ $val->id }}</td>
<td>{{ $val->transaction }}</td>
<td>{{ $val->customer }}</td>
</tr>
@endforeach
<tfoot>
<tr>
<td colspan="3" align="center">{{ $transaction->links() }}</td>
</tr>
</tfoot>
</table
</div>
REF:你能发布Ajax的JSON响应吗谢谢你路过先生,我已经更新了上面的问题。你可以检查一下,因为我知道他的方法加载了整个刀片模板,对吗?难道没有办法只加载分页器吗?因为如果是这样的话,我必须重新编码我的大多数视图来应用它,现在分页器链接是唯一正常工作的链接。谢谢你的回答,先生,但是有了这段代码,我仍然得到了object{}
,这是console.log(response.links)
helloo。。我的错。。我已经编辑了答案,请查收。很抱歉很晚才回复。。我有事要办。。但是你的答案是最好的!我从昨天开始一直在研究,我不知道我可以使用渲染!xD然后我被迫应用gist.github.com/tobysteward/6163902方法,该方法使我更改了大部分代码。。无论如何,我非常感谢你的好意。。我多么希望你昨天就回答了:)
if (Request::ajax())
{
$res = array(
'data' => $divs,
'links' => $transactions->links()->render()
);
return Response::json($res);
}
$.ajax({
url: href,
type: 'get',
dataType: 'json',
success: function(response){
console.log(response.data);//get data
console.log(response.links);//get links
$('.pagination').load(response.data);
});
});
Route::get('/home/ajax/get','TestController@getAjax');
public function getAjax() {
$transactions = DB::table('x_general_transactions')
->whereBetween('x_general_transactions.date_at', array($startDate,$endDate))
->paginate(30);
return View::make('home.ajax.data')->with('transaction',$transaction);
}
<div id="content">
<table>
<tr>
<td>id</td>
<td>transaction</td>
<td>customer</td>
</tr>
@foreach($transaction as $key => $val )
<tr>
<td>{{ $val->id }}</td>
<td>{{ $val->transaction }}</td>
<td>{{ $val->customer }}</td>
</tr>
@endforeach
<tfoot>
<tr>
<td colspan="3" align="center">{{ $transaction->links() }}</td>
</tr>
</tfoot>
</table
</div>
<html>
<head>
<title>Transaction</title>
</head>
<body>
<div class="box-body table-responsive no-padding">
@include('home.ajax.data')
</div>
</body>
</html>
$(window).on('hashchange',function(){
page = window.location.hash.replace('#','');
});
$(document).on('click','.pagination a', function(e) {
e.preventDefault();
//to get what page, when you click paging
var page = $(this).attr('href').split('page=')[1];
//console.log(page);
getTransaction(page);
location.hash = page;
});
function getTransaction(page) {
//console.log('getting sort for page = '+page);
$.ajax({
type: 'GET',
url: '{{URL::to("/home/ajax/get?page=")}}'+page
}).done(function(data) {
console.log(data);
$('#content').html(data);
});
}