vue2?element?實(shí)現(xiàn)表格點(diǎn)擊詳情返回時(shí)保留查詢參數(shù)的示例代碼
先直觀一點(diǎn),上圖
列表共5條數(shù)據(jù),準(zhǔn)備輸入Author過濾條件進(jìn)行查詢

進(jìn)入查看詳情頁,就隨便搞了個(gè)按鈕 啥都沒調(diào)啦

點(diǎn)擊返回后

一開始準(zhǔn)備用vuex做這個(gè)功能,后來放棄了,想到直接用路由去做可能也不錯(cuò)。有時(shí)間再整一套vuex版的
<!--
* @Author: chenhaoran
* @Date: 2024-03-03 13:44:10
* @LastEditors: chenhaoran
* @LastEditTime: 2024-03-03 23:07:02
-->
<template>
<div class="app-container">
<div class="search-area">
<el-form :inline="true" :model="queryParams" class="demo-form-inline">
<el-form-item label="Author">
<el-input v-model="queryParams.author" placeholder="作者"></el-input>
</el-form-item>
<el-form-item label="Status">
<el-select v-model="queryParams.status" placeholder="狀態(tài)">
<el-option label="發(fā)布" value="published"></el-option>
<el-option label="刪除" value="deleted"></el-option>
<el-option label="草稿" value="draft"></el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查詢</el-button>
<el-button type="primary" @click="reset">重置</el-button>
</el-form-item>
</el-form>
</div>
<el-table
v-loading="listLoading"
:data="list"
element-loading-text="Loading"
border
fit
highlight-current-row
>
<el-table-column align="center" label="ID" width="95">
<template slot-scope="scope">{{ scope.$index }}</template>
</el-table-column>
<el-table-column label="Title">
<template slot-scope="scope">{{ scope.row.title }}</template>
</el-table-column>
<el-table-column label="Author" width="110" align="center">
<template slot-scope="scope">
<span>{{ scope.row.author }}</span>
</template>
</el-table-column>
<el-table-column label="Pageviews" width="110" align="center">
<template slot-scope="scope">{{ scope.row.pageviews }}</template>
</el-table-column>
<el-table-column class-name="status-col" label="Status" width="110" align="center">
<template slot-scope="scope">
<el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
</template>
</el-table-column>
<el-table-column align="center" prop="created_at" label="Display_time" width="200">
<template slot-scope="scope">
<i class="el-icon-time" />
<span>{{ scope.row.display_time }}</span>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button @click="doView(scope.row)" type="text" size="small">查看</el-button>
<el-button type="text" size="small">編輯</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { getList } from '@/api/table'
// import { createNamespacedHelpers } from 'vuex'
// const { mapMutations, mapActions } = createNamespacedHelpers('queryParams')
export default {
filters: {
statusFilter(status) {
const statusMap = {
published: 'success',
draft: 'gray',
deleted: 'danger'
}
return statusMap[status]
}
},
beforeRouteEnter(to, from, next) {
// console.log('beforeRouteEnter:', from);
/**
* 官方文檔是這樣寫明的:
* -- start --
* beforeRouteEnter 守衛(wèi)不能訪問this,因?yàn)槭匦l(wèi)在導(dǎo)航確認(rèn)前被調(diào)用,因此即將登場(chǎng)的新組件還沒被創(chuàng)建。
* 不過,你可以通過傳一個(gè)回調(diào)給 next 來訪問組件實(shí)例。在導(dǎo)航被確認(rèn)的時(shí)候執(zhí)行回調(diào),并且把組件實(shí)例作為回調(diào)方法的參數(shù)
* beforeRouteEnter (to, from, next) {
next(vm => {
// 通過 `vm` 訪問組件實(shí)例
})
}
* -- end --
* 重點(diǎn)是第二句話,說明是有方法給組件實(shí)例修改值的
*/
/** 有問題的寫法
* const data = { testMsg: '測(cè)試信息'}
* const saveData = data
* next(vm => {
* 在這里卡了很久,這樣寫的話,組件created抑或mounted里,可以訪問data的屬性,但卻拿不到beforeRouteEnter中定義的屬性
* //vm.saveData = saveData
* //vm.$set(vm, 'saveData', saveData)
*
* })
*
* 執(zhí)行順序:
* beforeRouteEnter
* beforeCreate
* mounted
* vm
*/
// 有效處理
let obj = {}
if (from.name == 'itemDetail') {
obj = from.params
} else {
obj = {}
}
next(vm => {
/**
* 在這里卡了很久后,嘗試將設(shè)置value寫入methods方法中使用vm來調(diào)用,
* mounted拿不到beforeRouteEnter這里定義的變量,但是它可以訪問vm實(shí)例的變量和方法
* 再將beforeRouteEnter這定義的對(duì)象作為函數(shù)參數(shù)
*/
vm.setFilterParams(obj)
})
},
data() {
return {
list: null,
listLoading: false,
queryParams: {
author: '',
status: ''
},
}
},
created(){
this.fetchData()
},
mounted() {
// if (
// Object.keys(this.$store.state.queryParams.filterParams).length === 0
// ) {
// this.queryParams = {
// // pageNum: 1,
// // pageSize: 10,
// author: '',
// status: ''
// };
// } else {
// this.queryParams = JSON.parse(
// JSON.stringify(this.$store.state.queryParams.filterParams)
// );
// }
},
methods: {
// ...mapActions(["filterCache"]),
setFilterParams(obj) {
this.queryParams = Object.assign({},obj)
this.fetchData()
},
fetchData() {
this.listLoading = true
let queryParams = this.queryParams
getList(queryParams).then(response => {
this.list = response.data.items
this.listLoading = false
})
},
// 查看
doView(row) {
this.$router.push({
/* path與params不可同時(shí)出現(xiàn) */
// path: 'table/itemDetail',
name: 'itemDetail',
params: this.queryParams
})
},
// 查詢
onSubmit() {
// this.$store.dispatch("queryParams/filterCache", this.queryParams);
// this.filterCache(this.queryParams)
this.fetchData()
},
reset() {
this.queryParams = {}
this.fetchData()
}
}
}
</script>上面重點(diǎn)部分就是beforeRouteEnter了:
beforeRouteEnter(to, from, next) {
// console.log('beforeRouteEnter:', from);
/**
* 官方文檔是這樣寫明的:
* -- start --
* beforeRouteEnter 守衛(wèi)不能訪問this,因?yàn)槭匦l(wèi)在導(dǎo)航確認(rèn)前被調(diào)用,因此即將登場(chǎng)的新組件還沒被創(chuàng)建。
* 不過,你可以通過傳一個(gè)回調(diào)給 next 來訪問組件實(shí)例。在導(dǎo)航被確認(rèn)的時(shí)候執(zhí)行回調(diào),并且把組件實(shí)例作為回調(diào)方法的參數(shù)
* beforeRouteEnter (to, from, next) {
next(vm => {
// 通過 `vm` 訪問組件實(shí)例
})
}
* -- end --
* 重點(diǎn)是第二句話,說明是有方法給組件實(shí)例修改值的
*/
/** 有問題的寫法
* const data = { testMsg: '測(cè)試信息'}
* const saveData = data
* next(vm => {
* 在這里卡了很久,這樣寫的話,組件created抑或mounted里,可以訪問data的屬性,但卻拿不到beforeRouteEnter中定義的屬性
* //vm.saveData = saveData
* //vm.$set(vm, 'saveData', saveData)
*
* })
*
* 執(zhí)行順序:
* beforeRouteEnter
* beforeCreate
* mounted
* vm
*/
// 有效處理
let obj = {}
if (from.name == 'itemDetail') {
obj = from.params
} else {
obj = {}
}
next(vm => {
/**
* 在這里卡了很久后,嘗試將設(shè)置value寫入methods方法中使用vm來調(diào)用,
* mounted拿不到beforeRouteEnter這里定義的變量,但是它可以訪問vm實(shí)例的變量和方法
* 再將beforeRouteEnter這定義的對(duì)象作為函數(shù)參數(shù)
*/
vm.setFilterParams(obj)
})
},<!--
* @Author: chenhaoran
* @Date: 2024-03-03 14:59:08
* @LastEditors: chenhaoran
* @LastEditTime: 2024-03-03 22:31:39
-->
<template>
<div class="item-detail">
<el-button @click="handleClick">返回</el-button>
</div>
</template>
<script>
export default {
name: 'itemDetail',
data() {
return {
}
},
created() {
// console.log(this.$route);
},
methods: {
handleClick() {
/**
* go(-1): 原頁面表單中的內(nèi)容會(huì)丟失;
* this.$router.go(-1):后退+刷新;
* this.$router.go(0):刷新;
* this.$router.go(1) :前進(jìn)
*
* back(): 原頁表表單中的內(nèi)容會(huì)保留;在返回界面?zhèn)鬟f參數(shù);
* this.$router.back():后退 ;
* this.$router.back(0) 刷新;
* this.$router.back(1):前進(jìn)
*
*/
this.$router.back()
}
},
watch: {
}
}
</script>
<style>
</style>到此這篇關(guān)于vue2 element 實(shí)現(xiàn)表格點(diǎn)擊詳情,返回時(shí)保留查詢參數(shù)的文章就介紹到這了,更多相關(guān)vue2 element 表格點(diǎn)擊詳情內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue+element-ui 的Table二次封裝的實(shí)現(xiàn)
這篇文章主要介紹了基于Vue+element-ui 的Table二次封裝的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
vue?parseHTML函數(shù)源碼解析start鉤子函數(shù)
這篇文章主要為大家介紹了vue?parseHTML函數(shù)源碼解析start鉤子函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vuex數(shù)據(jù)持久化實(shí)現(xiàn)的思路與代碼
Vuex數(shù)據(jù)持久化可以很好的解決全局狀態(tài)管理,當(dāng)刷新后數(shù)據(jù)會(huì)消失,這是我們不愿意看到的。這篇文章主要給大家介紹了關(guān)于Vuex數(shù)據(jù)持久化實(shí)現(xiàn)的思路與代碼,需要的朋友可以參考下2021-05-05
Vue監(jiān)聽使用方法和過濾器實(shí)現(xiàn)
這篇文章主要介紹了Vue監(jiān)聽使用方法和過濾器實(shí)現(xiàn),過濾器為頁面中數(shù)據(jù)進(jìn)行強(qiáng)化,具有局部過濾器和全局過濾器2022-06-06
vue常用事件v-on:click詳解事件對(duì)象,事件冒泡,事件默認(rèn)行為
這篇文章主要介紹了vue常用事件之v-on:click?以及事件對(duì)象,事件冒泡,事件默認(rèn)行為,其實(shí)v-on后面跟的不止是click事件也可以是其他的事件,用法均相似,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程)
這篇文章主要介紹了15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue中JS動(dòng)畫與Velocity.js的結(jié)合使用
這篇文章主要介紹了Vue中JS動(dòng)畫與Velocity.js的結(jié)合使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Vue Element前端應(yīng)用開發(fā)之界面語言國際化
我們開發(fā)的系統(tǒng),一般可以不用考慮語言國際化的問題,大多數(shù)系統(tǒng)一般是給本國人使用的,而且直接使用中文開發(fā)界面會(huì)更加迅速 一些,不過框架最好能夠支持國際化的處理,以便在需要的時(shí)候,可以花點(diǎn)時(shí)間來實(shí)現(xiàn)多語言切換的處理,使系統(tǒng)具有更廣泛的受眾用戶。2021-05-05

