0

Is there a way to declare temporary variable in a VueJS template? My problem is that in a v-for i check if the actual value exists in an Array and if not I print another text, but for each element of the v-for it will do this twice (firstly to check if exists, and then to print the value) where with a variable it could be done once.

I'm using VueJS2 without any preprocessor nor template engines.

Here it's my array :

let formats = [
    {id: 1, label: "A3"},
    {id: 1, label: "A4"},
    {id: 1, label: "A5"},
    {id: 1, label: "A6"},
];

Here is my template :

<tr v-for="data in ajaxDatas">
    <td>
        <template
            v-if="formats.find(e => {
                return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
            })"
        >
            {{ formats.find(e => {
                return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
            }).label }}
        </template>
        <template v-else>
            Other format
        </template>
    </td>
</tr>

As you can see it's not really optimized... I was thinking in a way to make a temporary variable to stock the find return, something like this :

<tr v-for="data in ajaxDatas">
    <td>
        {{
            var f = formats.find(e => {
                return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
            });
            f ? f.label : 'Other format'
        }}
    </td>
</tr>

FOR THE MOMENT i found a way but it's not working properly, i can make something like this :

<tr v-for="data in ajaxDatas">
    <td>
        {{ f = formats.find(e => {
                return (e.shortEdge == data.height && e.longEdge == data.width) || (e.shortEdge == data.width && e.longEdge == data.height);
            }) }}
        {{ f ? f.label : 'Other format' }}
    </td>
</tr>

But this is actually printing me result of f in the HTML ... So i put it into an undisplayed html tag but it's make me think there's something better to do here...

0

3 Answers 3

1

Just create a method and use it in your template:

methods: {

  getFormat(width,height) {

    var f = this.formats.find( e => {

      return (e.shortEdge == height && e.longEdge == width) || (e.shortEdge == width && e.longEdge == height);

    });

    return f ? f.label : 'Other format'

  }

}
<tr v-for="data in ajaxDatas">
  <td>
      {{ getFormat(data.width,data.height) }}
  </td>
</tr>

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm yes, it works, but as I have so many methods in my view I was looking for something more easy, and I think it could be great to declare variables in the vue template like twig does with set
0

Don't do this in template you can filter out your data in computed property then in v-for use that computed property.As a rule of thumb always keep your template as simple as possible and all the complex logic will go in script (methods, computed props or life cycle hooks).

3 Comments

how do you do a computed method depending on all the data that your ajax return?
Lets say you have some property which holds data once resolved from ajax response, in computed property you can apply any filtering logic on the props which holding data and return the filtered result additionally you can set computed property directly by using setter and on template side check if computed props has data eg: v-if="computedProps.length" then render it using v-for.
Yeah, i see, but I need one property for each row i'll render, so i need as many computed as i have row in my table?
0

I found a way from fabruex's answer, but directly in the template... It's not relative to VueJs, it's making a Self-Executing Anonymous Function...

Check this out :

<tr v-for="data in ajaxDatas">
    <td>
        {{
            ((h, w) => {
                var f = formats.find(e => {
                    return (e.shortEdge == h && e.longEdge == w) || (e.shortEdge == w && e.longEdge == h);
                });
                return f ? f.label : 'Other format'
            })(data.height, data.width)
        }}
    </td>
</tr>

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.