Vue開發實例(十一)用戶列表的實現與操作

用戶列表的實現與操作

  • 一、創建用戶頁面和路由
  • 二、表格優化
    • 1、表頭自定義
    • 2、表格滾動
    • 3、加入數據索引
    • 4、利用插槽自定義顯示
  • 三、功能
    • 1、查詢功能
    • 3、增加
    • 4、刪除
    • 5、修改

一、創建用戶頁面和路由

  1. 創建用戶頁面

src/components/Main 下創建文件夾user,創建文件UserList.vue,文件內容如下:

頁面中數據的請求代碼:

  • 定義方法getUserData,使用axios 去請求數據,并將請求的數據設置給userData。
  • 在created鉤子函數中調用getUserData方法。
<template><el-table:data="userData"style="width: 100%"border:default-sort="{ prop: 'date', order: 'descending' }"><el-table-column prop="date" label="日期" sortable width="180"></el-table-column><el-table-column prop="name" label="姓名" sortable width="180"></el-table-column><el-table-column prop="address" label="地址"> </el-table-column></el-table>
</template><script>
export default {name: "UserList",data() {return {userData: [],};},methods: {getUserData() {this.$axios.post("/post/userList").then((res) => {this.userData = res.data.userData;});},},created() {this.getUserData();},
};
</script><style scoped>
.el-table {width: 100%;height: auto;
}
</style>
  1. 在之前使用的mockjs中,將菜單數據進行修改,然后加入用戶管理的數據
Mock.mock('/post/menuList', 'get', function () {const menu_data = [{name: '用戶管理',icon: 'el-icon-user',path: '/index/userList',component: 'user/UserList'},{name: '一級菜單1',icon: 'el-icon-location',path: '/index/menu1',component: 'Main1'},{name: '一級菜單2',icon: 'el-icon-document',path: '/index/menu2',component: 'Main2'}]return {menu_data}
});Mock.mock('/post/userList', 'post', function () {const userData = [{date: "2022-05-02",name: "牛3號",address: "北京市XXXXXX路1號",},{date: "2022-05-04",name: "牛4號",address: "北京市XXXXXX路2號",},{date: "2022-05-01",name: "牛5號",address: "北京市XXXXXX路3號",},]return {userData}
});

頁面效果
在這里插入圖片描述

二、表格優化

1、表頭自定義

  1. el-table 增加屬性 header-row-class-name 值為 table_header_class
  2. 設置樣式,注意使用:/deep/ .table-header-class th

參考代碼:

<template><el-table:data="userData"style="width: 100%"border:default-sort="{ prop: 'date', order: 'descending' }"header-row-class-name="table-header-class"><el-table-column prop="date" label="日期" sortable width="180"></el-table-column><el-table-column prop="name" label="姓名" sortable width="180"></el-table-column><el-table-column prop="address" label="地址"> </el-table-column></el-table>
</template><script>
export default {name: "UserList",data() {return {userData: [],};},methods: {getUserData() {this.$axios.post("/post/userList").then((res) => {this.userData = res.data.userData;});},},created() {this.getUserData();},
};
</script><style scoped>.el-table {width: 100%;height: auto;
}/deep/ .table-header-class th{background-color: #91a8b1 !important;color: white;
}
</style>

頁面效果
在這里插入圖片描述

2、表格滾動

如果數據過多則需要加入表格滾動,否則是外部滾動很難看,這種情況處理起來比較簡單,在el-table加入max-height屬性即可,比如我設置值為500px
在這里插入圖片描述

效果展示
在這里插入圖片描述

3、加入數據索引

加入數據索引
給表格加上一列即可,設置 type=“index”

<el-table-column type="index" width="50"></el-table-column>

在這里插入圖片描述

4、利用插槽自定義顯示

讓數據看起來更顯眼、分類更明確,比如 userData 加了tag,表示地址是工作地址,還是家

