5

I have data : 1,2,3,4,4,5 & my code like this:

<div id="looping" v-for="display in editlistAssesments">
  {{display.test_id}}
</div>

My code if in php such as like this

$temp_id = array();
foreach($data as $data){
  if(in_array($data ->test_id,$temp_id)){
    echo" 1: no";
    echo" 2: no";
    echo" 3: no";
    echo" 4: yes"; //because he have same value 
    echo" 5: no";
    $temp_id[] = $data ->test_id;
  }
}

how I can do that in loop vueJs..??

2
  • 2
    But that array in php start empty. I don't quite understand what you are trying to do... Commented Mar 4, 2016 at 11:45
  • What does your data look like in your JS? Commented Mar 4, 2016 at 13:34

2 Answers 2

9

From my point of view, the best way is:

<div id="looping" 
     v-for="display in editlistAssesments">
    <span v-if="typeof display.test_id !== 'undefined'">
        {{display.test_id}}
    </span>
</div>

Because if you use v-if="display.test_id" and the test_id value is 0 (boolean comparation) you will never see the display.text_id.

You can use also this another condition to check if is null or undefined: display.test_id != null (loose equality operator)

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

Comments

6

As far as I understand, you want to check if value is in array and then render it accordingly?

If so, you need a Vue custom filter. Something like this will do the trick:

var vm = new Vue({
    el: 'body',

    data: {
        editlistAssesments: [1,2,3,4,4,5]
    },

    filters: {
        ifInArray: function (value) {
            return this.editlistAssesments.indexOf(value) > -1 ? 'Yes' : 'No';
        }
    },
});

And then use it like this:

<div id="looping" v-for="display in editlistAssesments">
    <span v-text="display.test_id | ifInArray"></span>
    <!-- bind Vue value to html element is better practice -->
</div>

Check docs for more information: http://vuejs.org/guide/custom-filter.html

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.