英文:
When try server side sorting with Grid.js it does multiple calls to the server that could be avoided
问题
我正在使用Grid.js来访问服务器数据并执行服务器端排序(请参考Grid.js文档)。
我的问题是,当我尝试对一列进行排序时,它会对服务器发出与可排序列的数量相等的调用。
以下是一个JSFiddle上的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<link
href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css"
rel="stylesheet"
/>
</head>
<body>
<div id="wrapper"></div>
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
<script type="text/javascript">
new gridjs.Grid({
columns: ['Title', 'Director', 'Producer'],
sort: {
multiColumn: false,
server: {
url: (prev, columns) => {
if (!columns.length) return prev;
const col = columns[0];
const dir = col.direction === 1 ? 'asc' : 'desc';
let colName = ['title', 'director', 'producer'][col.index];
return `${prev}&order=${colName}&dir=${dir}`;
}
}
},
pagination: {
limit: 3,
server: {
url: (prev, page, limit) => `${prev}?limit=${limit}&offset=${page * limit}`
}
},
server: {
url: 'https://swapi.dev/api/films?',
then: data => data.results.map(m => [
m.title, m.director, m.producer
]),
total: data => data.count
}
}).render(document.getElementById("wrapper"));
</script>
</body>
</html>
我想了解为什么会这样,并如何避免这种情况。
非常感谢。
英文:
I'm using Grid.js accessing server data and performing server side sorting (this on Grid.js doc ).
My problem is that when I try to sort a column it makes a number of calls to the server equal to the number of sortable columns.
Here's an example on JSFiddle
<!DOCTYPE html>
<html lang="en">
<head>
<link
href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css"
rel="stylesheet"
/>
</head>
<body>
<div id="wrapper"></div>
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
<script type="text/javascript">
new gridjs.Grid({
columns: ['Title', 'Director', 'Producer'],
sort: {
multiColumn: false,
server: {
url: (prev, columns) => {
if (!columns.length) return prev;
const col = columns[0];
const dir = col.direction === 1 ? 'asc' : 'desc';
let colName = ['title', 'director', 'producer'][col.index];
return `${prev}&order=${colName}&dir=${dir}`;
}
}
},
pagination: {
limit: 3,
server: {
url: (prev, page, limit) => `${prev}?limit=${limit}&offset=${page * limit}`
}
},
server: {
url: 'https://swapi.dev/api/films?',
then: data => data.results.map(m => [
m.title, m.director, m.producer
]),
total: data => data.count
}
}).render(document.getElementById("wrapper"));
</script>
</body>
</html>
I want to understand why it does it and how to avoid it.
Many thanks
答案1
得分: 3
似乎是该库的一个已知问题。
错误报告:https://github.com/grid-js/gridjs/issues/1317
它仍未修复(截至2023年6月)。
似乎是“sort”操作导致了大量的API调用。
英文:
It seems it's a know bug of the library.
Bug report : https://github.com/grid-js/gridjs/issues/1317
it's still not fixed(june 2023).
It's seems that this is the sort
who make so much api calls
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论