Welcome Guest, Not a member yet? Register   Sign In
AJAX Pagination
#1

(This post was last modified: 07-15-2020, 01:18 PM by hobbyci.)

Hello,



I am using CI 4 since a couple of weeks and play around to learn the basics.





I wrote a guestbook and I fetch the entries via AJAX and this works wonderful.



Now what I want to do is to implement a pagination also via AJAX.



PHP Code:
public function create()
    {  
        
if( $this->request->isAJAX() && !empty($this->request->getVar()) )
        {
            return $this->setResponseFormat('json')->respond(
                                        [
                                            'success' => true,
                                            'data' => $this->db->table('gb')->insert($this->request->getVar(NULL,FILTER_SANITIZE_STRING)),
                                            'msg' => 'Thanks you.' 
                                        ]);
        }
        else
        {
            return index();
        }
    






PHP Code:
public function fetch()
    {
        if( $this->request->isAJAX() )
        {   
            $pager 
= \Config\Services::pager();
            $contact_model = new ContactModel();
            return json_encode(array( 'entries' => $contact_model->orderBy('id''desc')->paginate($this->entries_per_page),
                                    'pager' => ...//what to put here to get the pagination links?
                                    
                               );
        }
        else
        {
            return json_encode$this->_fetch_error() );
        }
    


My Javascript looks like this:



Code:
function get_comment_html(data)
{
    var html_string = '<div class="panel panel-default">';
   
    if( isEmpty(data))
    {
            html_string += '<div class="panel-heading">Systemmessage</div>' +
                            '<div class="panel-body">No entries available.</div>' +
                            '<div class="panel-footer" align="right"></div>';
    }
    else
    {
            $.each( ($.parseJSON(data)).entries, function (i, obj) {
                    html_string += '<div class="panel-heading">By <b>' + obj.name + '</b> on <i>' + obj.created_at + '</i></div>';
                    html_string += '<div class="panel-body">' + XBBCODE.process({
                                                                      text: obj.message,
                                                                      removeMisalignedTags: false,
                                                                      addInLineBreaks: true
                                                                    }).html + '</div>';
                    html_string += '<div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="id.......">Reply</button></div>';
                });   
      
    }
    html_string += '</div>';
   
    // HERE I WANT SOMETHING LIKE FOLLOWING


        $.each( ($.parseJSON(data)).pager, function (i, obj) {
                 html_string += 'CREATE MY PAGINATION LINKS';
         });

return html_string;
}

          





I have tried a couple of things but


Code:
($.parseJSON(data)).pager
is always empty.

Can someone help me? Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB