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

How do i asynchronous in post request. #132

Closed
sforest1975 opened this issue Sep 30, 2022 · 1 comment
Closed

How do i asynchronous in post request. #132

sforest1975 opened this issue Sep 30, 2022 · 1 comment

Comments

@sforest1975
Copy link

I want to send POST request in asynchronous mode.
data should retrive each page when click pagination.

totalNumber should send by the server.
but, totalNumber is only accept dataSource is URL.
but, URL mode is only GET.
If I want to send POST, must write by function

$(...').pagination({
dataSource: (done) => {
return done($.post(
'http://www.com/getpage/1', // retrive data only one page.
$(form).serialize());
},
totalNumber: () => {
return $.post(
'http://www.com/getTotal', // retrive total number of result.
$(form).serialize());
},
callback: (d, p) {
... // display process
}
});

I am not sure difference between 'totalNumber' and 'totalNumberLocator'.

simple imagination sample code.
$('...').pagination({
dataSource: (done) => { // <- if use POST must written by function().
return done([1, 2]); // sample. POST returns [1,2]. total records count is 100.
},
totalNumber: 100, // <- if i use dataSource in function, 100 is ignored.
totalNumberLoator: 100, // <- this ignore too.
callback: (d, p) {
... // display process
}
});

if i want to send totalNumber, should dataSource in URL.
but, cannot send POST by URL.

$('...').pagination({
dataSource: 'http://xxx.com/yyy/', <- I cannot send POST by URL.
totalNumber: 100, // <- totalNumber works.
callback: (d, p) {
... // display process
}
});

I have no idea to send POST request in asynchrounous mode.
I cannot send total data to browser, because too much data.

@superRaytin
Copy link
Owner

superRaytin commented Nov 28, 2022

I believe ajax param is what you want: https://pagination.js.org/docs/index.html#ajax

totalNumberLocator is used to retrive total size from the response of remote request, below is an example:

Assume that your response's structrue is:

{
  success: true,
  results: {
    list: [{...}],
    totalNumber: 100,
  }
}

Then you can invoke pagination like this:

$('#container').pagination({
    dataSource: 'http://www.com/getpage',
    locator: 'results.list',
    ajax: {
      type: 'post',
    },
    pageSize: 5,
    totalNumberLocator: 'results.totalNumber',
    callback: function(data, pagination) {
        // template method of yourself
        var html = template(data);
        dataContainer.html(html);
    }
})

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

2 participants