feat: Users: add query param for current page, refactor code

This commit is contained in:
woothu
2020-02-20 11:57:09 +01:00
parent f3a1e2269f
commit c68263b85f
3 changed files with 66 additions and 41 deletions
+13 -6
View File
@@ -111,21 +111,28 @@ function configRoutes () {
}, },
{ {
path: 'users', path: 'users',
meta: { label: 'Users'}, meta: {
label: 'Users'
},
component: { component: {
render (c) { return c('router-view') } render(c) {
return c('router-view')
}
}, },
children: [ children: [
{ {
path: '', path: '',
component: Users, name: 'Users',
component: Users
}, },
{ {
path: ':id', path: ':id',
meta: { label: 'User Details'}, meta: {
label: 'User Details'
},
name: 'User', name: 'User',
component: User, component: User
}, }
] ]
}, },
{ {
+20 -8
View File
@@ -10,8 +10,8 @@
striped striped
small small
fixed fixed
:items="getUserData($route.params.id)" :items="visibleData"
:fields="$options.fields" :fields="fields"
/> />
</CCardBody> </CCardBody>
<CCardFooter> <CCardFooter>
@@ -26,16 +26,28 @@
import usersData from './UsersData' import usersData from './UsersData'
export default { export default {
name: 'User', name: 'User',
fields: [ computed: {
{ key: 'key', _style: 'width:150px' }, fields () {
{ key: 'value' , _style: 'width:150px;' } return [
], {
methods: { key: 'key',
getUserData (id) { label: this.userData.filter(param => param.key === 'username')[0].value,
_style: 'width:150px'
},
{ key: 'value', label: '', _style: 'width:150px;' }
]
},
userData () {
const id = this.$route.params.id
const user = usersData.find((user, index) => index + 1 == id) const user = usersData.find((user, index) => index + 1 == id)
const userDetails = user ? Object.entries(user) : [['id', 'Not found']] const userDetails = user ? Object.entries(user) : [['id', 'Not found']]
return userDetails.map(([key, value]) => { return { key, value } }) return userDetails.map(([key, value]) => { return { key, value } })
}, },
visibleData () {
return this.userData.filter(param => param.key !== 'username')
}
},
methods: {
goBack() { goBack() {
this.$router.go(-1) this.$router.go(-1)
} }
+33 -27
View File
@@ -12,18 +12,11 @@
striped striped
:items="items" :items="items"
:fields="fields" :fields="fields"
:items-per-page="perPage" :items-per-page="5"
@row-clicked="rowClicked"
:pagination="$options.paginationProps"
index-column
clickable-rows clickable-rows
:active-page="activePage"
@row-clicked="rowClicked"
> >
<template #username="data">
<td>
<strong>{{data.item.username}}</strong>
</td>
</template>
<template #status="data"> <template #status="data">
<td> <td>
<CBadge :color="getBadge(data.item.status)"> <CBadge :color="getBadge(data.item.status)">
@@ -32,6 +25,13 @@
</td> </td>
</template> </template>
</CDataTable> </CDataTable>
<CPagination
align="center"
:double-arrows="false"
:active-page="activePage"
:pages="5"
@update:activePage="pageChange"
/>
</CCardBody> </CCardBody>
</CCard> </CCard>
</transition> </transition>
@@ -43,37 +43,43 @@
import usersData from './UsersData' import usersData from './UsersData'
export default { export default {
name: 'Users', name: 'Users',
data: () => { data () {
return { return {
items: usersData, items: usersData,
fields: [ fields: [
{ key: 'username', label: 'Name' }, { key: 'username', label: 'Name', _classes: 'font-weight-bold' },
{ key: 'registered' }, { key: 'registered' },
{ key: 'role' }, { key: 'role' },
{ key: 'status' } { key: 'status' }
], ],
perPage: 5, activePage: 1
} }
}, },
paginationProps: { watch: {
align: 'center', $route: {
doubleArrows: false, immediate: true,
previousButtonHtml: 'prev', handler (route) {
nextButtonHtml: 'next' if (route.query && route.query.page) {
this.activePage = Number(route.query.page)
}
}
}
}, },
methods: { methods: {
getBadge (status) { getBadge (status) {
return status === 'Active' ? 'success' switch (status) {
: status === 'Inactive' ? 'secondary' case 'Active': return 'success'
: status === 'Pending' ? 'warning' case 'Inactive': return 'secondary'
: status === 'Banned' ? 'danger' : 'primary' case 'Pending': return 'warning'
}, case 'Banned': return 'danger'
userLink (id) { default: 'primary'
return `users/${id.toString()}` }
}, },
rowClicked (item, index) { rowClicked (item, index) {
const userLink = this.userLink(index + 1) this.$router.push({path: `users/${index + 1}`})
this.$router.push({path: userLink}) },
pageChange (val) {
this.$router.push({ query: { page: val }})
} }
} }
} }