Controlling itemsPerPage (length, pageLength) in AJAX request

Controlling itemsPerPage (length, pageLength) in AJAX request

tacman1123tacman1123 Posts: 198Questions: 46Answers: 1
edited December 2019 in Free community support

I need to be able to control the itemsPerPage (aka the 'length' parameter) in the ajax request, and/or only have the 'start' parameter be on a page break. This is when using scroller.

        $('#example-table').DataTable({
            ajax: this.apiRequest({
                url: this.url
            }),
            columns: this.columns,
            serverSide: true,
            processing: true,
            scrollY: '400px',
            // displayLength: 17, // not sure how to adjust the 'length' sent to the server
            // pageLength: 14,
            dom: 'ift',
            scroller: {}

Is there any way to do this? I can't figure out why datatables chooses the values it does anyway, I'm monitoring the request (by setting ajax to a function), and the records per page (length) it requests seem to be calculated internally (since they're different for each table).

I've played around with displayLength and pageLength, but they don't seem have an much of an effect. In particular, pageLength seems to be ignored with an AJAX call, but that'd be the most natural place to set it. However, it wouldn't be enough, because the start value changes while scrolling, but not to a full new page.

That is, if my server returns 30 items per page, and I only have control of the page number (not the starting element), then I don't want a datatable request of start=15, length=30, since I have to convert this to a page. I know I can implement a pipeline and make multiple requests, but this seems like a lot of work, when really I just want control over the page length. (and ideally the start parameter).

Is there an option to see the ajax length parameter?

Thanks.

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I think you are looking for the scroller.displayBuffer option.

    Kevin

  • tacman1123tacman1123 Posts: 198Questions: 46Answers: 1

    Thanks, that's in the right direction, but not exactly what I need.

    The number of rows requested (the "length") and start come from the calculations based on what's in the viewport and some buffer configuration, including the displayBuffer. Obviously datatables keeps these rows in a cache somewhere, what I want to do is force the refresh to be on page boundaries.

    I'm trying to tweak rowHight and scrollY to see if I can force a page size, but not having much luck with that.

This discussion has been closed.