Hiding the +/- button in Vue.js based on specific conditions

Is there a way to hide the +/- button when there are no items below?

Current Challenge

Example Sandbox Link

<template v-for="item in SevenLengthlist" :key="item">
  <ul>
    <li><input type="checkbox" id="item-0" /><label for="item-0">{{ item.mAcName }} --- {{ item.mName }}</label>
      <ul>
        <li v-for="item2 in item.TenLength" :key="item2">
          <input type="checkbox" id="item-0-0" />
          <label for="item-0-0">{{ item2.mAcName }} --- {{ item2.mName }}</label>
          <ul>
            <li v-for="item3 in item2.TenLength" :key="item3">
              <input type="checkbox" id="item-0-0" />
              <label for="item-0-0">{{ item3.mAcName }} --- {{ item3.mName }}</label>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</template>

Answer №1

Initially, the current implementation is not up to par.
Utilizing an image + background offset in this context is not ideal (SVG + v-show would be a more effective and manageable approach).

Here are some key takeaways:

  • The outcome will lack quality, resulting in pixelation when zoomed in
  • The logic should be incorporated into the template (avoid trying to handle everything within the style section; leverage Vue's capabilities)
  • Your example lacks recursion, which is essential for this task
  • I've rectified several errors related to best practices and console warnings (consider using ESlint)

Referencing the following example from the documentation is advisable: https://vuejs.org/examples/#tree


For the desired end result, you can utilize the following code snippet:

<template>
  <div class="css-treeview">
    <ul>
      <li v-for="item in list1" :key="item.a">
        <input :id="`item-${item.a}`" type="checkbox" />
        <label :for="`item-${item.a}`"
          :class="[!item.list2.length && 'hide-when-not-needed', item.list2.length && 'open-if-possible']">
          {{ item.a }}
        </label>
        <ul>
          <li v-for="item2 in item.list2" :key="item2.a">
            <input :id="`item-${item2.a}`" type="checkbox" />
            <label :for="`item-${item2.a}`"
              :class="[!item2?.list3?.length && 'hide-when-not-needed', item2?.list3?.length && 'open-if-possible']">
              {{ item2.a }}
            </label>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String,
  },
  data() {
    return {
      list1: [
        {
          a: 'Alex',
          list2: [{ a: 'Dog' }],
        },
        {
          a: 'Blex',
          list2: [{ a: 'Dogoo' }],
        },
        {
          a: 'Clex',
          list2: [],
        },
      ],
    }
  },
}
</script>

<style scoped>
.css-treeview ul,
...
...
/* Remaining CSS styles continue here */
...
...
</style>

A demo showcasing the above concepts is available at this link: View Playground

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

Is it necessary to include vendor prefixes when using the value "none"?

I have a question regarding vendor prefixes that has been on my mind. I couldn't find a concrete answer - do we need to include them when disabling/resetting styles with values such as "none", "initial", "0", etc.? For instance, if I apply a box shado ...

Action Bar in Nativescript positioned below the status bar

While working on creating a Nativescript Vue app, I encountered an issue with my Android 10 device (Samsung S10+), where the status bar was partially covering the Action Bar. This problem is also visible in the playground app. https://i.sstatic.net/VBsi8 ...

firefox is moving the div over to the right side

Hello! I am new to web development and excited to learn. Can someone help me with an issue on my site? I have a website that is coded in html/css. The site looks fine on chrome and safari, but on firefox, the Team page layout is getting messed up. Any s ...

What steps can be taken to ensure that an element is perfectly centered within a div?

I have a div containing some images: <div> <img src="1.png"> </img> <img src="1.png"> </img> </div> Currently, the layout in browsers looks like this: ----------------- |XX | ----------------- H ...

Unable to update the color of material icon using document.getElementById(item) method

if (document.getElementById(item).style.color == "grey") { document.getElementById(item).style.color = "red"; } <i class="material-icons" [ngStyle]="post.isLiked != null ? {'color': 'red'}: {'color': 'grey'}" id ...

