Iterate over rows in a b-table component in Vue

In the context of Vue bootstrap, a b-table consists of a list with an unknown number of rows. The requirement is to display a remove button only if the email in the row matches that of the currently logged-in user. This functionality currently works for a single row.

<b-table
    :items="selectedGrant.interested_agencies"
    :fields="interestedAgenciesFields"
  >
  <template #cell(actions)="row">
    <b-row v-if="loggedInUser.email === selectedGrant.interested_agencies[0]['user_email']">
      <b-button variant="danger" class="mr-1" size="sm" @click="unmarkGrantAsInterested(row)">
        <b-icon icon="trash-fill" aria-hidden="true"></b-icon>
      </b-button>
    </b-row>
  </template>
</b-table>

The question arises on how to iterate through all rows within the table.

Simply using

selectedGrant.interested_agencies[row]['user_email']
doesn't progress through each individual row. The conventional looping methods are mostly associated with li lists and not specifically tailored for b-tables. Attempts were made to include a v-for in the template as well as another b-row, but there was no success.

An alternative attempt involved utilizing a computed method, however, it proved unsuccessful:

loop() {
   let i;
   let match;
   for (i = 0; i < this.interestedAgenciesFields.length(); i += 1) {
     if (this.loggedInUser.email === this.selectedGrant.interested_agencies[i].user_email) {
       match = true;
     } else {
       match = false;
     }
   }
   return match;
 },

There's speculation whether implementing JavaScript's .find() method could be effective, yet experimentation yielded no positive results.

Is there a viable approach to iterate through a b-table, either within the template or through a computed method, in order to verify a specific value in every row? It should not be restricted to checking just one row.

Answer №1

There is no need to iterate through all the rows in the condition, as you are already within the row loop when using the #cell({key}) slot. Your goal should be to compare the current row's user_email with loggedInUser.email:

<template #cell(actions)="row">
  <b-row v-if="loggedInUser.email === row.item.user_email">
    <b-button variant="danger" 
              class="mr-1"
              size="sm"
              @click="unmarkGrantAsInterested(row)">
      <b-icon icon="trash-fill" aria-hidden="true"></b-icon>
    </b-button>
  </b-row>
</template>

Refer to the documentation here.


What's happening?
#cell(actions)="row" represents a #cell{{key}} slot (where {key} is actions). By clicking on the Show scope button in the docs, you can examine the structure of the slot (you named it row in your template - indicating the structure of row). You should utilize its item property, which holds the raw data of the current row.

You aim to show the content of the actions cell for rows where row.item.user_email matches loggedInUser.email.

That sums it up.

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

Replacing a function in TypeScript

Looking to alter the behavior of the alert function in TypeScript? You can achieve this by creating a new function that wraps around the original alert and modifies its behavior. alert = (function (origAlert) { return function (...messages: any[]) { ...

Have you ever wondered how the automatic video play on scroll feature works on Tiktok.com and YouTube shorts when using a mobile device?

My goal with React JS is to develop a website similar to Tiktok, where the video below will automatically play with sound as the user scrolls down. I attempted to set this up using window.addEventListener("scroll",...), but after looking into it further, ...

Running a PHP function within a PHP environment

After the user clicks the submit button and no errors are present, a token value input is generated in the script below. The goal is to post this value inside a php page for use in a query. While the input value is successfully generated, posting it to the ...

retrieving data from a node and embedding it into an HTML file

In my project, I have an index.html file and an app.js nodejs file. Within the app.js file, there is a variable named "name" that I would like to display in my index.html page. Here is the code snippet: var name = "Utsav"; var HTTP = require('HTTP&a ...

Exploring the differences between scoping with let and without any scoping in

Within my code, there is a forEach loop containing a nested for loop. It's interesting that, even though I have a statement word = foo outside of the for loop but still inside the forEach loop, I can actually log the value of word after the entire for ...

Having trouble loading an image after successfully connecting to an API with react.js

I've been working on a custom fetch component to load images from the "the dog API" onto my page. However, I'm facing some issues with getting the images to display correctly. Can anyone spot what might be missing in my setup? App.js import &apo ...

JavaScript conditional calculation mechanism

Here is the code snippet I am referring to: function myFunction() { var x = document.getElementById("ins-feet").value; if(x >= 0 && x <= 1499) { document.getElementById("show-cost").innerHTML = "Cost: $" + 300; } else if ...

Maintaining the initial value of a textbox in jQuery across various focusin events

Struggling to accurately explain the process, so feel free to check out this helpful fiddle. In my form, there are several text input fields and a single submit button. The submit button becomes enabled once any of the form fields have been altered. If a ...

Different ways to showcase a value from the CSS file on the console using console.log

In this guide, you can learn how to create a custom directive in Angular by following this tutorial: Custom Directive Tutorial. The directive should function as intended. Still, I want to see the color value set in the CSS file displayed on the console us ...

extract values from a JavaScript function on a website

Currently, I am facing a challenge in automatically retrieving elements from a webpage and I seem to be stuck on a few things. My approach involves looping through all classes named 'trList' using document.getElementsByClassName('trList&apo ...

Rendering a dynamic list of asynchronous components in Vue 3, with support for extension

Could you please assist me in resolving this issue? I've spent countless hours searching for a solution, but I can't seem to make it work. Vue is still very new to me. Let me provide some more context. I have an asynchronous component that i ...

What is the best way to dynamically insert a new row into a table, with each row containing a table heading and column?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tbl" class="tbl1"> <tr> <th> mobileno </th> <td class='mo' id="mo_0"> </td> ...

What are the steps to fix the CORS problem when sending AJAX requests from a frontend to a Flask server?

My current project involves creating a web application with Flask for the backend and JavaScript for the frontend. However, I'm encountering challenges with CORS policy when attempting to send AJAX requests from my frontend to the Flask server. Below ...

What is V8's approach to managing dictionaries?

After attending V8 presentations, it became clear to me that it optimizes constructions such as the one below by tagging a type object: function Point(x, y) { this.x = x; this.y = y; } I am curious about what happens if I were to return an object (JS ...

What is the best way to position two elements floated to the right in separate rows within a single row?

Having trouble with a web design issue. I am currently working on a website and trying to format an input field with a label so that the label sits on top of the input while floating to the right. However, I'm encountering difficulties as the elements ...

Is it possible to use window.print() to print the entire contents of a DIV with both horizontal and vertical scroll bars?

In my code, I have a div that contains both horizontal and vertical scrollers. Whenever I try to print the content inside this div using the window.print() method, it only prints the visible portion of the div. Here is the code snippet that I have attempte ...

Update the background image to be smoother, with no text overlay

I am working on a website where I need to smoothly change between background images. Here is the JavaScript code I currently have: var bg=[ 'images/best.jpg', 'images/61182.jpg', 'images/bg.jpg' ...

The Angular 2 http request for a POST method is not initiating

Utilizing Angular 2, I have successfully implemented a get request function. However, when attempting to make a post request, I am unable to detect any sign of the request in the Firebug Net panel. The code for these methods is as follows. Furthermore, th ...

Using NodeJS to store the value from a callback function in a variable external to the function

I am facing a situation where I need to assign the value inside a callback function to a variable outside of it. Below is an example using child_process. callback.js let result; require('child_process').spawn('python', ['./hello.py ...

Turn the image inside the div with the nth-child selector into a clickable link

I'm currently facing a challenge on my Squarespace website where I need to add individual links to various images using jQuery. The issue is that these images do not have a shared class, and unfortunately, I am limited to adding custom CSS or JavaScri ...