在quasar-framework中选择数据表行上的隐藏按钮

时间:2018-05-31 09:46:39

标签: vue.js vuejs2 quasar-framework

我正在使用quasar-framework datatable 组件,我想隐藏标题 并在选择至少一行时添加按钮。

我有下表:

table without select row

当选择行发生时:

table after select row

我还要隐藏上面的添加按钮,请参阅下面的代码

<template>

<!-- Table settings -->
<q-table
    :title="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 
            @click="$router.push(addUrl)"
            icon="add_circle"
            size="14px"
            color="secondary"
            label="Add" />
    </template>

    <!-- Actions bar slot -->
    <template slot="top-selection" slot-scope="props">
       <q-btn color="secondary" flat label="Action 1" class="q-mr-sm" />
        <q-btn color="secondary" flat label="Action 2" />
        <div class="col" />
        <q-btn color="negative" flat round delete icon="delete" 
@click="deleteRow" />
    </template>

    </q-table>

</template>

 <script>
    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.selectedSecond.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>

我也想要一个包含顶部选择的模板槽值的列表。

1 个答案:

答案 0 :(得分:0)

To hide the button, simply use 'v-show' with 'selected.length == 0' As for the second statement, I am not clear, but I will show how to get selections and display in a list...

new Vue({
  el: '#q-app',
  data: () =>({
      tableData: [{
        id: 1,
        name: 'Steve'
      },
      {
        id: 2,
        name: 'Bob'
      }],
      selection: 'multiple',
      selected: [],
      tableThs: [
        {field: row => row.id, label: 'Id'},
        {field: row => row.name, label: 'Name'}
      ],
      dataSource: [],
      tableTitle: 'Employee',
      addUrl: 'http://addthis'
      
    }),
    methods: {
      deleteRow () {
        this.$q.notify({
          color: 'secondary',
          icon: 'delete',
          message: `Will delete the selected 
  row${this.selectedSecond.length > 1 ? 's' : ''} later, ok?`
        })
      }
    },
    mounted() {
      axios.get("/api/"+this.dataSource)
        .then(response => {
        this.tableData = response.data.data;
      });
    }
})
   .row{
       margin-left: 0;
       margin-right: 0;
   }
   .q-table-bottom{
       border-top: 0;
   }
<div id="q-app">
<!-- Table settings -->
<q-table
    :title="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 
            @click="$router.push(addUrl)"
            icon="add_circle"
            size="14px"
            color="secondary"
            label="Add" v-show="selected.length == 0"></q-btn>
    </template>

    <!-- Actions bar slot -->
    <template slot="top-selection" slot-scope="props">
       <q-btn color="secondary" flat label="Action 1" class="q-mr-sm"></q-btn>
        <q-btn color="secondary" flat label="Action 2"></q-btn>
        <div class="col"></div>
        <q-btn color="negative" flat round delete icon="delete" 
@click="deleteRow"></q-btn>
    </template>
    </q-table>
  <q-list>
    <q-item v-for="s in selected" :key="s.id">
      {{s.name}}
    </q-item>                 
  </q-list>
</div>

Codepen