4

I want to split a row of a list and use the result in another list statement like this : I actually know that the syntax of Vue list renderer of this type is incorrect but I want to show you what I need!

var app = new Vue({
	el : "#app",
  data : {
    list : [
      {
        'name' : 'aaa',
        'codes' : '111+222+333'
      },
      {
        'name' : 'bbb',
        'codes' : '432+456+678'
      }
    ]
  }
})
<div id="app">
  <div v-for="row in list">
    <p>{{ row.name }}</p>
    <div v-for="code in (row.codes.split('+'))">
      <p>{{ code }}</p>
    <div>
  </div>
</div>

Update: The above code is correct and I had some problems when using filters like this that is wrong :

v-for="code in (row.codes | split('+'))"

2
  • 3
    Your code will work as long as you (1) load VueJS library, (2) fix unclosed <p> and <div> elements, and (3) add the missing }) at the end of your code. jsfiddle.net/teddyrised/k1L7p9hm Commented Apr 18, 2018 at 9:25
  • wow! this is unbelievable but I didn't try this syntax. I actually used filters in for that was incorrect! thanks, Terry :) Commented Apr 18, 2018 at 9:33

3 Answers 3

10

Here is my solution!

<div id="app">
  <div v-for="row in splitedList">
    <p>{{ row.name }}</p>
    <div v-for="code in row.codes">
      <p>{{ code }}</p>
      </div>
  </div>
</div>

new Vue({
  el: "#app",
  data: {
    list : [
      {
        'name' : 'aaa',
        'codes' : '111+222+333'
      },
      {
        'name' : 'bbb',
        'codes' : '432+456+678'
      }
    ]
  },
  computed: {
    splitedList(){
        let newArr = [...this.list]
      newArr.map(el => {
        return el.codes = el.codes.split('+')
      })
      return newArr
    }
  }

})

See it in action

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

2 Comments

yes, this a true solution but little complicated :)
why complicated?it is very clear and simple in my opinion
4

v-for="code in row.codes.split('+')"

1 Comment

Code only answers are discouraged. consider editing to add explanation. Quality answers accrue the most upvotes over time as future visitors learn something to apply to their own code bases.
1

I use in function in method...

<div id="app">
  <div v-for="row in list">
    <p>{{ row.name }}</p>
    <div v-for="code in splitedList(row.codes)">
      <p>{{ code }}</p>
      </div>
  </div>
</div>

new Vue({
  el: "#app",
  data: {
    list : [
      {
        'name' : 'aaa',
        'codes' : '111+222+333'
      },
      {
        'name' : 'bbb',
        'codes' : '432+456+678'
      }
    ]
  },
  computed: {
  },
methods: {  
    splitedList(row){               
              return (row !== null)?row.split('+'):'';
        }
    }

})

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.