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
titleprop so it only displays when no rows are selected. - Add Button: Add a
v-ifdirective to the button in thetop-rightslot 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 rowsselectedCount: Number of rows that are currently selectedtoggleAll: Method to toggle the selection state of all rows in the tableclearSelection: Method to instantly clear all selected rowsprops: 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:
- Added conditional rendering for the table title and Add button based on
selected.length - Destructured
selected,selectedCount, andclearSelectionin thetop-selectionslot to demonstrate practical prop usage - Fixed the typo in
deleteRow(changedthis.selectedSecondtothis.selected) - Added a "Clear Selection" button using the built-in
clearSelectionmethod from the slot scope
内容的提问来源于stack exchange,提问作者Mohammed Galal
相关产品推荐
相关产品推荐

