如何让表格的某些列包含一个下拉框来过滤表格?

发布于 2025-01-11 08:29:58 字数 783 浏览 0 评论 0原文

我正在使用 VueJs 和 Vuetify。我有 Vuetify 的表格,用于展示数据。我正在尝试编辑某些列的标题列,以便我可以添加下拉搜索栏来过滤表格。类似于:

“在此处输入图像描述”"

其中 Select Item 实际上是标题(请参阅旁边的排序符号)。我可以将其添加到表格上方的下拉菜单中,就像官方文档中所示的那样,但我试图将下拉菜单设置为标题列本身,而不仅仅是表格上方。

当前代码:

<v-data-table
  class="elevation-1"
  :headers="headers"
  :items="items"
  :items-per-page="tableItemsPerPage"
  :footer-props="tableFooterSettings"
  :loading="tableLoading"
  hide-default-footer
>

其中 headers 是一个简单的标题数组,例如:

{ text: 'data', value: 'pretty_data', align: 'center', sortable: true },

当然,我希望该列仍然可排序(就像图像[箭头]中所示)。我怎样才能完成它?

I'm using VueJs and Vuetify. I have Vuetify's table where I present the data. I'm trying to edit the header column of some of the columns so I could add a dropdown search bar to filter the table. Something like:

enter image description here

Where Select Item is actually the header (please see the sort symbol aside of it). I could add it the dropdown above the table, like it's shown in the official docs, but I'm trying to make the dropdown as the header column itself and not just above the table.

The current code:

<v-data-table
  class="elevation-1"
  :headers="headers"
  :items="items"
  :items-per-page="tableItemsPerPage"
  :footer-props="tableFooterSettings"
  :loading="tableLoading"
  hide-default-footer
>

where headers is a simple array of the headers, for example:

{ text: 'data', value: 'pretty_data', align: 'center', sortable: true },

Of course I want the column to be still sortable (like it's shown in the image [the arrow]). How can I make it done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-18 08:29:58

创建了这个工作演示。希望它能按照您的期望工作。

new Vue({
  el: '#app',
  data: () => ({
    selected: [],
    headers: [
      {
        text: 'Name',
        align: 'left',
        value: 'name'
      }
    ],
    filters: {
      name: []
    },
    desserts: [
      {
        name: 'Frozen Yogurt'
      },
      {
        name: 'Ice cream sandwich'
      },
      {
        name: 'Eclair'
      },
      {
        name: 'Cupcake'
      },
      {
        name: 'Gingerbread'
      },
      {
        name: 'Jelly bean'
      },
      {
        name: 'Lollipop'
      },
      {
        name: 'Honeycomb'
      },
      {
        name: 'Donut'
      },
      {
        name: 'KitKat'
      }
    ]
  }),
  computed: {
    filteredDesserts() {
      return this.desserts.filter(d => {
        return Object.keys(this.filters).every(f => {
          return this.filters[f].length < 1 || this.filters[f].includes(d[f])
        })
      })
    }
  },

  methods: {
    columnValueList(val) {
      return this.desserts.map(d => d[val])
    }
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet">
<div id="app">
  <v-app id="inspire">
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="filteredDesserts"
      item-key="name"
      class="elevation-1"
    >
      <template slot="headers" slot-scope="props">
        <tr class="grey lighten-3">
          <th
            v-for="header in props.headers"
            :key="header.text"
          >
            <div v-if="filters.hasOwnProperty(header.value)">
              <v-select flat hide-details small multiple clearable :items="columnValueList(header.value)" v-model="filters[header.value]">
              </v-select>
            </div>
          </th>
        </tr>
      </template>
      <template slot="items" slot-scope="props">
        <tr>
          <td>{{ props.item.name }}</td>
        </tr>
      </template>
    </v-data-table>
  </v-app>
</div>

Created this working Demo. Hope it will work as per your expectation.

new Vue({
  el: '#app',
  data: () => ({
    selected: [],
    headers: [
      {
        text: 'Name',
        align: 'left',
        value: 'name'
      }
    ],
    filters: {
      name: []
    },
    desserts: [
      {
        name: 'Frozen Yogurt'
      },
      {
        name: 'Ice cream sandwich'
      },
      {
        name: 'Eclair'
      },
      {
        name: 'Cupcake'
      },
      {
        name: 'Gingerbread'
      },
      {
        name: 'Jelly bean'
      },
      {
        name: 'Lollipop'
      },
      {
        name: 'Honeycomb'
      },
      {
        name: 'Donut'
      },
      {
        name: 'KitKat'
      }
    ]
  }),
  computed: {
    filteredDesserts() {
      return this.desserts.filter(d => {
        return Object.keys(this.filters).every(f => {
          return this.filters[f].length < 1 || this.filters[f].includes(d[f])
        })
      })
    }
  },

  methods: {
    columnValueList(val) {
      return this.desserts.map(d => d[val])
    }
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet">
<div id="app">
  <v-app id="inspire">
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="filteredDesserts"
      item-key="name"
      class="elevation-1"
    >
      <template slot="headers" slot-scope="props">
        <tr class="grey lighten-3">
          <th
            v-for="header in props.headers"
            :key="header.text"
          >
            <div v-if="filters.hasOwnProperty(header.value)">
              <v-select flat hide-details small multiple clearable :items="columnValueList(header.value)" v-model="filters[header.value]">
              </v-select>
            </div>
          </th>
        </tr>
      </template>
      <template slot="items" slot-scope="props">
        <tr>
          <td>{{ props.item.name }}</td>
        </tr>
      </template>
    </v-data-table>
  </v-app>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文