最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue js How to split string to array and use in v-for list renderer - Stack Overflow

programmeradmin2浏览0评论

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>

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('+'))"

Share Improve this question edited Apr 18, 2018 at 9:41 APB asked Apr 18, 2018 at 9:23 APBAPB 3371 gold badge2 silver badges8 bronze badges 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 – Terry 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 :) – APB Commented Apr 18, 2018 at 9:33
Add a comment  | 

3 Answers 3

Reset to default 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

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

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('+'):'';
        }
    }

})
发布评论

评论列表(0)

  1. 暂无评论