How to Limit the Number of Rows in a Vue.js Bootstrap Table

Is there a way to display only the first row of a Bootstrap table initially, and then reveal the rest of the rows using a checkbox? Keep in mind that the Bootstrap table and checkbox are located in different components.

<b-table hover small responsive bordered stacked
         :fields="fields"
         :items="items">
</b-table>

Check out the HTML structure of the table here: https://i.sstatic.net/pgn8v.png

Visual representation of the desired functionality:

Initial view: https://i.sstatic.net/fElhK.png

After clicking on the checkbox:

https://i.sstatic.net/ilnJ9.png

Answer №1

Here is the structure of your code:

<template>
  <section>
    <b-table
      striped
      hover
      responsive
      bordered
      :fields="fields"
      :items="showAllRows ? items : [items[0]]"
    >
    </b-table>

    <b-form-checkbox v-model="showAllRows">Display all rows</b-form-checkbox>
  </section>
</template>

<script>
import { BTable, BFormCheckbox } from "bootstrap-vue";
export default {
  components: [BTable, BFormCheckbox],
  data: () => ({
    fields: ["title", "price"],
    items: [
      { title: "book1", price: 10 },
      { title: "book2", price: 15 },
    ],
    showAllRows: true,
  }),
};
</script>

I have tested it and it is functioning perfectly.

Answer №2

<template>
  <table>
    <thead>
      <tr>
        <th>Sample 1</th>
        <th>Sample 2</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in limitedItems" :key="index">
        <td>{{ item.Sample1}}</td>
      </tr>
    </tbody>
  </table>
</template>


<script>
export default {
  data() {
    return {
      items: [
        { column1: 'Data 1', column2: 'Data 2' },
      ],
      maxRows: 10 
    };
  },
  computed: {
    LimitItems() {
      return this.items.slice(0, this.maxRows);
    }
  }
};
</script>

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

Complete a form when a link or button on the webpage is clicked

I have a variety of links and buttons on my webpage, and they are generated dynamically. I am unable to assign an onclick event to each one individually. My goal is to submit form data to the next page whenever any link or button on the page is clicked. ...

After submitting the axios method, the page will automatically redirect upon a successful result

In my Laravel app, I am using Axios to submit a form with the following script: <script> export default { props: [ 'route' ], data: function () { return { mobile: '' } }, methods: { submitMobile(){ ...

The Vimeo player JavaScript API is experiencing issues on iOS devices

I am facing an issue where the API for playing a video only works on iOS after the play button is clicked in the player. However, it works fine on desktop and Chrome for Android. http://codepen.io/bdougherty/pen/JgDfm $(function() { var iframe = $(&a ...

Can you please explain how to switch the focused slide by clicking on a specific slide in swiper.js?

When using swiper in centeredSlides mode with the loop option set to true, I want a clicked slide to become the centered slide in the swiper carousel. Swiper provides me with the index of the clicked slide, but how can I use it to change the centered slide ...

Choose the appropriate data type for the class variable (for example, fArr = Uint32Array)

const functionArray: Function = Uint32Array; new fArr(5); The code snippet above is functioning properly. However, TypeScript is throwing a TS2351 error: "This expression is not constructable. Type 'Function' has no construct signatures". I wo ...

Easily conceal and reveal elements using Svelte in a straightforward manner

I'm looking for a straightforward method to hide and show an element using a button in Svelte. Can someone guide me on how to achieve this? Additionally, I'm curious if it's easier to accomplish this task with vanilla JavaScript. ...

What do you think of the unique JSON syntax found in the React-Redux-Firebase documentation? Valid or not?

The React-Redux-Firebase documentation showcases a sample code snippet. import { compose } from 'redux' import { connect } from 'react-redux' import { firebaseConnect, populate } from 'react-redux-firebase' const populates = ...

Preserving the background image on an html canvas along with the drawing on the canvas

Can users save both their drawings and the background image after completing them? var canvas = document.getElementById("canvas"); // This element is from the HTML var context = canvas.getContext("2d"); // Retrieve the canvas context canvas.style.ba ...

Setting up a new folder in the internal storage within a React Native Expo environment

In my React Native Expo project, I am utilizing two functions to store data in a JSON file and then save the file to internal storage. However, the code currently asks for permission to store inside a chosen folder, but does not create the "ProjectName" fo ...

Issue with modal rendering in Bootstrap4 when the body is zoomed in

I am encountering an issue with a Bootstrap html page where the body is zoomed in at 90%. The Bootstrap modal is displaying extra spaces on the right and bottom. To showcase this problem, I have created a simple html page with the modal and body set to 90% ...

Changing dimensions of cube on stable base

I'm currently working on a project involving a dynamic cube that can be scaled in real time by adjusting its mesh. However, I'm facing a challenge in keeping the cube fixed to the floor while changing its height. Here's a snippet of the code ...

JSON.Parse function does not return a valid value

I'm currently developing a polling system. Once a user submits their answer choice, I expect to receive JSON data containing all the answers for display purposes. Upon submitting the AJAX form, the JSON response is as follows: [{"answer_1":0,"answer ...

Load an image dynamically within a JavaScript function

I'm currently utilizing a Javascript photo viewer plugin (found here: http://www.photoswipe.com/). My goal is to dynamically update the photos connected to the plugin as the user swipes through different images. To achieve this, I am using a Javascri ...

Comparing #id_gender against assigning $('#id_gender') to the variable gender

Users are required to specify their gender. HTML <form ...> <select id="id_gender" name="gender" onchange="hideMaidenName(this.value)"> <option value="M">Male</option> <option value="F">Female</option> <option value ...

The content within the tab is currently being loaded

Incorporating jQuery UI tabs into my webpage, I encounter a delay of 5-6 seconds when clicking on a tab as it triggers an ajax call for preparing and displaying content. The screen stays idle during this time, leading to user confusion. I am seeking a sol ...

Creating a login screen without using App.vue components requires a different approach and can be achieved through alternative methods

I have been working on a Single Page Application (SPA) that features a login screen and other views. One issue I encountered was that the login screen view was appearing in the Navigation bar when it shouldn't be there. To solve this, I implemented Ro ...

VueJS: Encounter a hiccup while compiling for production

Having encountered issues while trying to build a production version of my code that previously worked without any problems. Upon running vue build on my main.js file, the following errors are provided: \ Building for production...Browserslist: cani ...

Is there a method to accurately detect the rendering of individual elements within a React Component, rather than the entire component itself?

While it's known that Components rendering can be detected through React's Developer Tool, I came across other methods in this source. However, what I'm specifically interested in is detecting the rendering of individual elements within a Co ...

Surprising discovery of the reserved term 'await'

function retrieveUsers() { setTimeout(() => { displayLoadingMessage(); const response = fetch("https://reqres.in/api/users?page=1"); let userData = (await response.json()).data; storeAllUserDa ...

The JavascriptExecutor is unable to access the 'removeAttribute' property of a null object

While utilizing Javascript executor to remove the readonly attribute, I encountered an error message: Cannot read property 'removeAttribute' of null. I came across various discussions where users suggested that removing AdBlock from Chrome solve ...