如何从laravel刀片中的json数据创建分页链接

时间:2016-01-02 07:00:36

标签: php jquery json laravel

我正在尝试在laravel刀片模板中创建json响应的分页链接。我知道它可以创建使用PHP,但我怎么能从json响应。

这是我的控制器方法:

function getContact()
{   
     return $contacts = Contact::where(array('is_active'=>1))
                        ->paginate(2);
}

这是我的jquery ajax代码,用于在表格上显示数据......

   $.ajax({
            type:'GET',
            url: '<?php echo URL::to('contactgroup/contact') ?>', 

            dataType: 'json',               
            success: function(data){
                //console.log(data['data'].length);
            var table='<thead><th><input type="checkbox" id="checkAll" name="checkAll[]"/></th><th>Name</th><th>contact No</th></thead>';
            for(var i=0; i<data['data'].length; i++){
                table += '<tr><td><input type="checkbox" name="checkbox[]" class="individualCheckbox" value="'+data['data'][i].id+'"/></td><td>'+data['data'][i].contact_name+'</td><td>'+data['data'][i].primary_contact_no+'</td></tr>';

            }
            $('#contactTable').empty();
            $('#contactTable').append(table);
           }
       });

并且console.log中的json响应是 -

{"total":3,"per_page":2,"current_page":1,"last_page":2,"next_page_url":"http:\/\/localhost\/smsapi\/public\/contactgroup\/contact\/?page=2","prev_page_url":null,"from":1,"to":2,"data":[{"id":1,"contact_name"
:"M Islam","primary_contact_no":"017********","personal_email":"m@company.com","work_email"
:"m@company.com","personal_phone":"017********","work_phone":"017********","personal_address":"abc","work_address":"ring road","is_active":1,"entry_by":7},{"id":4,"contact_name":"sdsdf","primary_contact_no":"242342","personal_email":"mu@company.com","work_email":"mu@company.com","personal_phone":"12142","work_phone":"fgbf","personal_address":"gfg","work_address":"fgfg"
,"is_active":1,"entry_by":7}]}

我可以使用上面的jquery代码将数据显示到表中,但是如何从"total":3,"per_page":2,"current_page":1,"last_page":2,"next_page_url":"http:\/\/localhost\/smsapi\/public\/contactgroup\/contact\/?page=2","prev_page_url":null,"from":1,"to":2,显示分页

使用jquery。

1 个答案:

答案 0 :(得分:4)

在您的控制器中

function getContact()
{   
   $contacts = Contact::where(array('is_active'=>1))
                    ->paginate(2);

   //return multiple value in JSON format
   return \Response::JSON(array(
                                   'data'       => $contacts, 
                                   'pagination' => (string) $contacts->links()
                                )
                          );
}

在你看来

 $.ajax({
        type:'GET',
        url: '<?php echo URL::to('contactgroup/contact') ?>', 

        dataType: 'json',               
        success: function(data){
            //console.log(data['data'].length);
        var table='<thead><th><input type="checkbox" id="checkAll" name="checkAll[]"/></th><th>Name</th><th>contact No</th></thead>';
        for(var i=0; i<data['data'].length; i++){
            table += '<tr><td><input type="checkbox" name="checkbox[]" class="individualCheckbox" value="'+data['data'][i].id+'"/></td><td>'+data['data'][i].contact_name+'</td><td>'+data['data'][i].primary_contact_no+'</td></tr>';

        }
        $('#contactTable').empty();
        $('#contactTable').append(table);
        $('#pagination').html(data['pagination']);   //add this element in your HTML as well
       }
   });