DataTable.vue 6.31 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
      .col-3.col-sm-5.col-md-4.col-lg-3.col-xl-3
6
        slot(name="top-buttons", slot-scope="props")
7
      .col-9.col-sm-7.col-md-8.col-lg-9.col-xl-9
Anton's avatar
Anton committed
8
        q-search(v-model="filter", dark, icon="search")
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      q-table.full-width(
        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")
26

27 28
        template(slot="top-left", slot-scope="props")
          slot(name="buttons-left")
Anton's avatar
Anton committed
29

30 31 32 33 34 35 36 37 38 39
        <!--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")
          template(v-if="customTitleLink")
          router-link.primary(:to="getCustomLink(customTitleLink, props.row._uuid)")
            promise-span(:value="props.value")
        template(v-else)
          router-link.primary(v-if="hasShow", :to="getViewLink(props.row._uuid)")
            promise-span(:value="props.value")
          promise-span(v-if="!hasShow", :value="props.value")
40

41 42 43 44 45
        q-td(slot="body-cell-tags", slot-scope="props", :props="props")
          promise-span(:value="props.value")
        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
46 47 48
</template>

<script>
Anton's avatar
Anton committed
49
  import PromiseSpan from '../elements/PromiseSpan'
50 51
  import { mapGetters } from 'vuex'
  import { userHasFeature } from 'mbjs-quasar/src/lib'
Anton's avatar
Anton committed
52
  // import { DateTime } from 'luxon'
Anton's avatar
Anton committed
53
  export default {
54
    props: ['config', 'path', 'query', 'title', 'basePath', 'hasShow', 'requestTransform', 'customTitleLink'],
Anton's avatar
Anton committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    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 () => {
86 87 88 89
              return _this.config.actions.filter(action => {
                if (action.feature) return _this.user && userHasFeature(_this.user, action.feature)
                return true
              })
Anton's avatar
Anton committed
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 124 125 126 127 128 129 130
            }
          }
          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 {
131
            await this.$store.dispatch(`${this.path}/delete`, props.row._uuid)
Anton's avatar
Anton committed
132 133 134 135 136 137 138 139 140 141 142 143
          }
          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}`
      },
144 145 146
      getCustomLink (name, id) {
        return { name: name, params: { uuid: id } }
      },
Anton's avatar
Anton committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      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
164
            this.rows = data.items.map(r => Object.assign(r, { actions: '' }))
Anton's avatar
Anton committed
165
          }
Anton's avatar
Anton committed
166 167
        }
        catch (err) {
Anton's avatar
Anton committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
          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: {
182 183 184
      ...mapGetters({
        user: 'auth/getUserState'
      }),
Anton's avatar
Anton committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
      tableTitle () {
        return this.$t(this.title)
      }
    },
    watch: {
      selected () {
        console.debug('selected', this.selected)
      },
      config () {
        this.updateConfig()
      }
    }
  }
</script>
199 200 201

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