php - 如何从 laravel blade 中的 json 数据创建分页链接

标签 php jquery json laravel

我正在尝试从 laravel blade 模板中的 json 响应创建分页链接。我知道它可以简单地使用 php 创建,但我如何从 json 响应中做到这一点。

这是我的 Controller 方法:

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。

最佳答案

在你的 Controller 中

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

关于php - 如何从 laravel blade 中的 json 数据创建分页链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34563494/

相关文章:

PHP Imagick 无法读取 SVG 文件

php - 使用 Pgsql 驱动程序从 Laravel 5 连接到 Redshift 数据库?

javascript - 函数如何获取 'x' 值

javascript - 传单 map : how to removeLayer and add popups to the map?

json - 如何解析这个 JSON 对象/字符串?

php 注销不起作用

php - 在 mysql 中查找、修剪和更新列

javascript - 如果元素可见,则自动增加步数

json - 使用 Go 发布解析 JSON 文件

javascript - 下载 json 文件并将其导入到您的 firebase 数据库