Search code examples
jqueryajaxrhomobilerhodes

How do i make paging using jquery ajax for rhomobile app


I have a table where i need to show records in pagination manner.

Let say i have 200 records, i need to show 10 records on each page. I have used Paginate to get data from Rhom.

  MyModel.paginate(:page => 1, :per_page => 10)

So how can i use the ajax call to fetch the next page records without changing the view.

Help is appreciated.


Solution

  • You can have the ajax call to a method by sending the page number and page limit, and get the view through responce.

    You should make the view to have the exact design as you have in you original view, and after ajax success, just replace the view with the new one.

    $.get('/app/MyModel/get_new_view, 
        { pageNumber: 1,  // u need to set it dynamically
          pageLimit: 10 }
        )
        .success( function ( data ) {
    
            // Check the Responce
            if(data.length != 0) {
                $('.container').html(data);
            }
            else {
                alert('Unable to get the page');
            }
        })
        .error ( function ( data ) {
            alert('Unable to get the page');
        });
    

    Suggession

    If you want to reduce the burden of writing such code, you can simply get all the records in the view and use JqueryDataTable to render them as like you want.

    $(document).ready( function () {
         $('.youTable').dataTable({
           "aaSorting": [[ 1, "asc" ]],
           "aoColumnDefs": [{ "bSortable": false, "aTargets": [ 0,1 ] }]
         });
     });