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


How to get pagination links using ajax (laravel 4.2)

你好,

在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:对象{}`为空。

我希望这能帮助

控制器响应(使用render渲染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);
         });
 });

希望此代码能帮助您

路线:

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脚本(主页):

将此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