LePhenix47 / HRnet-Data-Table_npm-package

This repository hosts a React NPM package intended to replace the jQuery-based counterpart for the 14th project of the JS-React traineeship at OpenClassrooms.com
https://www.npmjs.com/package/@lephenix47/react-datatable
1 stars 0 forks source link

[CONCEPTION] Pagination Index #6

Closed LePhenix47 closed 1 year ago

LePhenix47 commented 1 year ago

Pagination Index:

image

Composition:

They're all a <button type="button"> with the value of the index or text to go to the previous or next entry of the table wrapped inside the second column of the<tfoot>

Features:

This element should:

ex:

Imagine we have some data with 1 000 entries (data.length === 1 000), the user wants to see 50 entries
To compute all the indexes for the table we do:

1000 ÷ 50 = 20

Now imagine the user goes to the table index: 15
The body must start from:
(15 - 1) × 50 = 700

To:
(15 - 1) × 50 + 50  - 1 = 749

Is 749> 1000? → False, do not set the end index to its length - 1

It should now show in the <tbody>, the data starting at index 700 (data[700]) and finishing at index 749 (data[749])
LePhenix47 commented 1 year ago

Demo:

https://user-images.githubusercontent.com/78600723/217267270-137dc610-a290-401a-b016-980746485f0c.mp4

LePhenix47 commented 1 year ago

Steps to integrate the feature (PART 1/3):

ex: 
The user chose to go the pagination index n°5 in a table with an amount of shown entries set to 25 out of 
the total amount of entries equal to 1 000:

→ PaginationIndex = 5

→ Shown entries = 25

→ TotalEntries = 1 000

→ TotalPaginationIndexes = [TotalEntries] ÷ [ShownEntries] = 1 000 ÷ 25 = 40

→ StartIndex = ([PaginationIndex] - 1) × [ShownEntries] = 4 × 25 = 100

→ EndIndex = [StartIndex] + [ShownEntries] - 1 = 100 + 25 - 1 = 124

→ Is 124 > [TotalEntries] - 1? False

→ Reset the dataToShow array (empty it)

→ Add the values starting from the StartIndex and ending at the end index

→ Update the UI

Shares some steps with #1

LePhenix47 commented 1 year ago

Shared steps with #1 :

LePhenix47 commented 1 year ago

Steps to integrate the feature (PART 2/3):

1) table-index--1

2) table-index--2

3) table-index--3

In order to create the pagination effect on the foot of the table:

LePhenix47 commented 1 year ago

When the amount of pagination indexes <= 7:

LePhenix47 commented 1 year ago

Steps to integrate the feature (3/3):

For the buttons, we need to make a condition with the TotalPaginationIndex (or TPI for short):

LePhenix47 commented 1 year ago

Shared button steps:

LePhenix47 commented 1 year ago

Idea

For the PaginationIndex with a TotalPaginationIndex over 7, if its value is in between (case 2), then it should be working like a counter: image

image

LePhenix47 commented 1 year ago

Each case should be rendered inside a component

Case 1)

 <FirstRow TotalPagination={TPI} PaginationIndex={PI} />

Case 2)

<MiddleRow TPI={TPI} PI={PI} />

Case 3)

<LastRow TPI={TPI} PI={PI} />
LePhenix47 commented 1 year ago

Recall of how to show the table:

1) We get the start and end index: SI = (PI - 1) × SE EI = SI + SE 2) Reset the dataToShow DTS = []

3) If the array has to be sorted → sort it with sortArrayOfObjects()

4) Fill in the new values for the dataToShow array starting at SI and finishing at EI

for(let i = [SI]; i < [EI]; i++){
  const item = data[i];
  DTS.push(item);
}

5) Re-render the values inside the component

setValues( getArrayObjectValues(DTS) )
LePhenix47 commented 1 year ago

⚠ Issue ⚠

There's currently an issue with the table, let's say we have a table of 429 entries and:

And that we wanted to expand the amount of entries shown to 25:

With the current implementation, the pagination index does not change, and so we end up at the pagination index number 5 instead of number 2

The formula to fix it is this:

[Current PI] = [Previous PI] × [New TPI] ÷ [Old TPI]

ex: [Current PI] = 5 × 18 ÷ 43 = 2
LePhenix47 commented 1 year ago

💡 Idea how to implement it

An idea as to how to implement it would be to store the TPI inside an array with a state If that array exceeds a length of 2 → remove its first element

It would be called before initialisazing the starting and ending indexes ex:

const [historyPaginationsArray, setHistoryPaginationsArray] = useState([])

//We're going to use the spread operator and the normal syntax
setHistoryPaginationsArray([... historyPaginationsArray, PI])

if(historyPaginationsArray.length > 2){
  //We are going to use the function notation to remove the first element

  setHistoryPaginationsArray( ( hTPI ) => {

      //We remove the first item only
      return hTPI = hTPI.shift()
   }
  )

setStartAndEndIndex();
//...
}

And then get the old TPI using the first element of the state and the new TPI as the new one:

const oldTPI = historyPaginationsArray[0];
const newTPI = historyPaginationsArray[1];
LePhenix47 commented 1 year ago

The idea has a huge flaw → can't update the pagination index because it's a state

LePhenix47 commented 1 year ago

Update on how the [PI] works:

  1. Get the [New PI]
  2. Set the [SI] and [EI] with the [New PI]
  3. Check if the [New PI] underflows or overflows
  4. Give the indexes to the <EntriesIndex /> component
  5. Reset the [DTS] to an empty array
  6. Fill in the values of [data] in [DTS]
  7. Update the UI