简体   繁体   English

从表 vuejs、graphql、json 中搜索/过滤数据

[英]search/filter data from table vuejs, graphql, json

I have a table that shows the ID and title from my data.我有一个表格,显示我的数据中的 ID 和标题。 I had to pull the Title from a json which is saved in the database as a string.我不得不从 json 中提取标题,该标题作为字符串保存在数据库中。 Now I want to filter/search in this table.现在我想在这个表中过滤/搜索。 I can search the IDs but not the title.我可以搜索 ID,但不能搜索标题。 I tried everthing that google showed me, but it didn't work.我尝试了谷歌向我展示的一切,但没有奏效。 What Am I doing wrong?我究竟做错了什么?

I'm new in Vue, can anybody help me?我是Vue的新手,有人可以帮助我吗?

Code:代码:

        <v-text-field
            v-model="search"
            append-icon="mdi-magnify"
            label="Search"
            single-line
            hide-details
        ></v-text-field>
       
        <v-data-table
          :headers="headers"
          :items="showActivatedOnly()"
          :search="search"
          sort-by="id"
          class="elevation-1"
        >....

    showActivatedOnly: function(){
      if(!this.search){
        if(this.showActivatedSurveysOnly===true){
          return this.adminSurveys
        }else{
          return this.adminSurveysOnlyActivated;
        }
      }
      return this.getAllSurveysFiltered();
    },
    getAllSurveysFiltered() {
            const surveys = this.adminSurveys;
      
      const child = surveys.filter(item => {
        let json = JSON.parse(item.json)
        return json.title.toString().toLowerCase().includes(this.search.toLowerCase())
      });
      
      console.log(child + "CHILD")
      console.log(surveys + "ALL_SURVEYS")
      
      return(child)

    },```
      

The Method getAllSurveysFiltered() response the right list but the elements aren't showing in my table.

The first potential problem is that you are attempting to filter results on the client side.第一个潜在问题是您试图在客户端过滤结果。 That may be OK if the number of results is so small that you will always be able to load all of them in the client and filter them from there.如果结果的数量如此之少,以至于您始终能够将所有结果加载到客户端并从那里过滤它们,那可能没问题。 But what if the search result is not matched in the first page of results, but if would have matched later pages?但是,如果搜索结果在结果的第一页没有匹配,但如果会匹配后面的页面呢? Then the client would incorrectly show that there are no search results.然后客户端会错误地显示没有搜索结果。

The title of the question says you are using GraphQL, so in theory you should to be able to filter the results on the server side using a query somewhat like this:问题的标题说您正在使用 GraphQL,因此理论上您应该能够使用类似这样的查询过滤服务器端的结果:

query {
  surveys(where: { title_CONTAINS: "seachTermGoesHere" }) {
    title
  }
}

However, that example query is probably not exactly right for your use case because the query syntax depends on how your back end is implemented.但是,该示例查询可能并不完全适合您的用例,因为查询语法取决于后端的实现方式。

If you do end up filtering results on the client side, your filter function should be simplified.如果您最终在客户端过滤结果,您的过滤器 function 应该被简化。 If I were you, I would also make it a computed property so that it would update based in its dependency:如果我是你,我也会将其设为计算属性,以便根据其依赖项进行更新:

computed: {
  filteredSurveys() {  
      // This assumes that adminSurveys is an array of objects
      // where each object has a `title` key
      return this.adminSurveys.filter((survey) => {
        return survey.title.toLowerCase().includes(this.search.toLowerCase())
      })
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM