Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep page does not work with servserside processing #1

Open
macrozone opened this issue Jan 14, 2016 · 7 comments
Open

keep page does not work with servserside processing #1

macrozone opened this issue Jan 14, 2016 · 7 comments

Comments

@macrozone
Copy link

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
Copy link
Author

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

@dragouf
Copy link

dragouf commented Feb 2, 2016

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()
            });
          }
        }
  }

@macrozone
Copy link
Author

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.

@jhyland87
Copy link
Owner

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
Copy link
Owner

@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.

@macrozone
Copy link
Author

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

@blupointmedia
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants