Limit Number of Rows

Limit Number of Rows

Mr Arslan JawedMr Arslan Jawed Posts: 9Questions: 6Answers: 1

Hello,

How can I limit the number of results in server side script.

Regards

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Mr Arslan Jawed ,

    The limitation should actually be done on the client-end, by pageLength. The protocol is discussed here. The client asks for a number of records, which the server is expected to return,

    Cheers,

    Colin

  • rmeetinrmeetin Posts: 100Questions: 24Answers: 1

    Maybe I have the syntax wrong or don't understand.

    When I add pageLength: 20, in my html file it seems to have no effect. I have a table of over 1000 records and wish to limit the total number of records returned (for performance) and also by a last modified field.

    In an ordinary query I would do something like:

    select * from $table order by last_modified desc limit 20.
    

    How would I accomplish this in datatables (in the html file or in the controller file with a where is fine)?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    When I add pageLength: 20, in my html file

    pageLength goes into your DataTables initialisation code.

  • rmeetinrmeetin Posts: 100Questions: 24Answers: 1
    edited August 2019

    Here the relevant excerpt from my initialization code:

      var table = $('#example').DataTable( {
    
      /* SCROLLING */
      scrollY:"480px",
      scrollCollapse: true,
      paging: false,
      scrollX: true,
      info: true,
      dom: 'Bfrtip',
      select: true,
    
      // to limit records
      pageLength: 5,
    
      recordsTotal: 50,
    

    Neither pageLength nor recordsTotal have any impact. Returns 1000+ records.

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    Doesn't look like you have enabled serverSide processing. If you want server side paging then you need to enable server side processing:

    This adds complications to your server code since it will be responsible for searching and order also. Please read the Server Side Processing docs for details. Also see this FAQ:
    https://datatables.net/faqs/index#speed

    It is recommended to use client side processing if possible. Which means all the rows are returned but the sorting and searching are done client side.

    Kevin

This discussion has been closed.