3

再会,

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

这是我的控制器在正常请求下:

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

所以当我尝试.load在我的 ajax 代码上使用时

 $.ajax({
          url: href,
          type: 'get',
          dataType: 'json',
          success: function(response){
            $('.pagination').load(response[1]);
         });
 });

但是什么也没发生,我也试过

console.log(response[1]);

阿贾克斯响应:

[[[{"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:对象 {}` 是空的。

4

2 回答 2

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);
         });
 });
于 2015-11-02T04:42:12.373 回答
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 脚本(主页):

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

于 2015-11-02T05:46:02.350 回答