The function to toggle each element in the array is malfunctioning

I am working on a Vue.js project where I have a table filled with data from the back-end. Currently, all the fields are displayed in the table as they are copied from the database structure object. However, I want to add functionality to manage which fields are displayed in the table. I have created a function for this purpose, but it is not working as expected. Can someone assist me in properly developing this feature?

<button class="btn btn-info dropdown dropdown-toggle" type="button" id="setVisibility" data-mdb-toggle="dropdown" aria-expanded="false" >
   TABLE FIELD MENAGEMENT 
</button>
  <ul class="dropdown-menu prevent-close py-1 px-2" aria-labelledby="setVisibility">
    <li>
     <div class="layout-header pt-2 text-center">
     <h6>Hide/Show</h6>
     </div>
     <div v-for="(header, key, index) in visibleHeaders" :key="key" class="form-check form-switch">
       <input v-model="isHidden" :value="key" class="form-check-input " type="checkbox" id="isHidden">
       <label class="form-check-label" for="isHidden">{{header}}</label>
     </div>
    </li>
  </ul>
<thead class="">
<tr>
   <th><input type="checkbox" class="form-check-input" v-model="selectAll" title="Select All"></th>
   <th v-if="!isHidden" v-for="(header, index) in visibleHeaders" :key="index" scope="col">
       {{ header }}
   </th>
   <th>ACTION</th>
</tr>
</thead>
<tbody>
 <tr v-show="leads.length" v-for="column in visibileColumn" >
 <td>
  <input type="checkbox" class="form-check-input" v-model="selected" :value="column.id" />
  </td>
  <td v-for="(atr, index) in column">
    {{atr}}
 
