0

为了实现多搜索功能,我们在每个带有 v-slot 的标题处添加一个搜索字段,我们直接定义标题名称。例如:

      <template v-slot:header.NAME="{ header }">
        {{ header.text }}
        <v-menu offset-y :close-on-content-click="false">
          <template v-slot:activator="{ on, attrs }">

            <v-text-field v-bind="attrs" v-on="on"
              v-model="nameSearch"
              class="pa"
              type="text"
            ></v-text-field>
          </template>         
        </v-menu>
      </template>  

现在我的数据表有大约 50 个标题。我的想法是动态渲染。我尝试使用 v-for 但它既不呈现也不给出任何错误。下面的代码:

        <template v-for="(x, index) in this.headers" slot="headers" slot-scope="props">
        {{ props.x.text }}
        <v-menu  offset-y :close-on-content-click="false" :key="index">
          <template v-slot:activator="{ on, attrs }">


            <v-text-field v-bind="attrs" v-on="on"
              v-model="searchObj"
              class="pa"
              type="text"
            ></v-text-field>

          </template>
          
        </v-menu>
      </template>  

我在这里错过了什么?还是我走的路完全错了?

以下是完整代码:

    <template>
      <v-app class="main-frame">
      <v-main>
            <v-data-table
              :headers="headers"
              :items="filteredData"
            >
              <template v-slot:header.NAME="{ header }">
                {{ header.text }}
                <v-menu offset-y :close-on-content-click="false">
                  <template v-slot:activator="{ on, attrs }">

                    <v-text-field v-bind="attrs" v-on="on"
                      v-model="nameSearch"
                      class="pa"
                      type="text"
                    ></v-text-field>

                  </template>
                  
                </v-menu>
              </template>        

              <template v-slot:header.DEPARTMENT="{ header }">
                {{ header.text }}
                <v-menu offset-y :close-on-content-click="false">
                  <template v-slot:activator="{ on, attrs }">


                    <v-text-field v-bind="attrs" v-on="on"
                      v-model="departmentSearch"
                      class="pa"
                      type="text"
                    ></v-text-field>

                  </template>
                  
                </v-menu>
              </template>  

          </v-data-table>
      </v-main>
      </v-app>
    </template>

    <script>

    import axios from "axios";

    }
    export default {
      name: 'Home',

      data() {
        return {
        /* Table data */ 
          headers: []
          allData: [],

          /* Column Search */
          nameSearch: '',
          departmentSearch: ''

        }
      },

      computed: {

        filteredData() {
          
          let conditions = [];
          
          if (this.nameSearch) {
            conditions.push(this.filterName);
          }

          if (this.departmentSearch) {
            conditions.push(this.filterDepartment);
          }
          
          if (conditions.length > 0) {
            return this.allData.filter((data) => {
              return conditions.every((condition) => {
                return condition(data);
              })
            })
          }
          
          return this.allData;
        }
      },


      mounted () {
        this.getAllData() 
      },

      methods: {
        getAllData() {
        this.allData = []

        axios
          .get("http://localhost:5001/"}
          .then(res => {
            if (res.status === 200) {
              if (res.data === 0) {
                console.log("Something is wrong !!!")
              } else {
                this.allData = res.data["allData"]

                this.headers = res.data["allDataHeaders"]

              }
            }
          
          }).catch(error => {
            console.log(error);
            
          })
      },

      filterName(item) {
          return item.NAME.toLowerCase().includes(this.nameSearch.toLowerCase())
      },

      filterDepartment(item) {
          return item.DEPARTMENT.toLowerCase().includes(this.departmentSearch.toLowerCase())
      },
        
    }
      
    }
    </script>
4

1 回答 1

0

我自己找到了解决方案:

        <template v-for="(header, i)  in headers" v-slot: 
        [`header.${header.value}`]="{  }"> 
        <div @click.stop :key="i">
         {{ header.text }}
            <v-text-field :key="i"
              v-model="multiSearch[header.value]"
              class="pa"
              type="text"
              :placeholder="header.value"
              prepend-inner-icon="mdi-magnify"
            ></v-text-field>
         </div>
        </template>

在数据中,我有一个名为的空对象:

searchObj: {}

在计算方法中,我有以下方法:

filteredData() {

  if (this.searchObj) {
    return this.allData.filter(item => {
      return Object.entries(this.searchObj).every(([key, value]) => {

        return (item[key] || '').toLowerCase().includes(value.toLowerCase())
        
      })
    })
  } else {
    return this.allData
  } 
},

完整解决方案如下:

  <template>
  <v-app class="main-frame">
  <v-main>
        <v-data-table
          :headers="headers"
          :items="filteredData"
        >

        <template v-for="(header, i)  in headers" v-slot: 
         [`header.${header.value}`]="{  }"> 
         {{ header.text }}
          <div @click.stop :key="i">
            <v-text-field :key="i"
              v-model="multiSearch[header.value]"
              class="pa"
              type="text"
              :placeholder="header.value"
              prepend-inner-icon="mdi-magnify"
            ></v-text-field>
          </div>
        </template>

      </v-data-table>
  </v-main>
  </v-app>
</template>

<script>

import axios from "axios";

}
export default {
  name: 'Home',

  data() {
    return {
    /* Table data */ 
      headers: []
      allData: [],
      searchObj: {},
    }
  },

  computed: {

    filteredData() {

      if (this.searchObj) {
        return this.allData.filter(item => {
          return Object.entries(this.searchObj).every(([key, value]) => {

             return (item[key] ||'').toLowerCase().includes(value.toLowerCase())

             })
        })
      } else {
          return this.allData
        } 
     },
  },


  mounted () {
    this.getAllData() 
  },

  methods: {
    getAllData() {
    this.allData = []

    axios
      .get("http://localhost:5001/"}
      .then(res => {
        if (res.status === 200) {
          if (res.data === 0) {
            console.log("Something is wrong !!!")
          } else {
            this.allData = res.data["allData"]

            this.headers = res.data["allDataHeaders"]

          }
        }
      
      }).catch(error => {
        console.log(error);
        
      })
  },

}
  
}
</script>
于 2021-11-08T12:04:20.773 回答