summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/DataTable.vue
blob: 721f1370f1f9740e09b3ec210da5385bd7326af9 (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
<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 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
          v-model="rowCount"
          :items="[ 5, 10, 20, 50, 100, { text: $t('all'), value: '-1' }]"
          color="primary"
          hide-details
          :menu-props="{
            offsetY: ''
          }"
        ></v-select>
      </v-flex>
    </v-layout>
    <v-divider></v-divider>

      <RecycleScroller
        class="scroller"
        :style="scrollerStyle"
        :items="filterdRows"
        :item-height="48"
        :page-mode="rowCount <= 0"
      >
        <div slot-scope="{ item }" class="table-row"
          :style="item.selected && { backgroundColor: $vuetify.theme.primary + '11' }"
          @click="selectItem(item)"
        >
          <div>
            <v-icon style="cursor: pointer">{{ item.selected ? 'check_box' : 'check_box_outline_blank' }}</v-icon>
          </div>
          <div v-for="header in headers" :key="header.key" :style="{ width: header.width || headerWidthPercent }">
            {{ item.data[header.key] }}
          </div>
        </div>
      </RecycleScroller>

  </div>
</template>

<script>
import DataTableItem from '@/components/DataTableItem'

export default {
  name: 'DataTable',
  components: { DataTableItem },
  props: {
    headers: {
      type: Array
    },
    items: {
      type: Array,
      required: true
    },
    value: {
      type: Array,
      default: () => []
    },
    minWidth: {
      type: Number,
      default: 600
    }
  },
  data () {
    return {
      search: '',
      lastSelectId: null,
      shiftKey: false,
      rowCount: 10
    }
  },
  computed: {
    scrollerStyle () {
      const style = { '--min-table-width': this.minWidth + 'px' }
      if (this.rowCount > 0) style.height = Math.min(this.rowCount, this.filterdRows.length) * 48 + 'px'
      return style
    },
    headerWidthPercent () { return 100/this.headers.length + '%' },
    dataKeys () { return this.headers.map(x => x.key) },
    rows () {
      return this.items.map(item => ({ data: item, selected: false, id: item.id }))
    },
    filterdRows () {
      const search = String(this.search).toLowerCase().trim()
      return search === '' ? this.rows : this.rows.filter(row => {
        return Object.keys(row.data).some(key => {
          return this.dataKeys.includes(key) && String(row.data[key]).toLowerCase().includes(search)
        })
      })
    }
  },
  watch: {
    value (v) {

    },
    items () {
      this.$emit('input', [])
    }
  },
  methods: {
    setShiftState (event) {
      this.shiftKey = event.shiftKey
    },
    selectItem (item) {
      item.selected = !item.selected
      var tmp = this.value
      if (item.selected) {
        tmp.push(item.data)
      } else {
        tmp.splice(tmp.indexOf(item.data), 1)
      }
      this.$emit('input', tmp)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

.scroller-wrapper {
  overflow-x: auto;
}

.scroller {
  overflow-x: auto;
}

.scroller >>> .vue-recycle-scroller__item-wrapper {
  min-width: var(--min-table-width);
}

.table-row {
  height: 48px;
  display: flex;
  align-items: center;
}

.table-row > div {
  padding: 0 24px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.theme--dark .table-row {
  border-bottom: 1px solid hsla(0,0%,100%,.12);
}

.theme--light .table-row {
  border-bottom: 1px solid rgba(0,0,0,.12);
}


.theme--dark .scroller >>> .hover > .table-row {
  background-color: #616161;
}

.theme--light .scroller >>> .hover > .table-row {
  background-color: #eee;
}

</style>