Paginator.vue 2.17 KB
Newer Older
Janis Streib's avatar
Janis Streib committed
1
2
<template>
  <div class="paginator">
Janis Streib's avatar
Janis Streib committed
3
    <FilterInput v-model="filterValue"></FilterInput>
Janis Streib's avatar
Janis Streib committed
4
    <b-pagination :total-rows="items.length" v-model="currentPage" :per-page="items_per_page"
5
                  v-if="filterValue === '' && items && items.length > items_per_page" align="center">
Janis Streib's avatar
Janis Streib committed
6
7
    </b-pagination>
    <div class="pagination_container">
8
      <slot name="content_skeleton_item" v-if="items == null || items.length === 0"></slot>
Janis Streib's avatar
Janis Streib committed
9
      <slot name="item" v-else
Janis Streib's avatar
Janis Streib committed
10
            v-for="item in slicer"
Janis Streib's avatar
Janis Streib committed
11
12
            v-bind:item="item"/>
    </div>
13
14
15
16
    <div v-if="items.length === 0"
         class="font-italic text-center mt-3">
      <span class="font-italic">{{ no_items_text }}</span><br/>
    </div>
Janis Streib's avatar
Janis Streib committed
17
    <b-pagination :total-rows="items.length" v-model="currentPage" :per-page="items_per_page"
18
                  v-if="filterValue === '' && items && items.length > items_per_page" align="center">
Janis Streib's avatar
Janis Streib committed
19
20
21
22
23
    </b-pagination>
  </div>
</template>

<script>
Janis Streib's avatar
Janis Streib committed
24
25
import FilterInput from '@/components/FilterInput'

Janis Streib's avatar
Janis Streib committed
26
27
export default {
  name: 'Paginator',
Janis Streib's avatar
Janis Streib committed
28
  components: {FilterInput},
Janis Streib's avatar
Janis Streib committed
29
30
  data() {
    return {
Janis Streib's avatar
Janis Streib committed
31
32
      currentPage: 1,
      filterValue: ''
Janis Streib's avatar
Janis Streib committed
33
34
35
36
37
38
39
40
41
42
43
44
45
    }
  },
  props: {
    items: {
      required: true,
      default() {
        return null
      }
    },
    items_per_page: {
      default() {
        return 25
      }
Janis Streib's avatar
Janis Streib committed
46
    },
47
48
49
50
51
    no_items_text: {
      default() {
        return 'Keine Einträge vorhanden'
      }
    },
Janis Streib's avatar
Janis Streib committed
52
53
54
55
    filter_function: {
      type: Function,
      default() {
        return function (item, term) {
56
57
58
          if (typeof term === 'string') {
            term = term.toLowerCase()
          }
Janis Streib's avatar
Janis Streib committed
59
60
          for (const v of Object.values(item)) {
            if (typeof v === 'string') {
61
              if (v.toLowerCase().includes(term)) {
Janis Streib's avatar
Janis Streib committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                return true
              }
            }
          }
          return false
        }
      }
    }
  },
  computed: {
    slicer() {
      if (this.filterValue === '') {
        return this.items.slice((this.currentPage - 1) * this.items_per_page, Math.min(this.items.length, this.currentPage * this.items_per_page))
      }
      return this.items.filter(x => this.filter_function()(x, this.filterValue))
Janis Streib's avatar
Janis Streib committed
77
78
79
80
81
82
83
84
    }
  }
}
</script>

<style scoped>

</style>