DataTable.vue 5.87 KB
Newer Older
Anton's avatar
Anton committed
1
<template lang="pug">
2 3
  div
    // buttons and search bar above the table
4
    .row.gutter-md.q-mb-md
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      .col-3.col-sm-4.col-md-3
        slot(name="top-buttons", slot-scope="props")
      .col-9.col-sm-8.col-md-9
        q-search(v-model="filter", dark, icon="search")

    q-table(
    v-if="cols.length > 0",
    dark,
    ref="table",
    row-key="name",
    :data="rows",
    :columns="cols",
    :selection="selection",
    :selected.sync="selected",
    @request="request",
    :loading="loading",
    :rows-per-page-options="rowsPerPage",
    :pagination.sync="pagination",
    :hide-bottom="hideBottom",
    :filter="filter",
    :title="tableTitle")
      template(slot="top-left", slot-scope="props")
        slot(name="buttons-left")
      <!--template(slot="top-right", slot-scope="props")-->
        <!--q-search(v-model="filter", dark, icon="search")-->
      q-td(slot="body-cell-title", slot-scope="props", :props="props")
        router-link.primary(v-if="hasShow", :to="getViewLink(props.row._uuid)")
          promise-span(:value="props.value")
        promise-span(v-if="!hasShow", :value="props.value")
      q-td(slot="body-cell-tags", slot-scope="props", :props="props")
Anton's avatar
Anton committed
35
        promise-span(:value="props.value")
36 37 38
      q-td(slot="body-cell-actions", slot-scope="props", :props="props")
        q-btn(v-for="btn in props.value", :key="btn.icon", "flat", size="sm", :icon="btn.icon",
        @click="defaultClick(btn, props)") {{ $t(btn.title) }}
Anton's avatar
Anton committed
39 40 41
</template>

<script>
Anton's avatar
Anton committed
42
  import PromiseSpan from '../elements/PromiseSpan'
43 44
  import { mapGetters } from 'vuex'
  import { userHasFeature } from 'mbjs-quasar/src/lib'
Anton's avatar
Anton committed
45
  // import { DateTime } from 'luxon'
Anton's avatar
Anton committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  export default {
    props: ['config', 'path', 'query', 'title', 'basePath', 'hasShow', 'requestTransform'],
    components: {
      PromiseSpan
    },
    data () {
      return {
        // basePath: 'timelines',
        rowsPerPage: [0],
        pagination: {
          sortBy: 'title',
          descending: false,
          page: 1,
          rowsPerPage: 0
          // rowsNumber: 0
        },
        hideBottom: true,
        selection: 'none',
        selected: [],
        filter: '',
        loading: false,
        rows: [],
        cols: []
      }
    },
    methods: {
      updateConfig () {
        if (!this.config || !this.config.columns) return
        this.pagination = Object.assign(this.pagination, this.config.pagination)
        const _this = this
        const makeFormatter = function (field) {
          if (field === 'actions') {
            return () => {
79 80 81 82
              return _this.config.actions.filter(action => {
                if (action.feature) return _this.user && userHasFeature(_this.user, action.feature)
                return true
              })
Anton's avatar
Anton committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            }
          }
          return undefined
        }
        const cols = this.config.columns.map(column => {
          column = Object.assign({
            name: !column.name && typeof column.field === 'string' ? column.field : undefined,
            align: 'left',
            filter: true,
            sortable: true,
            sort: (a, b) => {
              if (typeof a === 'string' && typeof b === 'string') {
                return a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase())
              }
              if (a < b) return -1
              if (a > b) return 1
              return 0
            },
            type: 'string'
          }, column)
          if (!column.format) {
            column.format = makeFormatter(column.field)
          }
          return column
        })
        cols.push({
          name: 'actions',
          align: 'right',
          type: 'string',
          filter: false,
          sortable: false,
          sort: false,
          format: makeFormatter('actions')
        })
        this.cols = cols
      },
      async defaultClick (btn, props) {
        if (btn.click) return btn.click(props.row)
        if (btn.type === 'delete') {
          this.loading = true
          try {
124
            await this.$store.dispatch(`${this.path}/delete`, props.row._uuid)
Anton's avatar
Anton committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          }
          catch (err) {
            if (typeof this.$captureException === 'function') this.$captureException(err)
            else console.error(err)
          }
          this.loading = false
          await this.request()
        }
      },
      getViewLink (id) {
        return `${this.basePath}/${id}`
      },
      async request (/* { pagination, filter } */) {
        this.loading = true
        let data = { items: undefined }
        try {
          if (typeof this.config.request === 'function') {
            data.items = await this.config.request(/* { pagination, filter } */)
          }
          if (!data.items) data = await this.$store.dispatch(`${this.path}/find`, this.query)
          if (!data.items) {
            this.loading = false
            return
          }
          // _this.rowsNumber = data.items.length
          if (typeof this.requestTransform === 'function') {
            data.items = await this.requestTransform(data.items)
          }
          if (Array.isArray(data.items)) {
Anton's avatar
Anton committed
154
            this.rows = data.items.map(r => Object.assign(r, { actions: '' }))
Anton's avatar
Anton committed
155
          }
Anton's avatar
Anton committed
156 157
        }
        catch (err) {
Anton's avatar
Anton committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
          if (typeof this.$captureException === 'function') this.$captureException(err)
          else console.error(err)
        }
        this.loading = false
      },
      action (type, target) {
        this.$emit('action', type, target)
      }
    },
    async mounted () {
      this.updateConfig()
      await this.request({ pagination: this.pagination, filter: this.filter })
    },
    computed: {
172 173 174
      ...mapGetters({
        user: 'auth/getUserState'
      }),
Anton's avatar
Anton committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
      tableTitle () {
        return this.$t(this.title)
      }
    },
    watch: {
      selected () {
        console.debug('selected', this.selected)
      },
      config () {
        this.updateConfig()
      }
    }
  }
</script>
189 190 191

<style scoped lang="stylus">
</style>