如何使用ajax获取分页链接(laravel 4.2)

时间:2015-11-01 03:44:59

标签: php jquery ajax laravel-4 laravel-pagination

美好的一天,

在laravel上进行ajax调用时如何通过e.g: $transactions->links();

按照正常请求继续我的控制器:

$transactions = DB::table('x_general_transactions')
    ->whereBetween('x_general_transactions.date_at', array($startDate,$endDate))
    ->paginate(30);

查看

{{ $transactions->(array('from' => $startDate, 'to'=>$endDate))->links() }}

和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"}]],{}]

result:对象{}`为空。

2 个答案:

答案 0 :(得分:1)

我希望这会有所帮助

控制器响应(使用渲染来呈现html并发送响应)

if (Request::ajax())
{
 $res = array(
  'data' => $divs,
  'links' => $transactions->links()->render()
 );
 return Response::json($res);
}

ajax代码

 $.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);
         });
 });

答案 1 :(得分:0)

希望此代码可以帮助您

路线:

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

查看(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>

查看(主页):

主页视图加载您的页面

<html>
 <head>
  <title>Transaction</title>
 </head>
 <body>
  <div class="box-body table-responsive no-padding">
   @include('home.ajax.data')
  </div>
 </body>
</html>

JS Script(home):

将此javascript放在主视图

$(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);
    });
}

参考:https://gist.github.com/tobysteward/6163902