mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Add pagination to my dex
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<script lang="ts">
|
||||
export let currentPage: number;
|
||||
export let itemsPerPage: number;
|
||||
export let totalPages: number;
|
||||
|
||||
function nextPage() {
|
||||
currentPage = Math.min(currentPage + 1, totalPages);
|
||||
}
|
||||
|
||||
function previousPage() {
|
||||
currentPage = Math.max(currentPage - 1, 1);
|
||||
}
|
||||
|
||||
function setItemsPerPage(event: any) {
|
||||
itemsPerPage = parseInt(event.target.value, 10);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<button on:click={previousPage} disabled={currentPage === 1}>Previous</button>
|
||||
<button on:click={nextPage} disabled={currentPage === totalPages}>Next</button>
|
||||
|
||||
<label for="itemsPerPage">Items per page:</label>
|
||||
<select id="itemsPerPage" on:change={setItemsPerPage}>
|
||||
<option value="20">20</option>
|
||||
<option value="50">50</option>
|
||||
<option value="100">100</option>
|
||||
</select>
|
||||
</div>
|
||||
@@ -0,0 +1,45 @@
|
||||
import PokedexEntryModel, { type PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
import CatchRecordModel, { type CatchRecord } from '$lib/models/CatchRecord';
|
||||
import { CombinedData } from '$lib/models/CombinedData';
|
||||
|
||||
class CombinedDataRepository {
|
||||
async findCombinedData(page: number = 1, limit: number = 20): Promise<CombinedData[]> {
|
||||
const skip = (page - 1) * limit;
|
||||
const combinedData: CombinedData[] = await PokedexEntryModel.aggregate([
|
||||
{
|
||||
$lookup: {
|
||||
from: 'catchrecords',
|
||||
localField: '_id',
|
||||
foreignField: 'pokedexEntryId',
|
||||
as: 'catchRecord'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$catchRecord',
|
||||
preserveNullAndEmptyArrays: true
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
'boxPlacement.box': 1,
|
||||
'boxPlacement.row': 1,
|
||||
'boxPlacement.column': 1
|
||||
}
|
||||
},
|
||||
{ $skip: skip },
|
||||
{ $limit: limit }
|
||||
]).exec();
|
||||
|
||||
return combinedData.map((data) => ({
|
||||
pokedexEntry: data as PokedexEntry,
|
||||
catchRecord: data.catchRecord as CatchRecord
|
||||
}));
|
||||
}
|
||||
|
||||
async countCombinedData(): Promise<number> {
|
||||
return PokedexEntryModel.countDocuments().exec();
|
||||
}
|
||||
}
|
||||
|
||||
export default CombinedDataRepository;
|
||||
Reference in New Issue
Block a user