0

I'm trying to figure out if this is best way to go about conditional rendering of HTML in a v-for with Vuejs. I have several different types of information displayed on "cards" that I'm getting back as json from a DynamoDB instance. Each type of card should have it's own template. The code I have now is:

<ul>
    <template v-for="card in cards | onlyMatching">
        <li transition="expand" v-if="card.Data.event_type === 'user_comment'">
            @include('feed.card_templates.user_comment')
        </li>

        <li transition="expand" v-if="card.Data.event_type === 'user_position'">
            @include('feed.card_templates.user_position')
        </li>
    </template>
</ul>

Each of those @include's pulls in the html for that specific type of card rendering and uses vue's templating engine to plug in the properties. On initial page load this works really well. But if I do any type of filtering on the original array - the html always stays on the page. In fact after updating it will duplicate the html on the page as many times as I run the filter.

Can anyone point me in the right direction? Am I going about this the most efficient way?

2 Answers 2

0

seems to work if you take the for loop off the root element of the component:

<ul>
    <template>
       <div v-for="card in cards | onlyMatching">
          <li transition="expand" v-if="card.Data.event_type === 'user_comment'">
               @include('feed.card_templates.user_comment')
          </li>
          <li transition="expand" v-if="card.Data.event_type === 'user_position'">
              @include('feed.card_templates.user_position')
          </li>
       </div>
    </template>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

When I do it that way - none of the cards show up at all. Not even on initial render.
I'd need to see your javascript to know more
The javascript isn't really the issue. In your example the v-for would be on the wrong element anyway. In your code the ul itself would be rendered multiple times which is not what is needed here. My js is actually posted in this question here: stackoverflow.com/questions/35093138/…
0

Vue was working as it was supposed to with the current js code. Looks like this issue was with Laravel's blade engine and partials. Clearing the view cache seems to have fixed the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.