[英]Axios REST api client pagination
I have a problem. 我有个问题。 I have a rest api in flask application.
我在烧瓶应用程序中有一个REST API。 Application is connected to database which stores 10k users.
应用程序已连接到存储10k用户的数据库。 I need to do something like a pagination.
我需要做类似分页的事情。 I want to get for example: list of 100 users, from 1 to 100, and then get an url to next page with users from 101 to 200 etc. I made a axios request like this:
我想得到例如:100个用户的列表,从1到100,然后从101到200的用户获取到下一页的URL,等等。我发出了axios请求,如下所示:
document.getElementById('showUserInputForm').addEventListener('submit', performGetRequest1);
function performGetRequest1(){
var start = document.getElementById('starting').value;
var limit = document.getElementById('limit').value;
var resultElement = document.getElementById('getResult1');
resultElement.innerHTML = '';
axios.get('http://127.0.0.1:5000/users/page', {
params: {
start:start,
limit:limit
}
})
.then(function (response) {
resultElement.innerHTML = generateSuccessHTMLOutput(response);
})
.catch(function (error) {
resultElement.innerHTML = generateErrorHTMLOutput(error);
});
}
This code returns me json: 这段代码返回了我json:
{
"limit": 100,
"next": "users/page?start=200&limit=100",
"previous": "users/page?start=1&limit=99",
"results": [
{
{
"id": 101,
"password": "hicqjjzxftvq",
"username": "jclkoxgwgvvw"
},
{
"id": 100,
"password": "xtitexyetpwg",
"username": "uwerzuwnxkju"
},
How i can use these next/previous links to make < and > buttons with it? 我如何使用这些下一个/上一个链接来制作<和>按钮? Or any ideas how to make it?
或任何想法如何做到这一点?
The backend actually returns the endpoints you can use for the next / previous page. 后端实际上返回可用于下一页/上一页的端点。
You can do something like this: 您可以执行以下操作:
let nextUrl = null;
let previousUrl = null;
url = 'http://127.0.0.1:5000/users/page';
function performGetRequest1(url){
var start = document.getElementById('starting').value;
var limit = document.getElementById('limit').value;
var resultElement = document.getElementById('getResult1');
resultElement.innerHTML = '';
axios.get(url)
.then(function (response) {
resultElement.innerHTML = generateSuccessHTMLOutput(response);
nextUrl = 'http://127.0.0.1:5000/' + response.next;
previousUrl = 'http://127.0.0.1:5000/' + response.previous;
})
.catch(function (error) {
resultElement.innerHTML = generateErrorHTMLOutput(error);
});
}
If you run this function using either the nextUrl
or previousUrl
it should return the correct values. 如果您使用
nextUrl
或previousUrl
运行此函数,它将返回正确的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.