How can I resolve the "Unexpected redundant attribute on `<template>`" error occurring in a BootstrapVue table?

This inquiry is related to the answer on StackOverflow that can be found here

How can I incorporate superscript in the column header of a BootstrapVue table?

Below is the unaltered code snippet for the BootstrapVue table.

<script>
  export default {
    data() {
      return {
        // Note 'isActive' has been omitted and will not be displayed in the rendered table
        fields: [
          {
            key: 'last_name',
            sortable: true
          },
          {
            key: 'first_name',
            sortable: false
          },
          {
            key: 'age',
            label: 'Person age',
            sortable: true,
            // The variant attribute applies to the entire column, including the header and footer
            variant: 'danger'
          }
        ],
        items: [
          { isActive: true, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
          { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
        ]
      }
    }
  }
</script>

Here's the solution to add superscript to the column header of the BootstrapVue table.

<b-table>
  <template #head(age)="data">
    {{ data.label }} <sup>1</sup>
  </template>
</b-table>

The aforementioned solution works perfectly with the original code. However, it encounters an issue if the original key name (age) contains a space and the % character between characters (%age new). Below is the modified code when there is a space within the key name.

<script>
  export default {
    data() {
      return {
        // Note 'isActive' has been omitted and will not be displayed in the rendered table
        fields: [
          {
            key: 'last_name',
            sortable: true
          },
          {
            key: 'first_name',
            sortable: false
          },
          {
            key: '%age new', // A space in the key causes 
            label: 'Person age',
            sortable: true,
            // The variant attribute applies to the entire column, including the header and footer
            variant: 'danger'
          }
        ],
        items: [
          { isActive: true, '%age new': 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { isActive: false, '%age new': 21, first_name: 'Larsen', last_name: 'Shaw' },
          { isActive: false, '%age new': 89, first_name: 'Geneva', last_name: 'Wilson' },
          { isActive: true, '%age new': 38, first_name: 'Jami', last_name: 'Carney' }
        ]
      }
    }
  }
</script>

With this alteration in the key name, the respective solution would be as follows.

<b-table>
  <template #head(%age new)="data">
    {{ data.label }} <sup>1</sup>
  </template>
</b-table>

An error message displays:

error  Unexpected useless attribute on `<template>`  vue/no-useless-template-attributes
error  Unexpected useless attribute on `<template>`  vue/no-useless-template-attributes

What steps can be taken to rectify this error?

I am utilizing Vue v2.6 and BootstrapVue.

Answer №1

When Vue encounters the attributes #head(%age and new)="data", it interprets them as HTML attributes first before considering any syntactic implications from Vue or Bootstrap-Vue (such as brackets and parentheses). This behavior is explained in the documentation under "Dynamic Argument Syntax Constraints" (v2 docs, v3 docs), which applies even though the attribute name appears complex but not exactly dynamic:

Due to syntax constraints for dynamic argument expressions, certain characters like spaces and quotes are considered invalid within HTML attribute names. For instance, the following example is deemed invalid:

<!-- This will trigger a compiler warning. -->
<a v-bind:['foo' + bar]="value"> ... </a>

To work around this issue, you can either avoid using spaces or quotes in your expressions, or replace the complicated expression with a computed property.

Despite the flexibility of attribute names in accepting different characters, escaping spaces is not possible, posing a limitation. As a result, you have two options:

  • If feasible, you could define a computed property or data value such as
    percentagePropertyName = "head(%age new)"
    within your component, enabling the use of #[percentagePropertyName] syntax (among others).
  • An alternative approach involves renaming your field to eliminate special characters, for instance, changing it to percentage_new. Subsequently, you can easily map this in your data objects, e.g.,
    dataArray = dataArray.map(x => ({ percentage_new: x["%age new"], ...x }));
    .

Answer №2

One option is to consider utilizing string literals

<b-table>
  <template #head(`age new`)="data">
    {{ data.label }} <sup>1</sup>
  </template>
</b-table>

Another approach could be to either substitute the space with an underscore or convert the key to camelCase

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

Difficulty arising from the final column of average sums - Information extracted from JSON using Angular JS

I am facing a problem with calculating the average total. I need to determine the average sum using a specific formula, but the code provided does not calculate the last column of Average. I am looking for a desired result and would appreciate any assistan ...

Collapse an array containing objects with nested objects inside

Similar questions can be found here and here, but I am struggling to modify the code provided in the answers to fit my specific needs due to its conciseness. The structure of my array of objects is as follows: [ { id: 1, someProp: &quo ...

How can I include JavaScript files in Maya or 3DS Max?

My dilemma lies in attempting to access a js file on Maya (2013) and 3ds max (2016) without any available three.js importers for these particular softwares. How can I effectively import a js file on either Maya or 3ds max? Alternatively, are there other ...

What is the best way to convert a 2D PHP array into a JavaScript array?

I have encountered a problem with a PHP array setup as follows: $output = array(array(1,1,1,1),array(2,2,2,2),array(3,3,3,3)); When I encoded the array to JSON, I received this output: $output = {"1":[1,1,1,1],"2":[2,2,2,2],"3":[3,3,3,3]} My goal is to ...

Viewing a ViewChild in Angular 8 results in receiving an undefined value

Struggling to access the childView instance as it keeps showing that the childView is undefined. Below is the code snippet for childViews: @ViewChild(CreateQuestionnaireComponent,{ read: true, static: false }) private childQuestionnaireDialog:CreateQues ...

I'm experiencing a strange issue where the timezone offset is being doubled on my computer, yet it is functioning correctly on the UTC

A situation has arisen where the timezone offset on my local server is being duplicated by the server while creating a new event in my app. For every event created by a user, the client-side scripting computes and adds the time zone offset to the input be ...

A guide on adding images using Isotope

Working on a cutting-edge single-page JavaScript application showcasing images, inspired by pinspire. Utilizing jQuery (1.7.2) and the latest version of isotope libraries for development. Incorporating an infinite scroll feature within the app. Upon reach ...

Why does this image slider begin with blankness?

For my recent school project, I created an image slider. However, upon starting the page or reloading it, only the arrows and dots are displayed. View screenshot of the issue Below is the code snippet: // JavaScript function to control the slideshow sho ...

What steps should I take to either combine my two functions in my script or ensure they are executed in a specific sequence?

I'm attempting to incorporate a script into an HTML file (based on the solution provided by "skara9" in this post) and customize it to suit my requirements. However, I'm struggling to figure out how to make it work as intended. The script consis ...

Why won't disabling a Javascript checkbox also grey out the associated label in ASP.NET?

My current challenge involves using JavaScript to disable checkboxes in a checkbox list. The code snippet I am working with looks like this: var objItem = document.getElementById("<%= resCBL.ClientID %>"); var checkBoxes = objItem.getElementsByTagN ...

How come my flexbox items are not aligning in a row when using v-for?

I have designed a component that acts as a container for multiple items, using v-for to display four of these containers on my page. However, they are currently only shown in a single column layout and I need them to be displayed in two columns per row on ...

JavaScript event listener for SVG path element click is not functioning correctly as anticipated

How to determine if an element or its children have been clicked? I am trying to identify when a parent element or any of its child SVG icons with the attribute name set to "setGameState" have been clicked. The issue I am facing is that sometimes the even ...

Issue with Vue3: The imported module is not defined

Update: I recently downgraded vue-cli to version 4.5.19 and now the code is functioning properly. It seems like there might be an issue related to vue-cli or its dependencies. I encountered a problem while working on a simple vue.js project using vue-cli. ...

Looping through data returned from Ajax call and displaying it as HTML in CodeIgniter using JQuery

Just dipping my toes into the world of JQuery AJAX, here's what I've got so far: $(document).ready(function() { $("#city").change(function() { var city_id = $("#city").val(); if (city_id != '') { $.ajax({ type ...

What is the best way to modify the state of a particular element in an array when using useState in React?

In my Next.js application, I am using a useState hook to manage state. Here is how my initial state looks like: const [sampleData, setSampleData] = useState({ value1: '', value2: '', value3: [] }); To update the state ...

Issues with jQuery functionality in Django

Having some trouble displaying HTML data stored in result on my page with this code: $("#regerror").html(result); It doesn't seem to be working. However, when I use plain Javascript it works fine: document.getElementById("regerror").innerHTML = res ...

Ways to extract repeated value from a function?

Currently, I am working with two files. One file contains a script that generates a token, while the other file handles that token. The issue arises with the second script, as it only logs the initial token received and does not update with any new values ...

Navigating with Three.JS FPS controls by moving left and right

Currently, I am working on a demo to check player controls for a FPS game. The camera rotation is controlled by the mouse, and the player can move using W-A-S-D keys. However, I am facing an issue with implementing movement left and right relative to the d ...

JavaScript Delayed Redirection

I'm attempting to launch opera from the command line and have it redirect to a page after 30 seconds. Here's what I currently have: C:\Programme\Opera\opera.exe -newpage javascript:function%20func1(){window.location.href='htt ...

Organizing outcome searches through ajax

I have a result table displayed on the left side https://i.stack.imgur.com/otaV4.png https://i.stack.imgur.com/pp9m0.png My goal is to transform it into the format shown on the right side of the table In a previous inquiry found here, @Clayton provided ...