summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/ComponentSearchTable.vue
blob: 2e3846d3d2f8e395c128960a54773f022c5716ca (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
<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 })"
          offset-y
          color="primary"
          hide-details
          content-class="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
          v-bind="computedDataTableProps"
          :value="value"
          @input="$emit('input', $event)"
          :search="search"
          :pagination.sync="pagination"
          :custom-filter="customFilter"
    >
      <template v-if="$scopedSlots.items" slot="items" slot-scope="props">
        <slot name="items" :data="props" ></slot>
      </template>
    </v-data-table>
  </div>
</template>

<script>

export default {
  name: 'ComponentSearchTable',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    dataTableProps: {
      type: Object,
      default: () => {}
    },
    rowsPerPageItems: {
      type: Array,
      default: () => [10, 25, 50]
    }
  },
  data () {
    return {
      search: '',
      pagination: {},
      selected: []
    }
  },
  computed: {
    computedDataTableProps () {
      return { ...this.dataTableProps, hideActions: true }
    },
    headersValues () { return this.dataTableProps.headers.map(x => x.value) },
    headerCount () { return this.dataTableProps.headers.length + (this.dataTableProps.selectAll ? 1 : 0) },
    rowsPerPage () { return this.pagination.rowsPerPage },
    actionsNeeded () {
      return this.dataTableProps.items && this.rowsPerPageItems.length > 0 && this.dataTableProps.items.length > this.rowsPerPageItems[0]
    }
  },
  watch: {
    rowsPerPage () {
      this.pagination.page = 1
    }
  },
  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()
      let matchedItems = 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 = matchedItems.length
      return matchedItems
    }
  },
  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-top: 0;
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 5px;
}
.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>