Paginator.vue 1.86 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"
janis.streib's avatar
janis.streib committed
5
                  v-if="filterValue == '' && items && items.length > items_per_page" align="center">
janis.streib's avatar
janis.streib committed
6
7
8
9
    </b-pagination>
    <div class="pagination_container">
      <slot name="content_skeleton_item" v-if="items == null || items.length == 0"></slot>
      <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
13
            v-bind:item="item"/>
    </div>
    <b-pagination :total-rows="items.length" v-model="currentPage" :per-page="items_per_page"
janis.streib's avatar
janis.streib committed
14
                  v-if="filterValue == '' && items && items.length > items_per_page" align="center">
janis.streib's avatar
janis.streib committed
15
16
17
18
19
    </b-pagination>
  </div>
</template>

<script>
janis.streib's avatar
janis.streib committed
20
21
import FilterInput from '@/components/FilterInput'

janis.streib's avatar
janis.streib committed
22
23
export default {
  name: 'Paginator',
janis.streib's avatar
janis.streib committed
24
  components: {FilterInput},
janis.streib's avatar
janis.streib committed
25
26
  data() {
    return {
janis.streib's avatar
janis.streib committed
27
28
      currentPage: 1,
      filterValue: ''
janis.streib's avatar
janis.streib committed
29
30
31
32
33
34
35
36
37
38
39
40
41
    }
  },
  props: {
    items: {
      required: true,
      default() {
        return null
      }
    },
    items_per_page: {
      default() {
        return 25
      }
janis.streib's avatar
janis.streib committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    },
    filter_function: {
      type: Function,
      default() {
        return function (item, term) {
          for (const v of Object.values(item)) {
            window.console.debug(typeof v)
            if (typeof v === 'string') {
              if (v.includes(term)) {
                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
66
67
68
69
70
71
72
73
    }
  }
}
</script>

<style scoped>

</style>