显示分页列索引的逆序

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

Display Paginated column index in reverse order

问题

I'm facing a problem during the display of paginated data. Default the program will display 5 rows per page and during the column index it is displaying the row no starting from 1, and then on the second page the row no will start from 6.

page = 2:

idx: 6 | name: Tim
idx: 7 | name: John
idx: 8 | name: Doe
idx: 9 | name: John
idx: 10 | name: Doe

Code:

const names = [
    "John",
    "Doe",
    "John",
    "Doe",
    "John",
    "Tim",
    "John",
    "Doe",
    "John",
    "Doe",
];

let page = 1;
let limit = 5;
let totalCount = names.length || 0;

function pagination(array, page, limit) {
    return array.slice((page - 1) * limit, page * limit);
}

const newArray = pagination(names, page, limit);

newArray.forEach((item, index) => {
    const idx = (page - 1) * limit + (index + 1);
    console.log("idx:", idx, "|", "name:", item);
});

When I change page no to 2, then it will display starting row no 6 on the second page.

But the problem is I want to display the row indexing in reverse order something like this: the first row should display the index as 10, and on the second page, the index starts from 5. How can I achieve this and what is the formula for this?

page = 2:

idx: 5 | name: Tim
idx: 4 | name: John
idx: 3 | name: Doe
idx: 2 | name: John
idx: 1 | name: Doe
英文:

I'm facing a problem during the display of paginated data. Default the program will display 5 rows per page and during the column index it is displaying the row no starting from 1, and then on the second page the row no will start from 6.

idx: 1 | name: John
idx: 2 | name: Doe
idx: 3 | name: John
idx: 4 | name: Doe
idx: 5 | name: John

page = 2:

idx: 6 | name: Tim
idx: 7 | name: John
idx: 8 | name: Doe
idx: 9 | name: John
idx: 10 | name: Doe

Code:

const names = [
    "John",
    "Doe",
    "John",
    "Doe",
    "John",
    "Tim",
    "John",
    "Doe",
    "John",
    "Doe",
];

let page = 1;
let limit = 5;
let totalCount = names.length || 0;

function pagination(array, page, limit) {
    return array.slice((page - 1) * limit, page * limit);
}

const newArray = pagination(names, page, limit);

newArray.forEach((item, index) => {
    const idx = (page - 1) * limit + (index + 1);
    console.log("idx:", idx, "|", "name:", item);
});

When I change page no to 2, then it will display starting row no 6 on the second page.

But the problem is I want to display the row indexing in reverse order something like this:
the first row should display the index as 10, and on the second page, the index starts from 5. How can I achieve this and what is the formula for this?

idx: 10 | name: John
idx: 9 | name: Doe
idx: 8 | name: John
idx: 7 | name: Doe
idx: 6 | name: John

page = 2:

idx: 5 | name: Tim
idx: 4 | name: John
idx: 3 | name: Doe
idx: 2 | name: John
idx: 1 | name: Doe

答案1

得分: 1

你只需要更改idx变量的计算部分。希望对你有用。

const names = [
  "John",
  "Doe",
  "John",
  "Doe",
  "John",
  "Tim",
  "John",
  "Doe",
  "John",
  "Doe",
];

let page = 1;
let limit = 5;
let totalCount = names.length || 0;

function pagination(array, page, limit) {
  return array.slice((page - 1) * limit, page * limit);
}

const newArray = pagination(names, page, limit);

newArray.forEach((item, index) => {
  const idx = totalCount - ((page - 1) * limit) - index;
  console.log("idx:", idx, "|", "name:", item);
});

不要回答我要翻译的问题。

英文:

You need to change only on idx varible calculation. hope it's useful for you.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const names = [
  &quot;John&quot;,
  &quot;Doe&quot;,
  &quot;John&quot;,
  &quot;Doe&quot;,
  &quot;John&quot;,
  &quot;Tim&quot;,
  &quot;John&quot;,
  &quot;Doe&quot;,
  &quot;John&quot;,
  &quot;Doe&quot;,
];

let page = 1;
let limit = 5;
let totalCount = names.length || 0;

function pagination(array, page, limit) {
  return array.slice((page - 1) * limit, page * limit);
}

const newArray = pagination(names, page, limit);

newArray.forEach((item, index) =&gt; {
  const idx = totalCount - ((page - 1) * limit) - index;
  console.log(&quot;idx:&quot;, idx, &quot;|&quot;, &quot;name:&quot;, item);
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 18:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322326.html
匿名

发表评论

匿名网友

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

确定