Vue.js table columns not displaying properly when using Hide/Show checkboxes

Recently, I developed a vue.js bootstrap table to load data from local JSON files. My current challenge is implementing show/hide columns using checkboxes. While I have solved most of the issue, there's one lingering problem: when I hide a column and then try to show it again by clicking on the same checkbox, the table order gets disrupted (the column moves to the last position).

To give you an idea of my progress, here is how the app looks now: https://i.sstatic.net/Sqk62.jpg. You can also view the code on CodePen: https://codepen.io/frane_caleta/pen/KKPMKrL. However, please note that loading it without the JSON file won't work. Here's an example of the JSON data structure: https://i.sstatic.net/fgh7o.jpg.

This is my first time asking a question here, so if you require more information to help me solve this problem, please don't hesitate to ask :)

<b-form-group label="Hide columns: ">
  <b-form-checkbox-group id="checkbox-group-1" v-model="selected" :options="fields" name="flavour-1">
  </b-form-checkbox-group>
</b-form-group>

//my table
<b-table id="myTable" 
         :small="small" 
         :bordered="bordered"
         hover head-variant="dark"  
         stacked="md" 
         :items="cptItems" 
         :fields="selected" 
         :current-page="currentPage"
         :per-page="perPage" 
         :filter="filter" 
         :sort-by.sync="sortBy" 
         :sort-desc.sync="sortDesc"
         @filtered="onFiltered"
         :tbody-tr-class="rowClass"
          v-if="selected.length > 0">
