Issue with BootstrapVue b-table: selected rows are not saved when moving between pages with b-pagination

I implemented a b-table along with b-pagination in my Vue template:

<b-table
  bordered
  hover
  responsive
  :items="items"
  :fields="fields"
  :sort-by.sync="sortBy"
  :sort-desc.sync="sortDesc"
  sort-icon-left
  :filter="searchFilter"
  @filtered="onFiltered"
  :filter-included-fields="filterOn"
  :per-page="perPage"
  :current-page="currentPage"
  class="table1"
  ref="table1"
  selectable
  @row-selected="selectedRow"
  :tbody-tr-class="styleRow"
>
  <template #cell(selected)="{ rowSelected }">
      <template v-if="rowSelected">
          <span class="sr-only">Selected</span>
      </template>
      <template v-else>
          <span class="sr-only">Not selected</span>
      </template>
  </template>
</b-table>

<b-pagination
  v-model="currentPage"
  :total-rows="totalRows"
  :per-page="perPage"
  align="center"
  limit="15"
></b-pagination>

I have set up functions in my Vue app to handle row selection by the user:

export default {
  data() {
    return {
      items: [],
      sortBy: 'build',
      sortDesc: true,
      searchFilter: '',
      perPage: 11,
      currentPage: 10,
      totalRows: 1,
      fields: [],
      defaultFields: [],
      fieldNames: [],
      filterOn: [],
      selectedInfo: { item: {}, value: {}, field: {} },
      selected: [],
    };
  },
  methods: {
    styleRow(item) {
      if (item.selected) {
        return ['b-table-row-selected', 'table-secondary'];
      }
      return [];
    },
    selectedRow(items) {
      this.selected = items;
    }
  },
};

The issue arises when the user selects rows on one page, navigates to another, and then returns to the initial page only to find the selections erased. How can I address this issue? Is there a way to retain selected rows across pagination?

Answer №1

According to the BootstrapVue documentation, if you sort, filter, or paginate the table, the active selection will be cleared and the row-selected event will emit an empty array ([]).

My recommendation is to store the selected rows in a separate variable (e.g., selectedList: []) when the row-selected event is triggered. This way, it won't emit an empty array when pagination is triggered. Then, add the selectedList to the refreshed Event, so that when you navigate to the next or previous page, the selectedList variable will store all the rows you've selected.

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

infinite loop issue with beforeRouteEnter

I'm experiencing an issue with Vue Router. The scenario is to load / initially, retrieve data from there, and then navigate to the /chat/:chatId route to display the interface using the :chatId parameter obtained from an API. The routes configured a ...

Leveraging Variables within my .env Configuration

Does anyone have suggestions on how to set variables in my environment files? Website_Base_URL=https://${websiteId}.dev.net/api In the code, I have: websiteId = 55; and I would like to use config.get('Website_Base_URL'); to retrieve the compl ...

Examples of implementing Django Template styling with Bootstrap through code snippets

I am seeking a method to incorporate variables into the styling of django templates using Bootstrap. <div class="card mb-2 text-white bg-info"> The specific part for each card will vary accordingly. Is there a similar approach that I can ...

Utilize AJAX to parse an HTML Table retrieved from an ASP page and extract targeted cell data

Within our work intranet, there exists an ASP page that retrieves data from a variety of sensors. This data is organized in a table format with multiple rows and columns. It struck me as interesting to showcase some of these outputs on our department&apos ...

Sending Data Back to Main Function Using a Callback

When I try to call the emailExists() function, it should ideally return true or false. However, instead of that expected behavior, I am encountering this error: TypeError: cb is not a function Since asynchronous operations and callbacks are relatively ne ...

Discover the Location and Sign Up for Angular2+ Service

I'm currently using the Google Maps API to retrieve a user's geoLocation data, including latitude and longitude. My goal is to pass this information to a web API service in order to receive JSON output of surrounding addresses. I have implemented ...

Trapped in the clutches of the 'Access-Control-Allow-Origin' snag in our Node.js application

Our nodeJS application is deployed on AWS Lambda, and we are encountering an authentication issue with CORS policy when trying to make a request. The error in the console states: Access to XMLHttpRequest at 'https://vklut41ib9.execute-api.ap-south-1 ...

The local copy of Jquery.min.js is failing to trigger the window load function

Recently, I encountered an issue with the focus of tabs while working with jQuery. Initially, everything was fine when I included: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> in my JSP file. The wi ...

Unable to retrieve data for the initial keystroke in jQuery autocomplete

I'm currently working on setting up jQuery autocomplete with a specific method involving a separate source function and an intermediary variable for data storage. My main goal right now is to successfully pass the data to the source part of the autoCo ...

How can I invoke a PHP script using AJAX?

I am currently working on implementing a feature that involves multiple tables where users can move table rows between the tables. I want to use ajax to call a php script that will update the database with the new values, specifically assigning a new paren ...

Tips for adjusting image and div sizes dynamically according to the window size

In my quest, the end goal is to craft a simplistic gallery that closely resembles this particular example: EXAMPLE: This sample gallery is created using Flash technology, but I aim to develop mine utilizing HTML, CSS & Javascript to ensure compatibil ...

Update the link by simply clicking on a div tag

I am currently working on a PHP page and my goal is to add the extension ?id=variable_value to its URL when I click on a specific div. However, whenever I try to do this, it shows me an error message stating that the URL with the extension is undefined. B ...

Unable to transfer files, encountering issues with PHP and AngularJS integration

I am currently working on uploading files using both AngularJS and PHP. For the AngularJS part, I am utilizing angular-file-upload from https://github.com/danialfarid/angular-file-upload. I suspect that the issue lies in my PHP code, as it fails to move ...

Bring in a node module into a Vue.js CLI session

I recently set up a fresh Vue application by running vue init webpack sample-app. My goal is to incorporate the module found at this link () into my app.vue and display it correctly. Could someone guide me on the proper method to import this module into ...

Issue with Angular App: Bootstrap navbar not displaying on smaller screens

I am working on an Angular app (using Angular 11.2.4 with Bootstrap 4.5.3) and facing an issue where the navbar is not rendering correctly on screens smaller than ~580 pixels wide. Even when I click the toggler to 'expand' the collapse region, n ...

Sending a null value through AJAX to a controller

I've been working on adjusting my save routine to pass a null value to id_cellcarrier in my codeigniter controller. The code snippet below showcases what I've attempted so far, but it doesn't seem to be functioning correctly. I'm omitti ...

Issue encountered while constructing an array within the component data following the 'created' event

It's been a while since I've dealt with Vue.Js, so my memory is a bit fuzzy. I'm facing an issue where I can't seem to modify a data array within a function that is called in the created hook. My goal is to create a multidimensional ar ...

What steps can I take to address the problem of my undefined .length value?

I encountered a length error while executing line 2 in this code snippet. index.js:10 Uncaught TypeError: Cannot read property 'length' of undefined" Sample Code: <div class="formCustomerName"> <label>Name:</label> ...

Adding a personalized service into a separate service within Angular 2

I am having trouble injecting my own service into another service. While I can inject standard Angular services like Http without any issues, attempting to inject custom services results in an exception. For example, here is how MyService is set up: impo ...