简体   繁体   中英

Laravel 5.5 Pagination not working

In controller data comes from ajax and searching Working but pagination not working.I'm using Laravel 5.5. The problem starts when I'm displaying the results

But when I tried to show the pagination on the view page Pagination not working

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

Pagination logic here

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

What could the possible error be since the pagination does not show?

For this question I can understand Laravel Default pagination Which is following manner

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

Display pagination in View page

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

{{ $users->links() }}

Prevent default laravel pagination behaviour using the following code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM