如何基于ID

时间:2018-11-23 08:17:06

标签: javascript vue.js vuejs2

我正在制作Vue项目。它包含一个div,并循环标题和  数据。 div内有一个搜索过滤器和内容,它们以p标签呈现。 p标签也  循环。

代码如下:

<div>
    <div v-for="(item) in data" :key="item.id">
        <div>{{item.title}}</div>
        <input type="text" v-model="search" />
        <div v-for="(content, j) in filteredLists" :key="j">
            <p v-for="(con, k) in content" :key="k">{{con}}</p>
        </div>
    </div>
</div>

这是数据

search: "",
data: [
    {
        id: 1,
        title: "Devsrc",
        content: {
            id: 1,
            details: ["ONE_DEV_EMP1", "ONE_DEV_EMP2"]
        }
    },
    {
        id: 2,
        title: "Devsrc2",
        content: {
            id: 2,
            details: ["TWO_DEV_EMP1", "TWO_DEV_EMP2"]
        }
    },
    {
        id: 3,
        title: "Devsrc3",
        content: {
            id: 3,
            details: ["THREE_DEV_EMP1", "THREE_DEV_EMP2"]
        }
    }
]

计算属性

filteredLists() {
      return this.data.map(item => {
       return item.content.details.filter(detail => {
           return detail.toLowerCase().match(this.search);
       })
      });
    },

我想做的是仅渲染项目ID ==内容的详细信息  ID。

2 个答案:

答案 0 :(得分:1)

使filteredLists()成为一种方法,而不是为初学者计算。

<div>
    <div v-for="(item) in data" :key="item.id">
        <div>{{item.title}}</div>
        <input type="text" v-model="search" />
        <div v-for="(content, j) in filteredLists(item.id)" :key="j">
            <p v-for="(con, k) in content" :key="k">{{con}}</p>
        </div>
    </div>
</div>

function filteredLists(id) {

  let term = this.search.trim().toUpperCase();
  let scoped = this.data.filter(item => { return parseInt(item.content.id) === parseInt(id) });

  if(term.length > 0) {
    return scoped.filter(item => {
      return item.content.details.find(seek => seek.match(search));
    });
  }

  return scoped;
}

let search = 'EMP2';

let data = [{
    id: 1,
    title: "Devsrc",
    content: {
      id: 1,
      details: ["ONE_DEV_EMP1", "ONE_DEV_EMP2"]
    }
  },
  {
    id: 2,
    title: "Devsrc2",
    content: {
      id: 2,
      details: ["TWO_DEV_EMP1", "TWO_DEV_EMP2"]
    }
  },
  {
    id: 3,
    title: "Devsrc3",
    content: {
      id: 3,
      details: ["THREE_DEV_EMP1", "THREE_DEV_EMP2"]
    }
  }
];

function filteredLists(id) {

      let term = search.trim().toUpperCase();
      let scoped = data.filter(item => { return parseInt(item.content.id) === parseInt(id) });

      if(term.length > 0) {
        return scoped.filter(item => {
          return item.content.details.find(seek => seek.match(search));
        });
      }

      return scoped;
}

console.log(filteredLists(3));

答案 1 :(得分:0)

extra_kwargs