Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/anan/pagination #25

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
37 changes: 37 additions & 0 deletions src/lib/components/Playground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import AnnoucementCard from './AnnoucementCard/AnnoucementCard.svelte';
import thumbnail from '../assets/images/thumbnail.png';
import OrganizationCard from './OrganizationCard/OrganizationCard.svelte';
import Pagination from './pagination/pagination.svelte';

modalShow.set(false);

Expand Down Expand Up @@ -80,6 +81,38 @@
'ปี 2564',
'ปีย้าวยาวววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววว'
];
let PaginationMockitem: string[] = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
'25',
'26',
'27',
'28',
'29',
'30'
];

const annoucementCard = Array(3).fill({
imageURL: thumbnail,
Expand Down Expand Up @@ -308,6 +341,10 @@
<OrganizationCard />
</section>

<section class="section">
<h2 class="font-bold text-2xl mb-4">Pagination</h2>
<Pagination Arrayitem={PaginationMockitem} />
</section>
<Footer />
</div>

Expand Down
117 changes: 117 additions & 0 deletions src/lib/components/pagination/pagination.svelte
Chulinuwu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<script lang="ts">
import { cn } from '../../utils/cn';
import Fa from 'svelte-fa';
import { faChevronLeft, faChevronRight } from '@fortawesome/free-solid-svg-icons';
import Dropdown from '../Dropdown/Dropdown.svelte';

export let Arrayitem: string[] = []; // Explicitly define the type as any[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ลบ comment นี้ออกไปมันคือ string[] ไม่ใช่ any[]

let currentPage: number | string = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ขอ 2 อันนี้เป็น type number หน่อย

let currentPage: number
let itemsPerPage: number

เดี่ยว backend จะส่งมาเป็น type number เหมือนกัน
และมันจะไม่ต้องไป ParseInt หลายๆที่
หลังจากเปลี่ยน type แล้วพยายามเอา ParseInt ออกไป

let itemsPerPage: string = '5'; // Default items per page
let totalPages: number = Math.ceil(Arrayitem.length / parseInt(itemsPerPage));
let paginatedItems: string[] = [];

const pageChoice: string[] = ['5', '10', '15'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

พวกนี้อาจจะต้องเปลี่ยนเป็น props แทนหมายถึงต้อง export ไว้ด้วยละกันเพื่อ custom ทีหลัง


function changePage(page: number | string) {
currentPage = page;
paginateItems();
}
Comment on lines +15 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

เวลา change page ขอแบบให้มีขอบเขตหน่อย

function changePage(page: number) {
  if (page < 1) {
    currentPage = 1;
  } else if (page > totalPages) {
    currentPage = totalPages;
  } else {
    currentPage = page;
  }
}


function paginateItems() {
const startIndex = (Number(currentPage) - 1) * parseInt(itemsPerPage);
const endIndex = startIndex + parseInt(itemsPerPage);
paginatedItems = Arrayitem.slice(startIndex, endIndex);
}

function getVisiblePages(totalPages: number, currentPage: number) {
let pages = [];
if (totalPages <= 7) {
pages = Array.from({ length: totalPages }, (_, i) => i + 1);
} else {
pages.push(1);
if (currentPage > 4) {
pages.push('...');
}
let startPage = Math.max(2, currentPage - 2);
let endPage = Math.min(totalPages - 1, currentPage + 2);
for (let i = startPage; i <= endPage; i++) {
pages.push(i);
}
if (currentPage < totalPages - 3) {
pages.push('...');
}
pages.push(totalPages);
}
return pages;
}

$: {
totalPages = Math.ceil(Arrayitem.length / parseInt(itemsPerPage));
paginateItems();
}

$: if (!itemsPerPage || parseInt(itemsPerPage) < 1) {
itemsPerPage = '5';
}

$: paginateItems();
</script>

<div class="flex flex-col gap-2 justify-center items-center">
<div class="flex gap-2 max-md:flex-col max-md:gap-1">
<div class="flex gap-2 max-md:gap-1">
<button
class="px-4 py-2 rounded bg-white border max-md:scale-75"
on:click={() => changePage(Number(currentPage) - 1)}
disabled={currentPage === 1}
>
<Fa icon={faChevronLeft} scale={0.75} />
</button>

<div class="gap-2 flex flex-wrap">
{#each getVisiblePages(totalPages, Number(currentPage)) as page}
{#if page === '...'}
<span class="px-4 py-2 max-md:scale-75">...</span>
{:else}
<button
class={cn(
' px-4 max-md:px-4 py-2 rounded max-md:scale-75',
currentPage === page ? 'text-white bg-sucu-pink-02 ' : 'text-black bg-white border'
)}
on:click={() => changePage(page)}
>
{page}
</button>
Comment on lines +76 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

เอาอีก if นึงมาครอบตรงนี้มันจะได้ไม่ type error หลังจากนั้น

{#if typeof page === 'number'}
         <button>some content</button>
{/if}

{/if}
{/each}
</div>

<button
class="px-4 py-2 rounded bg-white border max-md:scale-75"
on:click={() => changePage(Number(currentPage) + 1)}
disabled={Number(currentPage) === totalPages}
>
<Fa icon={faChevronRight} scale={0.75} />
</button>
</div>
<div class="flex gap-2 max-md:gap-1">
<div class="flex items-center max-md:scale-75">
<Dropdown
items={pageChoice}
bind:currentChoice={itemsPerPage}
outerClass="w-20 bg-opacity-50 "
on:change={(e) => (itemsPerPage = e.detail)}
/>
Chulinuwu marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div class="flex items-center">/ page</div>
</div>
</div>
</div>

<div class="items-list mt-5">
{#each paginatedItems as item}
<div class="item">
{item}
</div>
{/each}
</div>
Loading