Skip to content

Instantly share code, notes, and snippets.

@iFwu
Created July 21, 2017 10:36
Show Gist options
  • Save iFwu/a758b5ad72e4cff5a390f4a84b4e1fa0 to your computer and use it in GitHub Desktop.
Save iFwu/a758b5ad72e4cff5a390f4a84b4e1fa0 to your computer and use it in GitHub Desktop.
<template>
<table>
<thead>
<tr>
<th
v-for="key in columns"
:key="key"
@click="sortBy(key)"
:class="{ active: sortKey == key }"
>
{{ key | capitalize }}
<span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
</span>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(entry, index) in filteredData" :key='entry'>
<td v-for="key in columns" :key='key'>
{{entry[key]}}
</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
name: 'qrcode-table',
props: {
data: Array,
columns: Array,
filterKey: String
},
data () {
var sortOrders = {}
this.columns.forEach(function (key) {
sortOrders[key] = 1
})
return {
sortKey: '',
sortOrders: sortOrders
}
},
computed: {
filteredData: function () {
var sortKey = this.sortKey
var filterKey = this.filterKey && this.filterKey.toLowerCase()
var order = this.sortOrders[sortKey] || 1
var data = this.data
if (filterKey) {
data = data.filter(function (row) {
return Object.keys(row).some(function (key) {
return String(row[key]).toLowerCase().indexOf(filterKey) > -1
})
})
}
if (sortKey) {
data = data.slice().sort(function (a, b) {
a = a[sortKey]
b = b[sortKey]
return (a === b ? 0 : a > b ? 1 : -1) * order
})
}
return data
},
filter: {
capitalize: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
},
methods: {
sortBy: function (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
}
}
}
}
</script>
<style{{#sass}} lang="scss"{{/sass}}>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment