Wrap element using v-if, otherwise just have content itself

Qix - MONICA WAS MISTREATED

I have a set of elements being generated in a v-for directive that, if the object has a url property, get wrapped in an <a> tag - otherwise, I need it to just emit the element itself.

For example,

var data = {
    events: [
        {name: "Foo"},
        {name: "Bar", url: "google.com"}
    ]
};

and the corresponding HTML:

<div v-for="event in events">
    <span>{{event.name}}</span>
</div>

What I need is to wrap the <span> in an <a v-bind:href="url"> only if there is a url property present.

I understand I could use a v-if and use two spans, such as:

<span v-if="!event.url">{{event.name}}</span>
<a v-bind:href="event.url" v-if="event.url">
    <span>{{event.name}}</span>
</a>

However, in my use case the <span> element here could be massive and I don't want to repeat myself just to wrap the element.

Is there a way to achieve a conditional wrap such as the above?

Egor Stambakio

You can use v-html for example and render your logic inside a function:

function wrapSpan(el, link) { // link wrapper function
  return `<a href="${link}">${el}</a>`;
}

new Vue({
  el: '#app',
  data: {
    events: [
      {name: "Foo"},
      {name: "Bar", url: "google.com"}
    ]
  },
  methods: {
    element: function(i) {
      const name = this.events[i].name;
      const url = this.events[i].url || null;
      const span = `<span style="color: green">${name}</span>`; // long span
      return (url) ? wrapSpan(span, url) : span;
    }
  }
});
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app">
  <div v-for="(event, index) in events">
    <span v-html="element(index)"></span>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap substring of element content with an element using jQuery

From Dev

Wrap the contents of an element in a ".content" div

From Dev

wrap content inside parent element

From Dev

Wrap content using directive and transclude

From Dev

Directive, wrap content and keep attributes on original element

From Dev

Match an element and wrap other content that comes before matched element

From Dev

Match an element and wrap other content that comes before matched element

From Dev

Using the Aurelia <content> element

From Dev

Using the Aurelia <content> element

From Dev

Facebook Fresco using wrap_content

From Dev

Using .delegate to wrap content loaded via AJAX

From Dev

Using custom icons, dp or wrap_content?

From Dev

search first element under itself using Jquery

From Dev

Empty element positions itself differently from one with content?

From Dev

Make View have a weight, but at least be wrap_content

From Dev

How to have an 'EditText' element deselect itself when 'enter' is pressed

From Dev

How to wrap an element using an angular directive?

From Dev

Wrap element to new line/row using flexbox

From Dev

Using Jquery wrap function with absolute placed element

From Dev

Wrap each line text to an element using JQuery

From Dev

Cannot show all the content using wrap_content in Android

From Dev

How to wrap inner content of a paragraph after specific child element?

From Dev

How to wrap inner content of a paragraph after specific child element?

From Dev

How to have a directive template keep element content?

From Dev

Is there a way to invoke route manually in ServiceStack, using just a string that would otherwise be its URL?

From Dev

Shorten content of an element using CSS

From Dev

Shorten content of an element using CSS

From Dev

How to show divs when the mouse moves anywhere on screen, not just the element itself?

From Dev

Using angular scope directive variable content as an attribute itself

Related Related

  1. 1

    Wrap substring of element content with an element using jQuery

  2. 2

    Wrap the contents of an element in a ".content" div

  3. 3

    wrap content inside parent element

  4. 4

    Wrap content using directive and transclude

  5. 5

    Directive, wrap content and keep attributes on original element

  6. 6

    Match an element and wrap other content that comes before matched element

  7. 7

    Match an element and wrap other content that comes before matched element

  8. 8

    Using the Aurelia <content> element

  9. 9

    Using the Aurelia <content> element

  10. 10

    Facebook Fresco using wrap_content

  11. 11

    Using .delegate to wrap content loaded via AJAX

  12. 12

    Using custom icons, dp or wrap_content?

  13. 13

    search first element under itself using Jquery

  14. 14

    Empty element positions itself differently from one with content?

  15. 15

    Make View have a weight, but at least be wrap_content

  16. 16

    How to have an 'EditText' element deselect itself when 'enter' is pressed

  17. 17

    How to wrap an element using an angular directive?

  18. 18

    Wrap element to new line/row using flexbox

  19. 19

    Using Jquery wrap function with absolute placed element

  20. 20

    Wrap each line text to an element using JQuery

  21. 21

    Cannot show all the content using wrap_content in Android

  22. 22

    How to wrap inner content of a paragraph after specific child element?

  23. 23

    How to wrap inner content of a paragraph after specific child element?

  24. 24

    How to have a directive template keep element content?

  25. 25

    Is there a way to invoke route manually in ServiceStack, using just a string that would otherwise be its URL?

  26. 26

    Shorten content of an element using CSS

  27. 27

    Shorten content of an element using CSS

  28. 28

    How to show divs when the mouse moves anywhere on screen, not just the element itself?

  29. 29

    Using angular scope directive variable content as an attribute itself

HotTag

Archive