<script setup lang="ts"> import { onMounted, ref, reactive, computed } from 'vue' import…
- User
- Assistant
Sure! To implement pagination with a maximum of 50 items per page based on the selected model type (SD-XL or FLUX), you'll need to:
currentPage reactive variable to keep track of the current page.currentPage when the model type changes to ensure the pagination starts from the first page.Here's the updated code with these changes:
<script setup lang="ts">
import { onMounted, ref, reactive, computed, watch } from 'vue'
import { Pixihttp } from '../systems/http.ts'
const checked_type = ref('1')
const model_id = ref('')
// Using reactive to create reactive arrays
const stable_diffusion_xls = reactive<Array<any>>([])
const fluxs = reactive<Array<any>>([])
// Pagination state
const currentPage = ref(1)
const itemsPerPage = 50
// Watcher to reset currentPage when checked_type changes
watch(checked_type, () => {
currentPage.value = 1
})
// Compute the total number of pages based on the selected type
const totalPages = computed(() => {
const totalItems = checked_type.value === '1' ? stable_diffusion_xls.length : fluxs.length
return Math.ceil(totalItems / itemsPerPage)
})
// Compute the models to be displayed on the current page
const paginatedModels = computed(() => {
const models = checked_type.value === '1' ? stable_diffusion_xls : fluxs
const start = (currentPage.value - 1) * itemsPerPage
const end = start + itemsPerPage
return models.slice(start, end)
})
onMounted(async () => {
const httpdata = await Pixihttp.get_model_list()
for (let entry of httpdata.data) {
if (entry.model_category === 'stable_diffusion_xl') {
stable_diffusion_xls.push(entry)
} else if (entry.model_category === 'flux') {
fluxs.push(entry)
}
}
console.log(stable_diffusion_xls)
console.log(fluxs)
})
</script>
<template>
<div class="root">
<van-row>
<van-col span="24">
<div class="main-canvas" ref="canvas">
<van-row>
<van-col span="8"></van-col>
<van-col span="8">模型测试</van-col>
<van-col span="8"></van-col>
</van-row>
<van-row>
<div class="model_type_in">
<van-radio-group v-model="checked_type" direction="horizontal">
<van-radio name="1">SD-XL</van-radio>
<van-radio name="2">FLUX</van-radio>
</van-radio-group>
</div>
</van-row>
<van-row>
<div class="model_id_in">
<van-cell-group inset>
<van-field
v-model="model_id"
label="模型ID:"
left-icon="smile-o"
right-icon="warning-o"
placeholder="输入模型ID"
/>
</van-cell-group>
</div>
</van-row>
<van-row>
<div class="model_list_page">
<v-list lines="one" density="compact" max-height="300">
<v-list-item
v-for="item in paginatedModels"
:key="item.model_id"
:title="item.model_name"
:subtitle="'ID: ' + item.model_id"
></v-list-item>
</v-list>
<v-pagination
v-model="currentPage"
:length="totalPages"
style="margin-top: 16px;"
></v-pagination>
</div>
</van-row>
</div>
</van-col>
</van-row>
</div>
</template>
<style scoped>
.model_id_in {
margin: 10px 10px;
}
.model_type_in {
margin: 10px 40px;
}
.model_list_page {
height: 300px;
width: 100%; /* Fixed the typo from 'weight' to 'width' */
}
</style>
Pagination State and Constants:
const currentPage = ref(1)
const itemsPerPage = 50
Watcher for checked_type:
watch(checked_type, () => {
currentPage.value = 1
})
This ensures that when the user switches between SD-XL and FLUX, the pagination resets to the first page.
Compute Total Pages:
const totalPages = computed(() => {
const totalItems = checked_type.value === '1' ? stable_diffusion_xls.length : fluxs.length
return Math.ceil(totalItems / itemsPerPage)
})
Compute Paginated Models:
const paginatedModels = computed(() => {
const models = checked_type.value === '1' ? stable_diffusion_xls : fluxs
const start = (currentPage.value - 1) * itemsPerPage
const end = start + itemsPerPage
return models.slice(start, end)
})
This slices the appropriate array to return only the models for the current page.
Template Modifications:
Use paginatedModels in v-for:
<v-list-item
v-for="item in paginatedModels"
:key="item.model_id"
:title="item.model_name"
:subtitle="'ID: ' + item.model_id"
></v-list-item>
Bind v-pagination to currentPage and set length to totalPages:
<v-pagination
v-model="currentPage"
:length="totalPages"
style="margin-top: 16px;"
></v-pagination>
Ensure that the pagination component correctly reflects the number of pages and updates the currentPage when the user navigates.
Style Fix:
weight: 100%; to width: 100%; in the .model_list_page class.currentPage remains within the valid range.This implementation provides a robust pagination system that dynamically displays models based on the selected type and ensures that only 50 items are shown per page. Let me know if you need further assistance!
Not yet included in a published release.