【问题标题】:How to display specified rows using vue js?如何使用 vue js 显示指定的行?
【发布时间】:2018-04-02 13:36:39
【问题描述】:

这是我从 url 获得的数据。我只需要打印第 2 行和第 4 行,

{
    "status": "ok",
    "source": "n",
    "sortBy": "top",
    "articles": [
        {
            "author": "Bradford ",
            "title": "friends.",
            "url": "http: //",
            "urlToImage": "http://"
        },
        {
            "author": "Bradford  ",
            "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
            "url": "http: //",
            "urlToImage": "http://"
        },
              "author": "Bord  ",
            "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
            "url": "http: //",
            "urlToImage": "http://"
        },
             "author": "Brad  ",
            "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
            "url": "http: //",
            "urlToImage": "http://"
        }
    ]
}

我的 vue js 脚本如下。这就是我从相应的 url 检索值的方式

 <script>
    new Vue({
        el: '#feed' ,
        data: {
        articles: [],
        },
        mounted() {
        this.$nextTick(function() {
          var self = this;
          $.ajax({
                url: "https://newsapi.org/v1/articles?source=espn&sortBy=top&apiKey=4db9d5d381a14ffcbca8e8a9aa8b864c",
                method: "GET",
                dataType: "JSON",
                success: function (e) {
                    if (e.status == 'ok') {
                        self.articles = e.articles;
                        console.log(e.articles)
                    }
                }, error: function(){
                console.log('Error occurred');
                }
            });
        })
        },
       })
</script>

我的 HTML 是

<div id="feed">
<div v-for="post in articles" class="mke_">
  <div class="row">
      {{post.title}}
    </div>
</div>
</div>

请帮我只显示第 2 篇和第 4 篇文章[]? 我在js方面很弱。所以,能帮到我就好了

【问题讨论】:

  • 如何确定需要第 2 行和第 4 行?

标签: javascript html vue.js vuejs2 vue-component


【解决方案1】:

我认为第 2 行和第 4 行是指索引 1 和 3 处的元素。最简单的方法是添加具有以下条件的 v-if 绑定:articles.indexOf(post) == 1 || articles.indexOf(post) == 3

new Vue({
  el: '#feed',
  data: {
    articles: [{
        "author": "Bradford ",
        "title": "friends.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Bradford  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }, {
        "author": "Bord  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Brad  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }
    ]
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>

<div id="feed">
  <div v-for="post in articles" v-if="articles.indexOf(post) == 1 || articles.indexOf(post) == 3" class="mke_">
    <div class="row">
      {{post.title}}
      <br/>
      <small>{{post.author}}</small>
    </div>
  </div>
</div>

为了将 UI 与逻辑分开,您可以为此目的使用计算。计算可以过滤所需的值,例如:

return this.articles.filter(t => 
    this.articles.indexOf(t) == 1 
    || this.articles.indexOf(t) == 3
);

new Vue({
  el: '#feed',
  data: {
    articles: [{
        "author": "Bradford ",
        "title": "friends.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Bradford  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }, {
        "author": "Bord  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Brad  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }
    ]
  },
  computed: {
    filteredPosts() {
      return this.articles.filter(t => this.articles.indexOf(t) == 1 || this.articles.indexOf(t) == 3);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>

<div id="feed">
  <div v-for="post in filteredPosts" class="mke_">
    <div class="row">
      {{post.title}}
      <br/>
      <small>{{post.author}}</small>
    </div>
  </div>
</div>

注意:我已将帖子作者添加到显示中,以便更容易了解过滤项目的索引。

更新

As suggested by Bert,过滤器可以改进为:

return this.articles.filter((t, i) => 
    i == 1 || i == 3
);

new Vue({
  el: '#feed',
  data: {
    articles: [{
        "author": "Bradford ",
        "title": "friends.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Bradford  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }, {
        "author": "Bord  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      },
      {
        "author": "Brad  ",
        "title": "Kershaw threw six mesmerizing innings to put L.A. into the Fall Classic.",
        "url": "http: //",
        "urlToImage": "http://"
      }
    ]
  },
  computed: {
    filteredPosts() {
      return this.articles.filter((t,i) => i == 1 || i == 3);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>

<div id="feed">
  <div v-for="post in filteredPosts" class="mke_">
    <div class="row">
      {{post.title}}
      <br/>
      <small>{{post.author}}</small>
    </div>
  </div>
</div>

【讨论】:

  • 不,先生,我需要打印文章数组的所有值。
  • 在这个例子中 { "author": "Bradford", "title": "Kershaw 投了六局令人着迷的局,让洛杉矶进入秋季经典赛。", "url": "http://" , "urlToImage": "http://" } { "author": "Brad", "title": "Kershaw 投了六局令人着迷的局,让洛杉矶进入秋季经典赛。", "url": "http:// ", "urlToImage": "http://" }
  • 在您写的问题中:我只需要打印第 2 行和第 4 行。你这是什么意思??
  • 请检查该网址。有很多数据。我的目标是只打印其中的几个
  • indexOf 在这里是不必要的。过滤器回调接收索引作为参数。 (t,i) =&gt; i == 1 || i == 3
猜你喜欢
  • 2018-04-01
  • 2018-05-01
  • 2018-04-15
  • 2018-03-23
  • 2021-03-12
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多