orderBy table

This commit is contained in:
Sebastian Frank 2017-08-25 13:00:32 +02:00
parent cd7a727f0f
commit d59aa8cf0d
No known key found for this signature in database
GPG Key ID: DC2BC5C506EBF6F3
3 changed files with 53 additions and 10 deletions

View File

@ -4,7 +4,10 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th v-for="(c, i) in columns" :key="i">{{ c.heading }}</th> <th v-for="(c, i) in columns" :key="i">
<span @click="orderBy(c.orderBy)" v-if="c.orderBy">{{ c.heading }} V</span>
<span v-else>{{ c.heading }}</span>
</th>
</tr> </tr>
</thead> </thead>
<tbody is="transition-group" name="list"> <tbody is="transition-group" name="list">
@ -40,6 +43,15 @@ export default {
data() { data() {
return { return {
} }
},
methods: {
orderBy(col) {
console.log(col);
this.$emit('sort', {
orderBy: col,
orderDesc: false
});
}
} }
} }
</script> </script>

View File

@ -1,9 +1,9 @@
<template> <template>
<div class="Scroll-Table"> <div class="Scroll-Table">
<my-table :columns="columns" :rows="rows"></my-table> <my-table :columns="columns" :rows="rows" @sort="sort"></my-table>
<div v-if="loadingDelayed">loading...</div> <div v-if="loadingDelayed">loading...</div>
<div style="padding-left: 30%; padding-bottom: 100px;"> <div style="padding-left: 30%; padding-bottom: 100px;">
<span v-show="hasMore" v-if="!loadingDelayed" v-observe-visibility="visibilityChanged">...</span> <span v-show="hasMore || sortTriggered" v-if="!loadingDelayed" v-observe-visibility="visibilityChanged">...</span>
</div> </div>
</div> </div>
</template> </template>
@ -42,6 +42,13 @@ export default {
type: Number, type: Number,
default: 10 default: 10
}, },
orderBy: {
type: String
},
orderDesc: {
type: Boolean,
default: false
},
loading: { loading: {
type: Boolean, type: Boolean,
default: false default: false
@ -79,14 +86,18 @@ export default {
rows: [ ], rows: [ ],
rowsToPush: [ ], rowsToPush: [ ],
currentOffset: this.offset, currentOffset: this.offset,
loadingDelayed: false currentOrderBy: this.orderBy,
currentOrderDesc: this.orderDesc,
loadingDelayed: false,
sortTriggered: false
} }
}, },
methods: { methods: {
visibilityChanged(isVisible, entry) { visibilityChanged(isVisible, entry) {
if (isVisible) { if (isVisible) {
// infinite scrolling // infinite scrolling
this.handler(this.currentOffset, this.limit); this.sortTriggered = false;
this.handler(this.currentOffset, this.limit, this.currentOrderBy, this.currentOrderDesc);
} }
}, },
pushRows() { pushRows() {
@ -97,6 +108,22 @@ export default {
this.pushRows(); this.pushRows();
}, 50); }, 50);
} }
},
clear() {
this.currentOffset = 0;
this.rows = [];
this.rowsToPush = [];
},
sort(e) {
if (e.orderBy == this.currentOrderBy) {
this.currentOrderDesc = !this.currentOrderDesc;
} else {
this.currentOrderDesc = false;
}
this.currentOrderBy = e.orderBy;
console.log(e);
this.sortTriggered = true;
this.clear();
} }
} }
} }

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="Userlist"> <div class="Userlist">
<h1>Userlist</h1> <h1>Userlist</h1>
<scroll-table :columns="columns" :new-rows="newRows" :has-more="hasMore" :loading="loading" :handler="more"></scroll-table> <scroll-table :columns="columns" :new-rows="newRows" :has-more="hasMore" :loading="loading" :handler="more" orderBy="id" :orderDesc="true"></scroll-table>
</div> </div>
</template> </template>
@ -18,11 +18,13 @@ export default {
columns: [ columns: [
{ {
heading: 'ID', heading: 'ID',
prop: 'ID' prop: 'ID',
orderBy: 'id'
}, },
{ {
heading: 'Benutzername', heading: 'Benutzername',
prop: 'Username' prop: 'Username',
orderBy: 'username'
}, },
{ {
heading: 'Render', heading: 'Render',
@ -37,13 +39,15 @@ export default {
} }
}, },
methods: { methods: {
more(offset, limit) { more(offset, limit, orderBy, orderDesc) {
this.loading = true; this.loading = true;
this.$store.dispatch('apiRequest', { this.$store.dispatch('apiRequest', {
endpoint: 'users', endpoint: 'users',
params: { params: {
start: offset, start: offset,
length: limit length: limit,
orderBy,
orderDesc
} }
}) })
.then(rows => { .then(rows => {