<el-table-column prop="tag" label="標簽"></el-table-column>

頁面效果
在這里插入圖片描述
只加個標簽的話,很平淡,我們用插槽試一下

<el-table-column prop="tag" label="標簽"><template slot-scope="scope"><el-tag:type="scope.row.tag === '家' ? 'primary' : 'success'"disable-transitions>{{ scope.row.tag }}</el-tag></template></el-table-column>

頁面效果
在這里插入圖片描述

三、功能

1、查詢功能

  1. 頁面添加搜索框和搜索按鈕,在table的前面加入代碼
  2. 在data里面定義search_name,對應el-input的v-model

userList代碼

<template><div id="button_div"><el-inputid="search-input"size="small"label-width="100px"prefix-icon="el-icon-search"placeholder="請輸入名字..."v-model="search_name"style="height: auto; width: 20%"></el-input>&nbsp;<el-buttontype="primary"size="small"class="search-button"@click="search"style="height: auto">搜索</el-button><el-table:data="userData"style="width: 100%"border:default-sort="{ prop: 'date', order: 'descending' }"header-row-class-name="table-header-class"class="custom-table-height"max-height="500px"><el-table-column type="index" width="50"> </el-table-column><el-table-column prop="date" label="日期" sortable width="180"></el-table-column><el-table-column prop="name" label="姓名" sortable width="180"></el-table-column><el-table-column prop="address" label="地址"> </el-table-column><el-table-column prop="tag" label="標簽"><template slot-scope="scope"><el-tag:type="scope.row.tag === '家' ? 'primary' : 'success'"disable-transitions>{{ scope.row.tag }}</el-tag></template></el-table-column></el-table></div>
</template><script>
export default {name: "UserList",data() {return {userData: [],search_name: "",};},methods: {getUserData() {this.$axios.post("/post/userList").then((res) => {this.userData = res.data.userData;});},search() {console.log('this.search_name',this.search_name)this.$axios.post("/post/searchUser", { name: this.search_name }).then((res) => {console.log('name===',name)this.userData = res.data.userData;});},},created() {this.getUserData();},
};
</script><style scoped>
/* .el-table {width: 100%;height: 300px !important;
} *//deep/ .table-header-class th {background-color: #91a8b1 !important;color: white;
}
.custom-table-height {height: auto !important; /* 或指定一個固定高度,例如 400px */
}
</style>

mockjs代碼

const userData = [{date: "2022-05-02",name: "牛3號",address: "北京市XXXXXX路1號",tag: '家'},{date: "2022-05-04",name: "牛4號",address: "北京市XXXXXX路2號",tag: '家'},{date: "2022-05-01",name: "牛5號",address: "北京市XXXXXX路3號",tag: '家'},
]
Mock.mock('/post/searchUser', 'post', function (param) {console.log('param',param.body);let body = JSON.parse(param.body);let name = body.name;let newData = []if (name) {newData = userData.filter((item) => {console.log('*******',item.name.indexOf(name))return item.name.indexOf(name) > -1;})console.log('eeee',newData)} else {newData = userData;console.log('eeee2',newData)}return {userData: newData}
});

測試如下
在這里插入圖片描述

3、增加

4、刪除

5、修改


休息一會,繼續補充~

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/716389.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/716389.shtml
英文地址,請注明出處:http://en.pswp.cn/news/716389.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java ZooKeeper-RocketMQ 面試題

Java ZooKeeper-RocketMQ 面試題 前言1、談談你對ZooKeeper的理解 &#xff1f;2、Zookeeper的工作原理&#xff08;Zab協議&#xff09;3、談談你對分布式鎖的理解&#xff0c;以及分布式鎖的實現&#xff1f;4、 zookeeper 是如何保證事務的順序一致性的&#xff1f;5、 zook…

設計模式之策略模式詳解

