Laravel 5.5分页不起作用

时间:2018-02-15 06:00:30

标签: php laravel

在控制器数据来自ajax和搜索工作但分页不工作。我正在使用Laravel 5.5。当我显示结果

时,问题就开始了

但当我试图在视图页面上显示分页时,分页无法正常工作

public function anyListAjax( Request $request ) {
    $user = Auth::guard( 'admin' )->user();
    $data = $request->all();
    if( $data ) { 
      //   print_r($data);
        $sortColumn = array( '', 'i_state_id', 'v_name', 'i_country_id', 'e_status' );
        $query = State::join( 'tbl_country', 'tbl_state.i_country_id', '=', 'tbl_country.i_country_id')->select( 'tbl_state.i_state_id', 'tbl_state.i_country_id', 'tbl_country.v_name as country_name', 'tbl_state.v_name', 'tbl_state.e_status' );

        if( isset( $data[ 'i_state_id'] ) && $data[ 'i_state_id' ] != '' ) {
            $query = $query->where( 'tbl_state.i_state_id', $data[ 'i_state_id' ] );
        }

        if( isset( $data[ 'i_country_id'] ) && $data[ 'i_country_id' ] != '' ) {
            $query = $query->where( 'tbl_state.i_country_id', $data[ 'i_country_id' ] );
         }

        if( isset( $data[ 'v_name'] ) && $data[ 'v_name' ] != '' ) {
            $query = $query->where( 'tbl_state.v_name', 'LIKE',  '%'. trim( $data[ 'v_name' ] ). '%' );
        }


        if( isset( $data[ 'e_status' ] ) && $data[ 'e_status' ] != '' ) {
            $query = $query->where( 'tbl_state.e_status', '=', $data[ 'e_status' ] );
        }
        /**REC_PER_PAGE difine in my Constant.php file**/
        $rec_per_page = REC_PER_PAGE;
        if( isset($data['length'])){
            if( $data['length'] == '-1') {
                $rec_per_page = '';
            } else {
                $rec_per_page = $data['length'];
            }
        }
        $sort_order = $data[ 'order' ][ '0' ][ 'dir' ];
        $order_field = $sortColumn[ $data[ 'order' ][ '0' ][ 'column' ] ];

        if( $sort_order != '' && $order_field != '' ) {
            $query = $query->orderBy( 'tbl_state.'.$order_field, $sort_order );
        }

        $record = $query->paginate($rec_per_page);
        $arrState = $record->toArray();


        $data = array();
        $i=0;   


        foreach( $arrState[ 'data' ] as $key => $val ) {
            $index = 0;
            $data[ $key ][ $index++ ] = '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"><input type="checkbox" name="id[]" value="'.$val[ 'i_state_id' ].'" class="delete_'.$val[ 'i_state_id' ].'"><span></span></label>';
            $data[ $key ][ $index++ ] = $val[ 'i_state_id' ];
            $data[ $key ][ $index++ ] = $val[ 'v_name' ];
            $data[ $key ][ $index++ ] = $val[ 'country_name' ];


            if( $val[ 'e_status' ] == 'Active' ) {
                $status = 'Active';
                $status_class = "label label-sm label-success";
            } else {
                $status = 'Inactive';
                $status_class = "label label-sm label-info";
            }

            $data[ $key ][ $index++ ] = '<a href="javascript:void(0);" data-id="'.$val['i_state_id'].'" id="change_status" rel="'.$val['e_status'].'" change-url="'.ADMIN_URL.'state/change-status" class="'.$status_class.'">'.$status.'</a>'; 

            $action = '<div class="actions"><a class="btn btn-sm blue-madison btn-outline" rel="'.$val['i_state_id'].'" href="'.ADMIN_URL.'state/edit/'.$val['i_state_id'].'"  title="Edit"><i class="fa fa-edit"> Edit</i></a></div>';


            $data[ $key ][ $index++ ] = $action;
        }

        $return_data[ 'data' ] = $data;
        $return_data[ 'recordsTotal' ] = $arrState[ 'total' ];
        $return_data[ 'recordsFiltered' ] = $arrState[ 'total' ];
        $return_data[ 'data_array' ] = $arrState[ 'data' ];
        return $return_data;
    }
}

这里的分页逻辑

$rec_per_page = REC_PER_PAGE;
    if( isset($data['length'])){
        if( $data['length'] == '-1') {
            $rec_per_page = '';
        } else {
            $rec_per_page = $data['length'];
        }
    }

由于分页未显示,可能出现的错误是什么?

2 个答案:

答案 0 :(得分:0)

对于这个问题,我可以理解Laravel Default分页 以下方式

$users = App\User::paginate(15);

在查看页面中显示分页

<div class="container">
@foreach ($users as $user)
    {{ $user->name }}
@endforeach
</div>

{{ $users->links() }}

答案 1 :(得分:0)

使用以下代码防止默认的laravel分页行为。

$(document).on('click', '.pagination li a', function (e) {
    e.preventDefault();
    if ($(this).attr('href')) {
        var queryString = '';
        var allQueries = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        if(allQueries[0].split('=').length >1){
            for (var i = 0; i < allQueries.length; i++) {
                var hash = allQueries[i].split('=');
                if (hash[0] !== 'page') {
                    queryString += '&' + hash[0] + '=' + hash[1];
                }
            }
        }
        window.location.replace($(this).attr('href') + queryString);
    }
});