Creating a button to display the following 5 arrays after using the slice method

Is it possible to create a button in my code that will display the next 5 arrays from a table with over 15 arrays, after slicing it to show only 5 of them initially?

Here is the code snippet I am working with:

          <tbody>
            <TableRow
              v-for="mandate in filteredMandates.slice(0, 5)"
              :mandate="mandate"
              :theme="getColor(mandate.asset_classification)"
              :key="mandate.isin"
            />
          </tbody>

I attempted to implement a button functionality to display the next 5 sliced items but struggled due to my limited knowledge in JavaScript. Unfortunately, my effort resulted in an error.

Answer №1

If you want to optimize your Vue script, consider setting two data attributes within the function.

data(){
 pageIndex: 0,
 pageSize: 5
}

With this setup, your code can be structured like this:

<tbody>
  <TableRow
    v-for="mandate in filteredMandates.slice(pageIndex, pageSize)"
    :mandate="mandate"
    :theme="getColor(mandate.asset_classification)"
    :key="mandate.isin"
  />
</tbody>

Your next page button should increment the page index by the page size:this.pageIndex += this.pageSize

To enhance performance and avoid repeated array slicing, you can modify your code as follows:

<tbody>
  <TableRow
    v-for="i in pageSize"
    :mandate="filteredMandates[startIndex+i-1]"
    :theme="getColor(filteredMandates[startIndex+i-1].asset_classification)"
    :key="filteredMandates[startIndex+i-1].isin"
  />
</tbody>
And similarly, your next page button would adjust the page index accordingly this.pageIndex += this.pageSize

This approach ensures that the original array is preserved without duplication, simply by updating the view start index for each new page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is it unnecessary to include a callback when using a setState function from useState as an argument in useEffect?

While working with a function from my component in the useEffect arguments, it is recommended to write a callBack so that it is memorized and used as a dependency of the useEffect. Without this, there is a warning. However, when using the setState of use ...

Can you please explain the distinction between angular.equals and _.isEqual?

Do these two options offer different levels of performance? Which one excels at conducting deep comparisons? I've encountered situations where Angular's equals function fails to detect certain differences. In addition, I've observed that th ...

Display <tr> tag when button is clicked without refreshing the page

I have a specific requirement to hide a certain tag initially. If the user clicks the forward button without selecting any radio buttons or checkboxes, the tag should be displayed and the page should not refresh. However, there seems to be an issue with ...

What is the best way to loop through a JSON property without knowing if it is an array or not?

I'm facing a challenge with an API response where the DEPARTURESEGMENT can sometimes contain only one object and other times it contains an array of objects. This variation requires different logic in my foreach-loop. Response A: { "getdeparturesr ...

There is currently no feature to integrate postal code objects within React's Stripe elements

I'm currently working on designing a custom form for Stripe. Instead of using the entire card element, I am opting to bring in individual components from Stripe elements for better styling options. My goal is to layout these individual inputs in a gri ...

Exporting a named export for every HTTP method is the way to go with NextJs

I'm currently working on a project to create an Airbnb clone using NextJs. The tutorial I'm following is using an experimental version, while I've opted for the latest version of NextJs. One aspect that's been causing confusion for me i ...

HTTP request form

I'm currently working on a form that utilizes XMLHttpRequest, and I've encountered an issue: Upon form submission, if the response is 0 (string), the message displayed in the #output section is "Something went wrong..." (which is correct); Howe ...

Ways to incorporate techniques and data for a vue.js element

As a newcomer to VueJS, I am currently working on creating a knob for my Pomodoro app to enhance my learning experience. Here is the fiddle where I started my project. While exploring different resources, I came across a knob implementation in jquery on ...

Unable to locate a declaration file for the module "../constants/links" in src/constants/links.js, as it implicitly defaults to type 'any'

Within my VueJS application, I have brought in some constant variables. <script lang="ts"> import { Component, Prop, Vue } from 'vue-property-decorator' import { MOON_HOLDINGS_LINK, TWITTER_LINK } from '../constants/links' @Comp ...

What is the best way to use scrollIntoView() to display an additional item at the top or bottom of the visible area

When implementing scrollIntoView() with navigation buttons (up and down), I aim to display two items at a time to signal to the user that there are more items to navigate. However, the first and last items should retain their default behavior so the user u ...

Utilizing JavascriptExecutor in Powershell

I've been working on a series of Powershell scripts that utilize the Selenium Webdriver. Now, I am looking to incorporate some JavaScript functionality into one of them. However, I am struggling to understand how to get the syntax right. I tried ada ...

Conceal a div once the content in a separate div has finished loading

After loading an image in slices inside a div, I want to ensure that the entire content is loaded before displaying the div. To achieve this, I am using another div as a mask while the content loads: <div id="prepage" style="position:absolute; left:0px ...

What is the best way to navigate to an active item within a list in React, especially when the list itself has overflow enabled but its parent element has overflow set to

I am facing an issue with a React component that contains numerous subcomponents. Specifically, I have a list within this component that has an overflow property set to auto. However, the parent element of this list has the property overflow: hidden; My c ...

Pass identical data to the view using both POST and GET methods in a node.js / Express.js application

At first, I attempted to utilize the router.post and router.get methods separately in my code. But then I made the decision to use router.all, and within the same function split POST and GET calls, using two res.render functions along with a common object ...

Can I obtain a dictionary containing the connections between labels and phrases from the classifier?

I've implemented a LogisticRegressionClassifier using the natural library for node: const natural = require('natural'); const classifier = new natural.LogisticRegressionClassifier(); classifier.addDocument('category1', 'sent ...

Encountering TypeScript errors when utilizing it to type-check JavaScript code that includes React components

My JavaScript code uses TypeScript for type checking with docstring type annotations. Everything was working fine until I introduced React into the mix. When I write my components as classes, like this: export default class MyComponent extends React.Compo ...

req.body is not defined or contains no data

I am facing an issue with my controllers and routers. bookController.js is functioning perfectly, but when I try to use userControllers for registration and login logic, req.body always appears empty. I tried logging the form data using console.log, but it ...

TypeError: Unable to access the 'classify' property of an object that has not been defined (please save the ml5.js model first)

In my React app, I have set up ml5.js to train a model by clicking on one button and make predictions with another. However, I encounter an error when trying to test the model for the second time: TypeError: Cannot read property 'classify' of und ...

Where does the 'Execution Context Destroyed' error originate from in my code?

Currently, I am developing a program to extract forum responses for the online University where I am employed. While I have managed to successfully navigate to the relevant pages, I encountered an issue when trying to include scraping for the list of learn ...

Using jQuery to fill input fields automatically with a mouse click rather than using the keyboard

I found a solution that works great for my needs here $("#EmailAddress").keyup(function(){ $("#Username").val(this.value); }); Even though this solution works perfectly when entering values with the keyboard, it doesn't seem to function properly ...