目錄 什么是策略模式 應用場景 業務場景實現 抽象類 實現類 Context上下文 測試類 策略模式的優缺點 什么是策略模式 他將定義的算法家族、分別封裝起來&#xff0c;讓他們之間可以相互替換&#xff0c;從而讓算法的變化不會影響到使用算法的用戶。 策略模式使用的就是…

idea出現莫名其妙錯的時候

正常情況idea使用起來都很順手&#xff0c;但是當項目比較多的時候&#xff0c;可能出現莫名奇妙的錯誤&#xff0c;比如導入的包始終報錯java: 程序包com不存在&#xff0c;或者引入自己寫的包也不存在&#xff0c;或者始終出現紅線但是排查之后沒有問題的情況&#xff0c;這種…

進來吧,給自己10分鐘,這篇文章帶你直接學會python

Python的語言特性 Python是一門具有強類型(即變量類型是強制要求的)、動態性、隱式類型(不需要做變量聲明)、大小寫敏感(var和VAR代表了不同的變量)以及面向對象(一切皆為對象)等特點的編程語言。 獲取幫助 你可以很容易的通過Python解釋器獲取幫助。如果你想知道一個對象(o…

OJ:鏈表的中間結點

876. 鏈表的中間結點 - 力扣&#xff08;LeetCode&#xff09; 思路 思路&#xff1a;首先最容易想到的思路是什么呢&#xff0c;就是先遍歷一遍鏈表&#xff0c;用一個值count來記錄鏈表的長度&#xff0c;然后我們運用除法&#xff0c;/2&#xff0c;結果是幾&#xff0c;就…

【C++干貨基地】揭秘C++11常用特性:內聯函數 | 范圍for | auto自動識別 | nullptr指針空值

&#x1f3ac; 鴿芷咕&#xff1a;個人主頁 &#x1f525; 個人專欄: 《C干貨基地》《粉絲福利》 ??生活的理想&#xff0c;就是為了理想的生活! 引入 哈嘍各位鐵汁們好啊&#xff0c;我是博主鴿芷咕《C干貨基地》是由我的襄陽家鄉零食基地有感而發&#xff0c;不知道各位的…

平臺工程: 用Backstage構建開發者門戶 - 2

本文介紹了如何使用開源Backstage構建自己的開發者門戶&#xff0c;并基于此實踐平臺工程。本系列共兩篇文章&#xff0c;這是第二篇。原文: Platform Engineering: Building Your Developer Portal with Backstage — Part 2 在本教程第一部分中我們了解了Backstage這個用于構…

外貿網站模板建站

測繪檢測wordpress外貿主題 簡潔實用的wordpress外貿主題&#xff0c;適合做測繪檢測儀器設備的外貿公司使用。 https://www.jianzhanpress.com/?p5337 白馬非馬衣服WordPress外貿建站模板 白馬非馬服裝行業wordpress外貿建站模板&#xff0c;適用于時間服裝企業的官方網站…

Git 如何上傳本地的所有分支

Git 如何上傳本地的所有分支 比如一個本地 git 倉庫里定義了兩個遠程分支&#xff0c;一個名為 origin&#xff0c; 一個名為 web 現在本地有一些分支是 web 遠程倉庫沒有的分支&#xff0c;如何將本地所有分支都推送到 web 這個遠程倉庫上呢 git push web --all

pytorch loss函數整理

變量名解釋 logits&#xff1a;未經過normalize&#xff08;未經過激活函數處理&#xff09;的原始分數&#xff0c;例如一個mlp將特征映射到num_target_class維的輸出tensor就是logits。 probs&#xff1a;probabilities的簡寫&#xff0c;logits經過sigmoid函數&#xff0c;…

Doris實戰——銀聯商務實時數倉構建

目錄 前言 一、應用場景 二、OLAP選型 三、實時數倉構建 四、實時數倉體系的建設與實踐 4.1 數倉分層的合理規劃 4.2 分桶分區策略的合理設置 4.3 多源數據遷移方案 4.4 全量與增量數據的同步 4.5 離線數據加工任務遷移 五、金融級數倉穩定性最佳實踐 5.1 多租戶資…

Jenkins的Pipeline概念

文章目錄 Pipeline什么是Jenkins Pipeline聲明式和腳本式Pipeline語法為何使用PipelinePipeline概念PipelineNodeStageStep Pipeline語法概述聲明式Pipeline腳本式Pipeline Pipeline示例 參考 Pipeline 什么是Jenkins Pipeline Jenkins Pipeline是一套插件&#xff0c;它支持…

【Django】model模型—模型繼承

Django中三種繼承風格 抽象基類&#xff1a;僅將父類用于子類公共信息的載體&#xff0c;這樣的父類永遠都不會單獨使用。多表繼承&#xff1a;繼承了一個模型&#xff08;可能來源其它應用&#xff09;&#xff0c;且想要每個模型都有對應的數據表。代理模型&#xff1a;只想…

JProfiler相關問題及答案(2024)

1、JProfiler是什么及其用途 JProfiler是一款功能豐富的商業Java性能剖析&#xff08;profiling&#xff09;工具&#xff0c;它主要面向開發者和性能分析師&#xff0c;用于監測和分析Java應用程序的運行時行為。以下是對JProfiler的一些詳細介紹和它的主要用途&#xff1a; …

webpack的一些知識

核心 webpack 是用來搭建前端工程的它運行在node環境中&#xff0c;它所做的事情&#xff0c;簡單來說&#xff0c;就是打包具體來說&#xff0c;就是以某個模塊作為入口&#xff0c;根據入口分析出所有模塊的依賴關系&#xff0c;然后對各種模塊進行合并、壓縮&#xff0c;形…

洛谷P1157 組合的輸出

深搜板子加一點點修改&#xff0c;適合初學者體會深搜&#xff0c;具體看代碼 題目鏈接 ACcode #include<bits/stdc.h>using namespace std;int a, b;bitset<50>vis;//剪枝 int d[50];void dfs(int x) {if (x b 1) {for (int i 1;i < b;i)cout << se…

HBM(High Bandwidth Memory)

選擇正確的高帶寬內存 構建高性能芯片的選擇越來越多&#xff0c;但附加內存的選擇卻幾乎沒有變化。為了在汽車、消費和超大規模計算中實現最大性能&#xff0c;選擇取決于一種或多種 DRAM&#xff0c;而最大的權衡是成本與速度。 盡管多年來人們一直在努力用更快、更便宜或更…

Linux:kubernetes(k8s)搭建mater節點(kubeadm,kubectl,kubelet)(2)

安裝k8有多種方式如&#xff1a; minikube kubeadm 二進制安裝 命令行工具 我這里就使用kubeadm進行安裝 環境 3臺centos7 master ip &#xff1a;192.168.113.120 2G運存 2內核 node1 ip &#xff1a;192.168.113.121 2G運存 2內核 node2 ip &#xff1a;192.168.1…

重構與設計模型的完美融合:構建穩定可擴展系統的關鍵步驟

在軟件開發的漫長旅程中&#xff0c;系統的穩定性和可擴展性一直是開發者們追求的目標。為了實現這一目標&#xff0c;重構和設計模型成為了不可或缺的兩個關鍵元素。本文將探討如何通過重構&#xff0c;使系統更穩定、更具可擴展性&#xff0c;并深入研究如何將重構與設計模型…

JavaEE:多線程(3):案例代碼

目錄 案例一&#xff1a;單例模式 餓漢模式 懶漢模式 思考&#xff1a;懶漢模式是否線程安全&#xff1f; 案例二&#xff1a;阻塞隊列 可以實現生產者消費者模型 削峰填谷 接下來我們自己實現一個阻塞隊列 1.先實現一個循環隊列 2. 引入鎖&#xff0c;實現線程安全 …