</b-table>
//Javascript file
function initializeVue() {
  return new Vue({
        el: '#app',
        data() {
          return {
            items: data.logdatas,
            selected: [],
            fields: [{
                  text: 'Origin',
                  value: {
                    key: 'origin',
                    label: 'Origin',
                    sortable: true,
                    class: 'text-center',
                    index: 0
                  }
                },
                {
                  text: 'Timestamp',
                  value: {
                    key: 'timeStamp',
                    label: 'Timestamp',
                    sortable: true,
                    class: 'text-center',
                    index: 1
                  }
                },
                {
                  text: 'Level',
                  value: {
                    key: 'level',
                    label: 'Level',
                    sortable: true,
                    class: 'text-center',
                    index: 2
                  }
                }, ...there are 4 more fields like this...

                //my method for creating the checkboxes
                created() {
                  this.selected = this.fields.map(field => field.value);
                }

Answer №1

The issue lies with the selected data. The b-checkbox-group :selection displays items in the order of selection.

Check out this example for reference.

The b-table :fields shows columns based on the item order.

It is recommended to create a static list of fields and filter them by selection.

// Define data or property
let columnNames = ["one", "two", "three", "infinity", "pi"];

// Specify data
let selected = []

// Determine computed // can be optimized 
let activeFields = columNames.filter(name => selected.includes(name))
// More like this
export default {

  data(){
    return {
      selected: [],
      columnNames: ['name1', 'name2']
  },
  computed(){
    activeColumns(){

      return this.columnNames.filter(this.selected.includes) || []
  }
}

const app = new Vue({
  data(){
    return {
      currentPage: 0,
      perPage: 10,
      fields: ['age', 'first_name', 'last_name'],
      //selected: [],
      selected: ['age', 'first_name', 'last_name'],
      items: [
          { age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          { age: 89, first_name: 'Geneva', last_name: 'Wilson' },
          { age: 38, first_name: 'Jami', last_name: 'Carney' }
        ]
    }
  },
  computed: {
    activeFields(){
      return this.fields.filter(name => this.selected.includes(name))
    }
  }
}).$mount("#app");
<!-- Add this to <head> -->

<!-- Load required Bootstrap and BootstrapVue CSS -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" />

<!-- Load polyfills to support older browsers -->
<script src="//polyfill.io/v3/polyfill.min.js?features=es2015%2CIntersectionObserver" crossorigin="anonymous"></script>

<!-- Load Vue followed by BootstrapVue -->
<script src="//unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>

<div id="app">
<b-form-group label="Hide columns: ">
  <b-form-checkbox-group id="checkbox-group-1" v-model="selected" :options="fields" name="flavour-1">
  </b-form-checkbox-group>
</b-form-group>


<b-table id="myTable" 
         
         :bordered="true"
         hover head-variant="dark"  
         stacked="md" 
         :items="items" 
         :fields="selected" 
         :current-page="currentPage"
         :per-page="perPage" 
         tbody-tr-class="row-class"
          v-if="selected.length > 0">
</b-table>
<b-table id="myTable" 
         
         :bordered="true"
         hover head-variant="dark"  
         stacked="md" 
         :items="items" 
         :fields="activeFields" 
         :current-page="currentPage"
         :per-page="perPage" 
         tbody-tr-class="row-class"
          v-if="selected.length > 0">
</b-table>
</div>

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

Creating a read-only DIV using Angular - a step-by-step guide

Is there a simple way to make all clickable elements inside a div read only? For example, in the provided HTML code, these divs act like buttons and I want to disable them from being clicked. Any tips or shortcuts to achieve this? Thank you. #html < ...

determining the number of characters in a phrase within a cell using Google Apps Script

I am new to scripting and I am trying to determine the length of a sentence within a cell in Google Sheets. For example, if the sentence "I want to check the length" is in cell A1, I want to know how many characters are in the sentence. Additionally, if I ...

Add information if the data does not exist, modify it if it does

My current project involves creating a TS3 bot using the Node.js Library from Multivit4min on GitHub. The purpose of this bot is to perform the following tasks: nick (txt) cldbid (int) clid (txt) lastChannel (int) Event #1 - When a client connects to a ...

What sets apart a space after the ampersand from no space in Material UI?

Could you clarify the difference between using a space after the ampersand compared to not having a space? For example: Why is there a space after the ampersand in & label.Mui-focused but no space in &.Mui-focused fieldset? const WhiteBorderTextF ...

Modifying data on the fly for a Highcharts series

My chart is currently functioning well with data in the options. However, when I leave the data empty for a series and attempt the following code (in order to change the data based on a click event), it fails to work. Any suggestions on how I can rectify ...

The x-axis is represented by JSON keys, while the y-axis is represented by

I am currently attempting to replicate a graph that was originally made in Excel. Here is the code I have written so far: var data = [ { 'FB':4, 'Mv':4, 'CB':5, 'SL':3, ...

What is the best way to differentiate and analyze new AJAX output from previous data using div elements?

Embarking on a project to develop a high/low game using JavaScript, I encountered a perplexing issue where the displayed numbers seemed to be out of sync with the variables stored. This discrepancy left me scratching my head as I struggled to get them to a ...

Ways to modify a global variable across all components

I am looking to implement an automatic system that changes the page title according to the view I am on. Initially, I considered using a global variable in my main.js file: // MAIN.JS FILE const App = createApp(AppVue).use(router) App.config.globalProper ...

Adjust the size of the iframe image to fit seamlessly within the design

Is there a way to adjust the size of the image on the right without altering the layout design? The current GIF is 500x500px, but it is only displaying as 100x100px. Any assistance would be greatly appreciated! To see what I currently have (Demo with code ...

Revive the JavaScript library for handling mouse wheel events

Utilizing the wheel-indicator JavaScript library, I am looking to revert the mouse wheel event back to its original state after it was initially set to preventDefault(). Despite attempting to use indicator.setOptions({preventMouse:"false"}) as suggested b ...

Observer fails to activate

I am currently working with Vue 3 using the options API. In the code below, I have a watch object monitoring changes to isToggleBtnLabelDigitizePolygon. When the method onDigitizePolygon changes the value of isToggleBtnLabelDigitizePolygon, the computed p ...

Unlocking the secret to obtaining durable identity provider tokens for JavaScript in web browsers

Currently, I am working on implementing browser-side JavaScript code for login with Facebook, Amazon, Twitter, and Google using Cognito. I have reached a point where I am able to obtain client tokens for all four platforms. However, the issue is that thes ...

Unique Shader - Three.js

I have been attempting to implement a custom shader in Three.js, following various examples, but I am encountering issues. Below is the code snippet I have been working with: var vertex = "void main(){vec4 mvPosition = modelViewMatrix * vec4( position, 1. ...

What is the best way to implement the <b-pagination-nav> component in Bootstrap Vue?

I'm eager to begin using the featured in this guide. However, I'm struggling to figure out how to incorporate the tag into my website. The tutorial's instructions are unclear and as a newcomer, I'm finding it difficult to make it func ...

Pattern matching in JavaScript using regular expressions is used to identify and extract repeating patterns,

There is a specific string that contains structured data and multiple instances of @doc { within it. I need to extract these occurrences, making sure the result includes two unique matches. To achieve this, regular expressions come in handy: var matches = ...

I attempted to establish a connection between my backend and the MongoDB database, however, an error was displayed

Error: Failed to establish a connection to the database! MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 Please check the database connection settings and try again. For more information, refer to the error log. ...

VUE 3 inexplicably failing to display the custom component's template, console remains error-free

I am utilizing flask, html, js for building a web application. I am currently facing an issue where the template defined in the component <add-movie> within movies.js is not being rendered into add_movies.html. The add_movies.html extends base_admin ...

Is it possible to arrange a react map based on the length of strings?

I am working on a React function that loops through an object and creates buttons with text strings retrieved from the map. I want to display the text strings in order of their length. Below is the code snippet for the function that generates the buttons: ...

What method can be used to incorporate Google Places API into a .js file without using a <script> element?

As I delve into the Google Places API documentation, I noticed that they require the API be loaded in this format: <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> Is ...

Ensure that the jQuery Knob is set to submit any modifications only after the final adjustment

Utilizing jQuery Knob by anthonyterrien, I have configured the step to be 20. My goal is to transmit the knob's value to the server. The issue arises when the change function is triggered every time the user adjusts the knob using either a right or le ...