DataTables-Keep-Conditions icon indicating copy to clipboard operation
DataTables-Keep-Conditions copied to clipboard

keep page does not work with servserside processing

Open macrozone opened this issue 9 years ago • 7 comments

Hi,

thank you for your amazing plugin, it works flawless so far.

The only thing that is not working with current datatable version (1.10.10) is keep the page with serverside processing.

it removes the page number from the hash as soon as the datable is initialized.

macrozone avatar Jan 14 '16 16:01 macrozone

What I found out is, that structureHash is called as soon as the rendering and loading is started. It then fetches data from the table, finds a page 0 and removes the page-number from the hash

macrozone avatar Jan 15 '16 13:01 macrozone

yes there is a bug with server side processing which reset page setting.

I did a quick fix by creating a function to get parameters from url :

  function hashToAjaxParams () {
      var hashParsed = KeepConditions.queryString();
      var hashValue = hashParsed[Object.keys(hashParsed)[0]];

      var paginationRegex = /p([0-9]+)/;
      var lengthRegex = /l([0-9]+)/;
      var sortingRegex = /o(a|d)([0-9]+)/;
      var searchRegex = /f(.*)/;

      var result = {
        page: null,
        length: null,
        sort: { column: null, dir: null },
        search: null
      };

      if(hashValue) {
        var parsed;
        $.each(hashValue.split(':'), function (index, value) {
          if ((parsed = value.match(paginationRegex)) !== null) {
            result.page = parsed[1];
          }
          if ((parsed = value.match(lengthRegex)) !== null) {
            result.length = parsed[1];
          }
          if ((parsed = value.match(sortingRegex)) !== null) {
            result.sort.dir = parsed[1] === 'a' ? 'asc' : 'desc';
            result.sort.column = parsed[2];
          }
          if ((parsed = value.match(searchRegex)) !== null) {
            result.search = parsed[1];
          }
        });
      }

      return result;
    }

and then merged it with parameters sent to server if they exists in url each time an ajax call is made:

  $("#tableid").DataTable({
  ajax: {
          url: "/your/url",
          data: function (d) {
            var params = hashToAjaxParams();
            if (params.sort) {
              d.order[0].column = parseInt(params.sort.column || 0);
              d.order[0].dir = params.sort.dir || 'asc';
            }
            if (params.length) {
              d.length = parseInt(params.length || 25);
            }
            if (params.page) {
              d.start = d.length * parseInt(params.page || 0);
            }
            if (params.search) {
              d.search.value = params.search || '';
            }

            return $.extend( {}, d, {
              //"extra_search": $('#extra').val()
            });
          }
        }
  }

dragouf avatar Feb 02 '16 08:02 dragouf

Hi dragouf,

thx for your help.

I tried it out but it does not work for me.

when the data function is called, the page-param is already set back to 0, so hashToAjaxParams will return a page of 0.

I tried therefore to fetch hashToAjaxParams at startup and merge these params in like you did in data. This works, it loads the correct page, but it displays the wrong page in datatables.

macrozone avatar Feb 22 '16 13:02 macrozone

Hey guys, sorry for the late reply, had some stuff going on in my personal life which caused me to take a break.

Ill look into this, and plan to have a new release with a fix for this sometime this weekend.

jhyland87 avatar Apr 01 '16 13:04 jhyland87

@dragouf I may end up using some of your code btw :)

If a user is trying to use this plugin, and defines their own ajax.data function like you did above, then that will probably cause quite a few issues.. Im not sure how to handle that one yet.

jhyland87 avatar Apr 05 '16 15:04 jhyland87

In my case I use a custom ajax-function, so yes, this would lead to issues ;-)

I also have seen, that the plugin resets the url-params before the ajax.data function is called, so the workaround above does not work for me

macrozone avatar Apr 07 '16 08:04 macrozone

Hi everyone. Any luck on getting this issue to work with server side processing? When I have a page set in the url and load that custom url page, the datatables are not initialized on the preset page in the url hash. Thanks for the great plugin, btw!

blupointmedia avatar Jul 31 '17 13:07 blupointmedia