AngularJS: Unable to locate element using getElementById function

Using a third party JavaScript application, I have integrated and invoked a function within one of my AngularJS factories. This function requires the id of a DOM element as a parameter, as it manipulates this element. ThirdPartyApp.doSomething("#elementId ...

Utilize the hexadecimal color code as a string within the darken function

When working with a less style, I have a string variable. @colorString: 'DADADA'; I can convert it into a color: @color: ~'#@{colorString}'; Then, I am able to use @color to define a value in a style: div { color: @color } However ...

The measurement of the element's height once the page has finished loading

I have a scenario where I need to set the height of a parent div based on its absolute child div, taking into consideration that there are images within the child div that may take some time to load. CSS: #parent { overflow:hidden; wi ...

Retrieve the CSS class definition using the console in the Chrome Developer Tools

Is there a way to automatedly extract CSS class definitions from Chrome Developer Tools? I want to retrieve the styles defined in a specific class name, similar to how it is displayed in the Styles tab on the right-hand side. I know about the getComputedS ...

Addressing ESLint and TypeScript Issues in Vue.js with Pinia: A comprehensive guide

Experiencing difficulties with Vue.js + Pinia and need assistance to resolve these issues. Error: 'state:' is defined but never used. Here is the snippet of code located in @/stores/user.ts. import { defineStore } from 'pinia' export ...

Toggle the visibility of HTML elements by utilizing a JavaScript checkbox event

I have put together these JavaScript functions to hide the delivery address fields on my shopping cart address form if the goods are being sent to the billing address. The functions control the visibility of the HTML wrapped by... function getItem(id) { ...

Discovering the values within a separate JSON object

I have a collection of json objects that include information about different languages and user details. Languages User Details The user details contain a field for languages, which can have multiple values. Below is a sample json: $scope.languages = ...

Transform preexisting Vue UI library measurements into pixels

I was curious about converting all existing units (em / rem) to pixels. How can I easily convert the entire output units to px? Are there any tricks or methods available? Currently, I am utilizing Vuesax UI to create a plugin for various websites and temp ...

Verifying the number of div elements within an if condition

I've been attempting to determine the number of div elements in my DOM. However, when I try to assign this count to a variable, it displays as undefined in the console. Strangely, when I utilize an alert, it correctly shows the value as "8". My goal ...

If the text is a single line, center it. However, do not center the text if it

Can we center align text horizontally if it occupies a single line, but refrain from center aligning and use white-space: normal when the text spans multiple lines (ideally without the need for JavaScript)? ...

Is there a way to monitor song listens in a playlist using HTML5, Jplayer, and mysql?

In the process of creating a personalized player with 3 different playlists, the user will be able to choose which playlist they would like to listen to. I am seeking a way to track how many times each song in the selected playlist has been played. Is it ...

Placing an item from a "stack" onto a separate line on smaller screens

My goal in Bootstrap is to achieve the design shown in this image: https://i.stack.imgur.com/9Eoen.png The first layout can be achieved by placing B and C within the same div container. The second layout can be accomplished by defining C on a new row ind ...

Increasing or setting a minimum value for a <input type="number"> compared to a specific number

I'm working with a form that looks like this: <input type="text" id="theName"> <input type="number" id="theNumber"> <input type="button" id="submitForm"> I need to implement validation using AngularJs when the submit button is clic ...

Slick.js integrated with 3D flip is automatically flipping after the initial rotation

I'm encountering an issue with my CSS3 carousel and 3D flipping. Whenever I navigate through the carousel and flip to the next slide, the first slide seems to automatically flip/flop after completing the rotation. You can see a visual demonstration o ...

Encountering npm issues while attempting to publish website on GitHub

Encountering errors in the terminal while attempting to deploy a website on Github using npm I am at a loss for what steps to take next PS C:\Users\user\modern_portfolio> npm run deploy > <a href="/cdn-cgi/l/email-protection" cl ...