2017-07-26 43 views
-1

我有一個職位的循環,每個崗位都有評論box.Initially所有評論框是隱藏的,當有人點擊「評論」按鈕,它應該顯示註釋中顯示旁邊的按鈕,唯一的輸入字段該用戶的字段爲。我無法顯示與特定帖子相關的特定評論框。我的代碼如下 -如何循環

<div class="post-section" v-for="(post,index) in posts"> 
    <div class="media-content">{{post.body}}</div> 

    <button @click="getComments(post, index)" class="btn btn-link"><i class="fa fa-comments-o"></i>{{ post.total_comments }} Comments</button> 

    <comment-input :postId="post.id"></comment-input> 
</div> 

<script> 
    export default { 
     data() { 
      return { 
       posts: [], 
      } 
     }, 

     created() { 
      Post.all(posts => this.posts = posts); 
     }, 

     methods: { 
      getComments(post, index){ 
       axios.post('getcomments', {id: post.id}) 
        .then(response => { 
         this.$set(this.posts, index, Object.assign({}, post, { comments: response.data })); 
        }); 
      }, 
     } 
    } 
</script> 

當getComments(post,index)方法執行時,我只想讓下一個註釋輸入可見。任何幫助?

enter image description here

在此先感謝。

+0

這是你想要的嗎? https://jsfiddle.net/r_vamsi_krishna/smkw17Lk/ –

+0

不完全! ..每個帖子都有一個關聯的評論框。我想最初隱藏所有評論框。當有人點擊評論按鈕(如UR小提琴),它將把所有的意見,也使看到的評論框只有這個職位。我已經簡化了我的代碼並顯示在上面,並用圖像更新了我的問題。它像谷歌加或Facebook。我希望你得到我想要的。感謝@VamsiKrishna – WahidSherief

+0

@VamsiKrishna作爲alwayw妳的傳奇!非常感謝。它像魅力一樣工作。 – WahidSherief

回答

1

可以稱爲toggleComments: false一個額外的屬性添加到您post對象。並用它來切換註釋部分,包括其在<div>分組註釋文本框。

這裏是fiddle

HTML

<div id="app"> 
    <div v-for="(post,index) in posts"> 
     <p>{{post.body}}</p> 
     <button @click="getComments(post, index)" class="btn btn-link">Show/Hide Comments</button> 
     <div v-if="post.toggleComments"> 
      <textarea rows="1" cols="50" placeholder="comment here ..."></textarea> 
      <div v-for='comment in post.comments'> 
       <span class="comm">Commented by:{{comment.name}}</span> 
      </div> 
     </div> 
    </div> 
</div> 

腳本

new Vue({ 
    el: '#app', 
    data: { 
     posts: [ 
      {id: 1, body: ' this is post #1'}, 
      {id: 2, body: ' this is post #2'}, 
      {id: 3, body: ' this is post #3'}, 
      {id: 3, body: ' this is post #4'}, 
      {id: 4, body: ' this is post #5'} 
     ] 
    }, 
    methods:{ 
     getComments(post, index){ 
      if(!post.comments){ 
       Vue.http.get('https://jsonplaceholder.typicode.com/users') 
       .then(response => { 
        this.$set(this.posts, index, Object.assign({}, post, { comments: response.data }, { toggleComments: false})); 
       },err => { 
        //handle errors 
       }); 
      } 
      post.toggleComments = !post.toggleComments; 
     } 
    } 
}) 
0

另一種方法,我可以分享的是:

HTML

<div class="post-section" v-for="(post,index) in posts"> 
    <div class="media-content">{{post.body}}</div> 

    <button @click="getComments(post, index)" class="btn btn-link"><i class="fa fa-comments-o"></i>{{ post.total_comments }} Comments</button> 

    <comment-input v-if="index == selectedPostIndex" :postId="post.id"></comment-input> 
</div> 

腳本

<script> 
    export default { 
     data() { 
      return { 
       posts: [], 
      } 
     }, 

     created() { 
      Post.all(posts => this.posts = posts); 
     }, 

     methods: { 
      getComments(post, index){ 
       axios.post('getcomments', {id: post.id}) 
        .then(response => { 
         this.$set(this.posts, index, Object.assign({}, post, { comments: response.data })); 
        }); 

      this.selectedPostIndex = index; 
      }, 
     } 
    } 
</script>