</tr>
<tr v-show="!leads.length">
 <td colspan="12" class="text-center">Sorry :( No data found.</td>
</tr>
</tbody>
...

data() {
   return {
     isHidden: false,
     headers: [],
     leads: [],
     ...
    }
}
 computed: {
        visibleHeaders() {
            return this.headers.map(h => {
                h.isHidden = true
                return h.Field.replace("_", " ").toUpperCase()
            });
        },
visibileColumn() {
            return this.leads.map(c => {
                // console.log(c)
                return c
            })
        },
}
headers:Array[9]
0:Object
Default:null
Extra:"auto_increment"
Field:"id"
Key:"PRI"
Null:"NO"
Type:"bigint unsigned"
isHidden:undefined


leads:Array[5]
0:Object
created_at:null
first_name:"john"
id:6
last_name:"doe"
lead_status:"new"
notes:null
primary_email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d3d6d1d7ddd6dcf9ded4d8d0d597dad6d4">[email protected]</a>"
primary_phone:"0696969699"
updated_at:null

Answer №1

If I have grasped your message correctly, you can implement it using the code snippet below:

New approach for Vue framework implementation:
  Initialization of Vue instance with specific properties and methods.
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <ul class="dropdown-menu prevent-close py-1 px-2" aria-labelledby="setVisibility">
    <li class="dropdown-item">
     <div v-for="(header, index) in visibleHeaders" :key="index" class="form-check form-switch">
       <input v-model="isHidden[index]" :id="index" :value="index" class="form-check-input " type="checkbox" id="isHidden">
       <label class="form-check-label" for="isHidden">{{ header }}</label>
     </div>
    </li>
  </ul>
  <table>
    <thead class="">
      <tr>
        <th v-if="isHidden[index]" v-for="(header, index) in visibleHeaders" :key="index" scope="col">
           {{ header }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-show="leads.length" v-for="column in leads" >
        <td v-for="(atr, index) in column" >
          <div v-if="isHidden[index]" >
            {{ atr }}
          </div>
        </td>
      </tr>
      <tr v-show="!leads.length">
        <td colspan="12" class="text-center">Apologies :( No data found.</td>
      </tr>
    </tbody>
  </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

Tips on how to choose all elements that do not include a specific value or group of values in cypress

Here's my situation: selector: ".list > div" const velue = [6, 9, 21] Each div contains a different value. I want to remove elements that contain a specific value, then select the first remaining element and click on it. It should look ...

Error encountered: jquery form validation fails to register changes

I am currently developing a calculator-like code where users will submit a form multiple times, and I need to save the result of calculations only if there are changes in the form. For instance, when a user clicks the "Calculate" button for the first time, ...

Executing JavaScript file using TypeScript code in Node.js

Is it possible to execute a JS file from TypeScript code in Node.js? One way to achieve this is by exposing the global scope and assigning values to it. For example: Global Scope (TypeScript): globalThis.names = ['Joe', 'Bob', 'J ...

What is the reason for the Web worker creating a new Class Instance every time a module is imported?

We are currently running a time-consuming function on a web worker. In addition, we have a Dispatcher Class that creates a single instance for other classes to utilize, as shown below: class Dispatcher { constructor() { console.log("calling"); ...

How can we maintain line breaks in Textfields when using React-Admin and Material UI?

Currently, I am working with React-Admin and encountered an issue where my database field contains line breaks (\n). However, when I try to display it on a page using the following code: <TextField source="extra_details" /> The line breaks are ...

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 ...

Vue Heatmap Chart with Zoomable Timeline using ApexCharts

Is there a method to incorporate a zoom feature to enhance the Heatmap chart in ApexCharts using VueJS? <VueApexCharts v-bind:style="{ 'box-shadow':'0px 0px 2.3px 0px'}" width="350" height="220" type=heatmap :options="chartOptionsNe ...

Convert a JSON object into a new format with a nested hierarchy

The JSON object below is currently formatted as follows: { "id": "jsonid", "attributes": { "personName": { "id": "name1", "group": "1.1" }, "ag ...

What steps can be taken to diagnose the cause of a failed Jquery AJAX request?

I am attempting to utilize the Yahoo Finance API to retrieve data in CSV format through Javascript. However, my current implementation shown below is not successful. $.ajax({ type: "GET", url: "http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT&f=sb2b3j ...

Tips for preserving component state during page rerenders or changes

I created a counter with context API in React, and I'm looking for a way to persist the state even when the page is changed. Is there a method to store the Counter value during page transitions, similar to session storage? My app utilizes React Router ...

Choosing the right target for AngularJS/Node.js output

I'm working on a project that utilizes AngularJS and node.js to process CSV files before generating an output. Right now, the only method I have for outputting the file is through: <a download="fileName.csv" ng-href="{{file}}">fileName.csv</ ...

Alert Ajax/Javascript when the background PHP operation has finished

Here's the scenario - straightforward and concise : On Page A : An ajax request is made (to intermediate.php) which triggers a background process - let's call it back.php interm.php responds back.php continues its operation until completion, wh ...

Receiving JSON data and saving it into an array in JavaScript

I have a JSON input that contains information about different categories including companies, countries, and persons. { "Categories": { "Facets": [{ "count": 1, "entity": "Company", "Company": [{ ...

Using the <script> attribute within the <head> tag without the async or defer attributes

https://i.stack.imgur.com/cRFaR.pngCurious about Reddit's use of the async or defer attribute in the script tag. Are there situations where blocking the parser is necessary? ...

Modal feature malfunctioning in web browser

My function is not working in the mobile browser, but it works on desktop. I'm not sure why this is happening. Has anyone experienced this before? $('body').on('click touchstart', '.show_range', function(event) { alert(" ...

Creating a personalized input slider: A step-by-step guide

I have a question about how to design a custom input slider with the label inside the field itself. The desired output should look like the screenshot below: https://i.sstatic.net/gE6EJ.png I have successfully created the input field part, but I am stru ...

Trouble with executing two asynchronous AJAX calls simultaneously in ASP.NET using jQuery

When developing a web application in asp.net, I encountered an issue with using jQuery Ajax for some pages. The problem arose when making two asynchronous Ajax calls - instead of receiving the results one by one, they both appeared simultaneously after the ...

Update the variables upon a click event following the completion of an AJAX request

Despite my efforts, I am unable to find a solution to the issue I am currently facing. To address this problem, I have created a script using PHP and jQuery that enables users to promote their "listings" on my website. When users visit a specific page, the ...

Using AJAX and PHP to redirect a PHP page once specific conditions are satisfied

Hello, I am currently facing an issue and I could use some guidance with Ajax as I am still learning how to work with it. The situation is this: I have a webpage that undergoes a refresh every 7 seconds using Ajax and Javascript. During each refresh, one ...

Issues arise when utilizing the jQuery cookie extension

Here is the code snippet that I am currently working with: <button class="create">create</button> <button class="check">check</button> and $(document).ready(function() { $("#set_state").change(function() { var theState ...