How to limit image downloads in datatables?

How to limit image downloads in datatables?

BrianHoardBrianHoard Posts: 3Questions: 1Answers: 0

I have PHP/MySQL and Datatables handling 1500 rows of data. Part of this data is the path to images that I display on the web page. As our number of rows increased, we started having problems with the server running out of memory. Now, we see using the browser dev tools, that even though the web page is displaying only 10 records, it is downloading all 1500 images! How can I only have datatables download the number of images being requested? Thank you for any help.

This question has an accepted answers - jump to answer

Answers

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

    Yep, for client-side processing, the full table is downloaded and cached. It sounds like you want to enable server-side processing instead - there it will only download the records you see on that current page.

    For server-side processing, enable serverSide. The protocol is discussed here. Also see examples here. If you download the DataTables repo, there are examples of the server-side scripts in /examples/server_side/scripts,

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,182Questions: 26Answers: 4,925
    edited January 2022

    I wonder if deferRender would help and only download the images when the table page is displayed. Guess it depends on whether the images are part of the ajax JSON response or if they are URLs and fetched separately. If fetched separately deferRender might help. If part of the data then serverSide is the way to go.

    Kevin

  • BrianHoardBrianHoard Posts: 3Questions: 1Answers: 0

    Thanks for your help. I see that setting serverSide to true reduces the images downloaded to only those being viewed, but this also breaks the search.

    With serverSide true, search has to be exact, and in the right order. For example, to find the string in my data: Foo bar, if I search "Foo bar", it works. But if I search "bar Foo", it fails to show any result. (Showing it in quotes here, I'm not searching using quotes).

    With serverSide false, search behaves as expected, showing the result if I search "Foo bar" or "bar Foo".

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    That's because the server-side scripts don't implement smart searching out of the box - that decision was taken as it would make the searching slower. There are some threads discussing how to implement that, such as here and here, that'll hopefully do the trick for you,

    Colin

  • BrianHoardBrianHoard Posts: 3Questions: 1Answers: 0

    Thanks again for the help. Good to know this is part of the design and not a mistake I was making. Now to dive into these links you mentioned and see if I can learn how it's done.

Sign In or Register to comment.