mvnobrega's avatar

How to make v-for display 10 items at a time

I have a questionnaire with 60 questions, and I need to display 10 questions at a time.

But, I'm having trouble making it work even though I see some references on the internet.

The way I am doing, each time I click next(), 10 items are added to the v-for, but the previous 10 items remain on the page.

I'm doing this:

<div class="test-questions comp">
                    <div class="question" v-for="q in perguntas['questions']" v-if="q.id <= perpage">
                        <div class="statement">
                            {{q.id}}. {{q.text}}
                        </div>
                        <div class="options yes-no">
                            <div class="caption option yes">Sim</div>
                            <div class="caption option no">Não</div>
                        </div>
                    </div>
                  
                </div>
                <div class="action-row">
                    <button v-if="perpage == 60" @click="salvarRelato()"><span>Publicar</span></button>
                    <button v-if="perpage < 50" @click="previous()"><span>Anterior</span></button>
                    <button v-if="perpage < 50" @click="next()"><span>Próximo</span></button>
                </div>

My data:

props: ['perguntas'],
        data(){
            return {
                perpage: 10,
            }
        },
        methods: {
            next(){
                this.perpage = this.perpage + 10;
            },
            previous(){
		this.perpage = this.perpage - 10;
            },
        }

Can you help me ?

0 likes
2 replies
piljac1's avatar
piljac1
Best Answer
Level 28

What @tray2 suggested is the ideal solution, especially for a huge amount of data. That being said, with only 60 records, it won't make much of a difference.

If you don't want to convert to multiple AJAX requests, you could simply add an offset data property and increment by 10 both the offset and your current perpage property in the next method (and decrement them by 10 in the previous method). You would also have to update your condition to work with the offset property.

Start your offset at 1 and your perpage at 10. So on page load, you'll get questions 1 to 10, then 11 to 20, then 21 to 30 and so on.

1 like

Please or to participate in this conversation.