summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/ComponentSearchTable.vue
blob: e9101dcb2bee096d5fe5571bc10c85619880668b (plain) (blame)
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
45
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
79
80
81
82
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
124
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<i18n>
{
  "en": {
    "search": "Search",
    "all": "All",
    "pageText": "{0}-{1} of {2}",
    "pageTextZero": "0 of 0",
    "rowsPerPageText": "Rows per page:"
  },
  "de": {
    "search": "Suche",
    "all": "Alle",
    "pageText": "{0}-{1} von {2}",
    "pageTextZero": "0 von 0",
    "rowsPerPageText": "Reihen pro page:"
  }
}
</i18n>

<template>
  <div>
    <v-layout v-if="actionsNeeded" wrap align-center class="actions-container">
      <v-flex md3 sm5 xs12 order-md1 order-sm1 order-xs1 class="text-xs-left">
        <v-text-field
          class="search-field"
          :placeholder="$t('search')"
          v-model="search"
          hide-details
          prepend-inner-icon="search"
        ></v-text-field>
      </v-flex>
      <v-flex md4 sm12 xs12 offset-md1 offset-sm0 offset-xs0 order-md2 order-sm3 order-xs3
              class="text-md-center text-xs-right caption font-weight-thin">
        {{ $t('rowsPerPageText') }}
        <v-select
          class="rows-per-page-select body-1"
          v-model="pagination.rowsPerPage"
          :items="rowsPerPageItems.concat({ text: $t('all'), value: -1 })"
          color="primary"
          hide-details
          :menu-props="{
            offsetY: '',
            contentClass: 'search-table-rows-per-page-select-content'
          }"
        ></v-select>
      </v-flex>
      <v-flex md4 sm6 xs12 offset-md0 offset-sm1 offset-xs0 order-md3 order-sm2 order-xs2
              class="text-xs-right caption font-weight-thin">
        <span class="page-text">{{
          pagination.totalItems > 0 ? $t('pageText', [
            (pagination.page - 1) * pagination.rowsPerPage + 1,
            Math.min(pagination.page * pagination.rowsPerPage, pagination.totalItems),
            pagination.totalItems
          ]) : $t('pageTextZero')
        }}</span>
        <v-btn class="page-button" icon @click="prevPage"><v-icon>keyboard_arrow_left</v-icon></v-btn>
        <v-btn class="page-button" icon @click="nextPage"><v-icon>keyboard_arrow_right</v-icon></v-btn>
      </v-flex>
    </v-layout>
    <v-divider v-if="actionsNeeded"></v-divider>
    <v-data-table
      :headers="headers"
      :items="items"
      :select-all="selectAll"
      :item-key="itemKey"
      :loading="loading"
      v-bind="dataTableProps"
      :value="value"
      @input="$emit('input', $event)"
      hide-actions
      :search="search"
      :pagination.sync="pagination"
      :custom-filter="customFilter"
      @click.native.capture.passive="setShiftState"
    >
      <template v-if="$scopedSlots.items" slot="items" slot-scope="props">
        <slot name="items" :data="props" :color="props.selected && { backgroundColor: $vuetify.theme.primary + '11' }" ></slot>
      </template>
    </v-data-table>
  </div>
</template>

<script>

export default {
  name: 'ComponentSearchTable',
  props: {
    headers: {
      type: Array,
      required: true
    },
    items: {
      type: Array,
      required: true
    },
    value: {
      type: Array,
      default: () => []
    },
    selectAll: {
      type: Boolean,
      default: () => false
    },
    itemKey: {
      type: String,
      default: () => 'id'
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    dataTableProps: {
      type: Object,
      default: () => {}
    },
    rowsPerPageItems: {
      type: Array,
      default: () => [10, 25, 50, 100]
    }
  },
  data () {
    return {
      search: '',
      pagination: {},
      displayedItems: [],
      lastSelectId: null,
      shiftKey: false
    }
  },
  computed: {
    headersValues () { return this.headers.map(x => x.value) },
    headerCount () { return this.headers.length + (this.selectAll ? 1 : 0) },
    rowsPerPage () { return this.pagination.rowsPerPage },
    actionsNeeded () {
      return this.items && this.rowsPerPageItems.length > 0 && this.items.length > this.rowsPerPageItems[0]
    },
    page () { return this.pagination.page }
  },
  watch: {
    rowsPerPage () {
      this.pagination.page = 1
      this.lastSelectId = null
    },
    items () {
      this.pagination.page = 1
      this.lastSelectId = null
    },
    page (value) {
      this.lastSelectId = null
    },
    displayedItems () {
      this.lastSelectId = null
    },
    value (newValue, oldValue) {
      const changedCount = newValue.length - oldValue.length
      var doSelect
      var startId = null
      if (this.shiftKey !== null && changedCount === 1) {
        if (this.shiftKey) startId = this.lastSelectId
        this.lastSelectId = newValue.find(x => !oldValue.includes(x)).id
        doSelect = true
      } else if (this.shiftKey !== null && changedCount === -1) {
        if (this.shiftKey) startId = this.lastSelectId
        this.lastSelectId = oldValue.find(x => !newValue.includes(x)).id
        doSelect = false
      }
      if (startId !== null) {
        var inRange = false
        var tmp = doSelect ? [...this.value] : []
        for (var i = 0; i < this.displayedItems.length; i++) {
          if (!inRange && (this.displayedItems[i].id === startId || (this.displayedItems[i].id === this.lastSelectId))) inRange = true
          else if (inRange && (this.displayedItems[i].id === startId || (this.displayedItems[i].id === this.lastSelectId))) break
          if (inRange && (!doSelect || !tmp.includes(this.displayedItems[i]))) tmp.push(this.displayedItems[i])
        }
        if (!doSelect || !tmp.includes(this.displayedItems[i])) tmp.push(this.displayedItems[i])
        if (!doSelect) tmp = this.value.filter(x => !tmp.includes(x))
        this.shiftKey = null
        this.$emit('input', tmp)
      }
    }
  },
  methods: {
    prevPage () {
      if (this.pagination.page > 1) this.pagination.page -= 1
    },
    nextPage () {
      if (this.pagination.page * this.pagination.rowsPerPage < this.pagination.totalItems) this.pagination.page += 1
    },
    customFilter (items, search) {
      search = String(search).toLowerCase().trim()
      this.displayedItems = search === '' ? items : items.filter(item => {
        return Object.keys(item).some(key => {
          return this.headersValues.includes(key) && String(item[key]).toLowerCase().includes(search)
        })
      })
      this.pagination.totalItems = this.displayedItems.length
      return this.displayedItems
    },
    setShiftState (event) {
      this.shiftKey = event.shiftKey
    }
  },
  created () {
    this.pagination.rowsPerPage = this.rowsPerPageItems[0] || -1
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.actions-container {
  padding: 20px;
}
.search-field {
  margin: 0 10px 5px 10px;
}
.rows-per-page-select {
  display: inline-block;
  margin: 0 10px 0 20px;
  width: min-content;
}
.page-text {
  margin-right: 20px;
}
.page-button {
  margin-top: 0;
  margin-bottom: 0;
}
</style>

<style>
.search-table-rows-per-page-select-content .v-list__tile {
  height: 38px;
}
</style>