Get page sent to server

Get page sent to server

krosoftkrosoft Posts: 8Questions: 0Answers: 0

Hello all
how I can get page# when user clicked on pagination button?
Thank you.

Replies

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Please?

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,004

    You can use page() to get the page number. The first page will be 0.

    Here is an example:

      $('#example').on( 'draw.dt', function () {
        console.log(table.page());
    });
    

    Kevin

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Thank you
    I try that:

      $('#example').on( 'draw.dt', function () {
        var table = $('#example').DataTable();
        console.log(table.page());
     });
    

    and it display 0 which is OK but then I click on 1 (on the pagination) and nothing happened.
    What I need is to somehow "know" on which page user clicked so I can send that to server.
    Thank you for any help.

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,004

    It works here for each page:
    http://live.datatables.net/dohemisu/1/edit

    Kevin

  • kthorngrenkthorngren Posts: 21,591Questions: 26Answers: 5,004

    Your subject indicates you are using server side processing. I had to change the function a bit to work in a server side example. Take a look here:
    http://live.datatables.net/suculuma/1/edit

      $('#example').on( 'draw.dt', function () {
        api = table.api();
        console.log(api.page());
      });
    

    Kevin

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Awesome, thank you so much.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    is serverSide set to true? if so, dataTables already send enough information for you to calculate the page (start and length). if start = 0 at any time, you are on page 1.
    If start = 5 and length = 5 (length as in page length or offset) you are on page 2

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Hello
    yes, "serverSide"is set to true.
    Your info looks interesting - do you have sample code, please?

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    if you do not run any interference, this link shows what is sent to the server and what it expects back https://datatables.net/manual/server-side

    I modified @kthorngren example by adding console logging. Just make sure your console is open or that JSBin's console is open when you run it so you can explore what is sent and what is received

    The data there is for an array of arrays.

    If your data is an array of objects, it will be slightly different.

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Hello @bindrid, thank you so much - do you have a link of your example?
    I checked this one: http://live.datatables.net/suculuma/1/edit
    and when I open console, I see only current page.
    Thank you for your help.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Sorry about that, try this one http://live.datatables.net/kevahiho/1/edit

    Most my server side work is done in c#. Here is the server side of the objects I deserialize the request into

    // represents what is sent from the client
    public class DataTableParameter
    {
        public int draw { get; set; }
        public int length { get; set; }
        public int start { get; set; }
        public List<columm> columns { get; set; }
    }
    public struct columm
    {
        public string data;
        public string name;
        public Boolean searchable;
        public Boolean orderable;
        public searchValue Search;
    }
    public struct searchValue
    {
        public string value;
        public Boolean regex;
    }
    
    

    And based on the data used in most datatable examples, this is the response objects

    // Single datatable row  
    public struct dtData
    {
        public string name;
        public string position;
        public Int32 salary;
        public string start_date;
        public string office;
        public string extn;
    }
     
    // So my web service returns a serialize version of this
    public struct DataTableResponse
    {
        public int draw;
        public int recordsTotal;
        public int recordsFiltered;
        public List<dtData> data;
    }
    
    
  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    Thank you so much, now I see what you mean.
    Btw. - what server you use?

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    My company uses IIS and our sites are a combination of VB.NET, C#, MVC for the back end and front end is combination of bootstrap/jquery and knockout js. and any number of plugins as we see fit.

  • krosoftkrosoft Posts: 8Questions: 0Answers: 0

    OK thank you.

This discussion has been closed.