【问题标题】:how to display 2 lists filtered by another field from one model如何显示由一个模型中的另一个字段过滤的 2 个列表
【发布时间】:2021-03-24 07:45:26
【问题描述】:

型号:文章。
身份证。
姓名。 类型:['代码','设计']

API 获取所有文章

如何显示两个列表:
所有 type ='Code',
的文章 所有 type = 'Design' 的文章

换句话说,是否可以过滤 API 查询
还是在 API 端做更好?

额外:同上,但在嵌套环境中(即文章属于类别。如何在类别详细信息页面上进行。

【问题讨论】:

    标签: vue.js vuejs3


    【解决方案1】:

    您可以使用计算属性。我构建了一个示例组件:

    编辑:花了一些时间把它擦干。

    父.vue

    <template>
      <div class="parent">
        <div class="row">
          <div class="col-md-6">
            <article-list title="Code Articles" :articles="codeArticles" />
          </div>
          <div class="col-md-6">
            <article-list title="Design Articles" :articles="designArticles" />
          </div>
        </div>
      </div>
    </template>
    
    <script>
      import ArticleList from './ArticleList.vue'
    
      export default {
        components: {
          ArticleList
        },
        data() {
          return {
            articles: [
              {
                id: 1,
                name: 'Article1',
                type: 'Code'
              },
              {
                id: 2,
                name: 'Article2',
                type: 'Design'
              },
              {
                id: 3,
                name: 'Article3',
                type: 'Code'
              },
              {
                id: 4,
                name: 'Article4',
                type: 'Design'
              },
            ]
          }
        },
        computed: {
          codeArticles() {
            return this.articles.filter(article => article.type === 'Code');
          },
          designArticles() {
            return this.articles.filter(article => article.type === 'Design');
          }
        }
      }
    </script>
    

    文章列表.vue

    <template>
      <div class="two-filtered-lists">
        <h5>{{ title }}</h5>
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>ID</th>
              <th>NAME</th>
              <th>TYPE</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="article in articles" :key="article.id">
              <td>{{ article.id }}</td>
              <td>{{ article.name }}</td>
              <td>{{ article.type }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </template>
    
    <script>
      export default {
        props: {
          title: {
            type: String,
            required: true
          },
          articles: {
            type: Array,
            required: true
          }
        }
      }
    </script>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多