You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quasar Framework中选中行时隐藏标题与添加按钮的技术需求

Solution for Quasar DataTable Requirements

Hey there! Let's tackle your Quasar DataTable requirements step by step. I'll break down the fixes and explanations for both of your needs.

1. Hide Table Title & Add Button When Rows Are Selected

To achieve this, we'll use conditional rendering based on the length of the selected array:

  • Table Title: Dynamically bind the title prop so it only displays when no rows are selected.
  • Add Button: Add a v-if directive to the button in the top-right slot to hide it as soon as any row is selected.

2. Top-Selection Slot Available Props List

The top-selection slot in Quasar's QTable provides several handy properties via its slot scope. Here's a breakdown of the most useful ones:

  • selected: Array containing the data objects of all currently selected rows
  • selectedCount: Number of rows that are currently selected
  • toggleAll: Method to toggle the selection state of all rows in the table
  • clearSelection: Method to instantly clear all selected rows
  • props: Object with underlying table properties (like the table instance, pagination data, etc.)

You can destructure these props directly in the slot for cleaner, more readable code—check the updated example below to see this in action.


Updated Full Code

Here's your modified code with all requirements implemented, plus a fix for a small typo in the deleteRow method:

<template>
  <!-- Table settings -->
  <q-table
    :title="selected.length === 0 ? tableTitle : ''"
    :data="tableData"
    :columns="tableThs"
    :selection="selection"
    :selected.sync="selected"
    row-key="__index"
  >
    <!-- Add button slot -->
    <template slot="top-right" slot-scope="props">
      <q-btn
        v-if="selected.length === 0"
        @click="$router.push(addUrl)"
        icon="add_circle"
        size="14px"
        color="secondary"
        label="Add"
      />
    </template>

    <!-- Actions bar slot with expanded props usage -->
    <template slot="top-selection" slot-scope="{ selected, selectedCount, clearSelection }">
      <span class="q-mr-md">Selected {{ selectedCount }} row(s)</span>
      <q-btn color="secondary" flat label="Action 1" class="q-mr-sm" />
      <q-btn color="secondary" flat label="Action 2" class="q-mr-sm" />
      <q-btn color="primary" flat label="Clear Selection" @click="clearSelection" class="q-mr-sm" />
      <div class="col" />
      <q-btn color="negative" flat round icon="delete" @click="deleteRow" />
    </template>
  </q-table>
</template>

<script>
import axios from 'axios'

export default {
  props: ['tableThs', 'dataSource', 'tableTitle', 'addUrl'],
  data: () => ({
    tableData: [],
    selection: 'multiple',
    selected: [],
  }),
  methods: {
    deleteRow () {
      this.$q.notify({
        color: 'secondary',
        icon: 'delete',
        message: `Will delete the selected row${this.selected.length > 1 ? 's' : ''} later, ok?`
      })
    }
  },
  mounted() {
    axios.get(`/api/${this.dataSource}`)
      .then(response => {
        this.tableData = response.data.data;
      });
  }
}
</script>

<style>
.row{
  margin-left: 0;
  margin-right: 0;
}
.q-table-bottom{
  border-top: 0;
}
</style>

Key Changes Made:

  1. Added conditional rendering for the table title and Add button based on selected.length
  2. Destructured selected, selectedCount, and clearSelection in the top-selection slot to demonstrate practical prop usage
  3. Fixed the typo in deleteRow (changed this.selectedSecond to this.selected)
  4. Added a "Clear Selection" button using the built-in clearSelection method from the slot scope

内容的提问来源于stack exchange,提问作者Mohammed Galal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:44:03