When try server side sorting with Grid.js it does multiple calls to the server that could be avoided

huangapple go评论62阅读模式
英文:

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;link
href=&quot;https://unpkg.com/gridjs/dist/theme/mermaid.min.css&quot;
rel=&quot;stylesheet&quot;
/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;wrapper&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/gridjs/dist/gridjs.umd.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
new gridjs.Grid({
columns: [&#39;Title&#39;, &#39;Director&#39;, &#39;Producer&#39;],
sort: {
multiColumn: false,
server: {
url: (prev, columns) =&gt; {
if (!columns.length) return prev;
const col = columns[0];
const dir = col.direction === 1 ? &#39;asc&#39; : &#39;desc&#39;;
let colName = [&#39;title&#39;, &#39;director&#39;, &#39;producer&#39;][col.index];
return `${prev}&amp;order=${colName}&amp;dir=${dir}`;
}
}
},
pagination: {
limit: 3,
server: {
url: (prev, page, limit) =&gt; `${prev}?limit=${limit}&amp;offset=${page * limit}`
}
},
server: {
url: &#39;https://swapi.dev/api/films?&#39;,
then: data =&gt; data.results.map(m =&gt; [
m.title, m.director, m.producer
]),
total: data =&gt; data.count
} 
}).render(document.getElementById(&quot;wrapper&quot;));
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

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

huangapple
  • 本文由 发表于 2023年3月31日 21:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定