前期回顾
先介绍、在贴码,
Element UI的默认语言是英文。那么如何才能将其改成中文呢?
封装了一个子组件(table、分页),在不同页面引入
分页、表格排序、文字居中、溢出隐藏、操作列、开关、宽、最小宽、type类型(selection/index/expand)、格式化 、不同页面不同操作列、vuex、vue持久化插件、(此处没有接口所以用到,还涉及了query与params传值区别)
说思路:data数据请求接口拿到,表头数据一般也是后台接口,如没,前台可自定义自己写
- //lable指定表头名 //prop指定每一项数据
- <el-table :data="tableData">
- <el-table-column
- :label="item.label"
- :prop="item.prop"
- :key="index" v-for="(item,index) in tableHeader"
- >
- </el-table-column>
- </el-table>
-
-
- //表头
- tableHeader:[
- {label:'姓名' , prop : 'uname'},
- {label:'年龄' , prop : 'age'},
- {label:'性别' , prop : 'sex'},
- ],
-
- //表数据
- tableData:[
- {uname:"小明",age:'20',sex:'男'},
- {uname:"小黑",age:'18',sex:'男'},
- ]
-
-
念及此!那开始正文
- <template>
- <!-- 项目的子组件 封装后的表格、分页 -->
- <!-- 表格数据 Start-->
- <el-table :data="tableData" height="200" style="width: 100%" border>
- <!-- 循环表头 template是不会渲染为dom 在小程序中是block -->
- <template v-for="(item, index) in tableHeader" :key="index">
- <el-table-column
- :prop="item.prop"
- :label="item.label"
- :align="item.align || 'center'"
- :show-overflow-tooltip="item.overHidden || false"
- :min-width="item.minWidth || '100px'"
- :sortable="item.sortable || false"
- :type="item.type || 'normal'"
- :fixed="item.fixed"
- :width="item.width || ''"
- :formatter="stateFormat"
- >
- <!-- 自定义列 开关-->
- <template #default="scope" v-if="item.dataType==='switch'">
- <el-switch
- v-model="scope.row.status"
- active-text="开"
- inactive-text="关"
- active-color="#13ce66"
- inactive-color="#ff4949"
- @change="changeSwitchStatus(scope.row.id, scope.row.status)"
- />
- </template>
-
- <!-- 自定义列 按钮 -->
- <!-- <template #default="scope" v-if="item.dataType==='operate'">
- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
- <el-button
- size="small"
- type="danger"
- @click="handleDelete(scope.$index, scope.row)"
- >Delete</el-button>
- </template>-->
- </el-table-column>
- </template>
-
- <!-- 自定义列 按钮 -->
- <el-table-column fixed="right" label="操作列" :width="operateWidth" v-if="isOperate">
- <template #default="scope">
- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
- <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
- </template>
- </el-table-column>
- </el-table>
- <!-- 表格数据 End-->
-
- <!-- 分页 Start-->
- <el-pagination
- :page-sizes="pageSizesArr"
- :layout="layout"
- :total="total"
- @size-change="handleSizeChange"
- @current-change="handleCurrentChange"
- ></el-pagination>
- <!-- 分页 End-->
- </template>
- <template>
- <!-- 项目的子组件 封装后的表格、分页 -->
- <!-- 表格数据 Start-->
- <el-table :data="tableData" height="200" style="width: 100%" border>
- <!-- 循环表头 template是不会渲染为dom 在小程序中是block -->
- <template v-for="(item, index) in tableHeader" :key="index">
- <el-table-column
- :prop="item.prop"
- :label="item.label"
- :align="item.align || 'center'"
- :show-overflow-tooltip="item.overHidden || false"
- :min-width="item.minWidth || '100px'"
- :sortable="item.sortable || false"
- :type="item.type || 'normal'"
- :fixed="item.fixed"
- :width="item.width || ''"
- >
- <!-- 自定义列 开关-->
- <template #default="scope" v-if="item.dataType==='switch'">
- <el-switch
- v-model="scope.row.status"
- active-text="开"
- inactive-text="关"
- active-color="#13ce66"
- inactive-color="#ff4949"
- @change="changeSwitchStatus(scope.row.id, scope.row.status)"
- />
- </template>
- <!-- 加密手机号 -->
- <template #default="scope" v-if="item.dataType==='phone'">{{encryptionPhone(scope.row)}}</template>
- <!-- 自定义列 按钮 -->
- <template #default="scope" v-if="item.dataType==='operate'">
- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
- <el-button
- size="small"
- type="danger"
- @click="handleDelete(scope.$index, scope.row)"
- >Delete</el-button>
- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
- <el-button
- size="small"
- type="danger"
- @click="handleDelete(scope.$index, scope.row)"
- >Delete</el-button>
- </template>
- </el-table-column>
- </template>
-
- <!-- 自定义列 按钮 -->
- <el-table-column fixed="right" label="操作列" :width="operateWidth" v-if="isOperate">
- <template #default="scope">
- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
- <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
- </template>
- </el-table-column>
- </el-table>
- <!-- 表格数据 End-->
-
- <!-- 分页 Start-->
- <el-pagination
- :page-sizes="pageSizesArr"
- :layout="layout"
- :total="total"
- @size-change="handleSizeChange"
- @current-change="handleCurrentChange"
- ></el-pagination>
- <!-- 分页 End-->
- </template>
-
- <script setup>
- import { defineProps, defineEmits, onMounted, reactive } from "vue";
-
- const emits = defineEmits([
- "handleSizeChange",
- "handleCurrentChange",
- "handleChangeSwitchStatus",
- ]);
- // 表格数据格式化
- const stateFormat = (row, column, cellValue, index) => {
- // console.log("😂👨🏾❤️👨🏼==>: ", row.phone);
- // if (cellValue != null) {
- // const rol = row.phone.slice(0, 3);
- // const ral = row.phone.slice(7, 12);
- // const pho = rol + "****" + ral;
- // return pho;
- // }
- };
- //开关改变事件
- const changeSwitchStatus = (rowId, _boolean) => {
- emits("handleChangeSwitchStatus", rowId, _boolean);
- };
- // 操作列 编辑
- const handleEdit = (index, row) => {
- console.log(" index🚀", index);
- console.log(" row🚀", row);
- };
- // 操作列 删除
- const handleDelete = (index, row) => {
- console.log(" index🚀", index);
- console.log(" row🚀", row);
- };
-
- // 页数改变的时候触发的事件
- const handleSizeChange = (val) => {
- emits("handleSizeChange", val);
- };
- // 当前页改变的时候触发的事件
- const handleCurrentChange = (val) => {
- emits("handleCurrentChange", val);
- };
- // 手机号格式化
- const encryptionPhone = (row) => {
- // console.log(row);
- let phone = row.phone;
- if (phone != null) {
- const rol = phone.slice(0, 3);//用于截取数组,并返回截取到的新的数组,数组与字符串对象都使用(⚠️:对原数组不会改变)
- const ral = phone.slice(7, 12);
- const pho = rol + "****" + ral;
- return pho;
- }
- };
- const props = defineProps({
- // 表格显示的数据
- tableData: {
- type: Array,
- default: function () {
- return [];
- },
- },
- // 表头数据
- tableHeader: {
- type: Array,
- default: function () {
- return [];
- },
- },
- // 控制操作列是否显示
- isOperate: {
- type: Boolean,
- default: function () {
- return false;
- },
- },
- operateWidth: {
- type: Number,
- default: () => 200,
- },
- // 总页数
- total: {
- type: Number,
- // 必传类型
- required: true,
- default: 0,
- },
-
- // 分页的页容量数组
- pageSizesArr: {
- type: Array,
- default() {
- return [10, 20, 30, 50];
- },
- },
- // 分页的布局
- layout: {
- type: String,
- default: "total, sizes, prev, pager, next, jumper",
- },
- });
-
- onMounted(() => {
- // console.log("!这里输出😂👨🏾❤️👨🏼==>: ", props.tableData);
- // console.log("表格🚀", props.tableData, props.tableHeader, props.isOperate);
- // console.log("页容量🚀", props.total);
- });
- </script>
-
- <style lang="scss">
- * {
- margin: 0;
- padding: 0;
- }
- .el-switch__label--left {
- position: relative;
- left: 45px;
- color: #fff;
- z-index: -1111;
- }
-
- .el-switch__core {
- width: 50px !important;
- }
-
- .el-switch__label--right {
- position: relative;
- right: 46px;
- color: #fff;
- z-index: -1111;
- }
-
- .el-switch__label--right.is-active {
- z-index: 1111;
- color: #fff !important;
- }
-
- .el-switch__label--left.is-active {
- z-index: 1111;
- color: #fff !important;
- }
- </style>
- <template>
- <h1 style="color:green; text-align: center;">这里是home父组件 , 他们调用一个子组件</h1>
- <!-- query 刷新页面,地址栏参数不保存,地址栏不可见 必须在router.ts配置name -->
-
- <el-button
- type="primary"
- @click="$router.push({path:'/about',query:{id:666}})"
- >{{$route.params.id}}----跳转 about</el-button>
- <!-- 子组件 Start -->
- <comm-table-pagination
- :tableData="tableData"
- :tableHeader="tableHeader"
- :isOperate="isOperate"
- :total="total"
- :operateWidth="operateWidth"
- @handleSizeChange="onHandleSizeChange"
- @handleCurrentChange="onHandleCurrentChange"
- >
- <template v-slot:btn>
- <el-button type="success" size="small" @click="$router.push('/about')">跳转 about页面</el-button>
- <el-button link type="success" size="small">Edit</el-button>
- <el-button link type="success" size="small">Edit</el-button>
- <el-button link type="success" size="small">Edit</el-button>
- <el-button link type="success" size="small">Edit</el-button>
- </template>
- </comm-table-pagination>
- <!-- 子组件 End -->
- </template>
-
- <script setup>
- import commTablePagination from "@/components/commTable.vue";
- import { reactive, ref, onMounted } from "vue";
-
- // 表格所需的数据
- const tableData = reactive([
- {
- id: 1,
- title: "我是title活在风浪里我是title活在风浪里",
- status: "启动",
- docAuthor: "zhangshan",
- submitTime: "2020-06-06 23:58:49",
- },
- {
- id: 2,
- title: "我是title活在风浪里我是title活在风浪里",
- status: "禁用",
- docAuthor: "zhangshan",
- submitTime: "2020-06-07 00:10:59",
- },
- {
- title: "我是title活在风浪里我是title活在风浪里",
- status: "启动",
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- },
- {
- title: "我是title活在风浪里我是title活在风浪里",
- status: "启动",
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- },
- {
- title: "我是title活在风浪里我是title活在风浪里",
- status: "启动",
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- },
- {
- title: "我是title活在风浪里我是title活在风浪里",
- status: "启动",
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- },
- ]);
- // 表头数据 辅助tableData的数据 这里是自己定义
-
- const tableHeader = reactive([
- {
- type: "index",
- label: "#",
- fixed: true,
- width: "60px", // 宽度 不指定会 自适应屏幕铺满
- },
- {
- prop: "title",
- label: "稿件名称",
- overHidden: true, // 超出显示省略号 比如某一行字数多,缩小屏幕就省略号显示
- sortable: true,
- },
- {
- prop: "status",
- label: "稿件状态",
- sortable: true,
- },
- {
- prop: "docAuthor",
- label: "作者",
- },
- {
- prop: "submitTime",
- label: "提交日期",
- sortable: true, // 是否可排序
- align: "left", // 对齐方式
- width: "120px", // 最小宽度
- width: "200px",
- },
- ]);
- const operateWidth = ref(160); // 操作列宽度
- const isOperate = ref(true); // 操作列是否显示
- const pageSize = ref(10); // 每页显示条数
- const pageNum = ref(2); // 当前页码
- const total = ref(100); // 总条数
- // 分页改变事件
- const onHandleSizeChange = (val) => {
- pageSize.value = val;
- // getData(); 以上数据来自接口,这里是模拟数据,如果是真实数据页数变了,需要再次调用请求后台的接口,重新刷新页面
- };
- // 当前页改变事件
- const onHandleCurrentChange = (val) => {
- pageNum.value = val;
- // getData(); 以上数据来自接口,这里是模拟数据,如果是真实数据页数变了,需要再次调用请求后台的接口,重新刷新页面
- };
- </script>
- <template>
- <h1 style="color:red; text-align: center;">这里是about 父组件 , 他们调用一个子组件</h1>
- <!-- params 刷新页面,地址栏参数不保存,不可见 必须在router.ts设置name-->
- <el-button
- type="primary"
- @click="$router.push({ name: 'home', params: {id:'1'}}) "
- >{{$route.query.id}}----跳转 home</el-button>
-
- <p>{{ $store.state.count }}</p>
- <el-button type="primary" @click="add">点击++</el-button>
- <!-- 子组件 Start -->
- <comm-table-pagination
- :tableData="$store.state.tableData"
- :tableHeader="tableHeader"
- :isOperate="isOperate"
- :total="total"
- :operateWidth="operateWidth"
- @handleSizeChange="onHandleSizeChange"
- @handleChangeSwitchStatus="onHandleChangeSwitchStatus"
- ></comm-table-pagination>
- <!-- 子组件 End -->
- </template>
-
- <script setup>
- import commTablePagination from "@/components/commTable.vue";
- import { reactive, ref, onMounted } from "vue";
- import { useStore } from "vuex";
-
- let store = useStore();
-
- // 表格所需的数据
- let tableData = reactive([
- {
- id: 1,
- title: "我是title活在风浪里我是title活在风浪里",
- status: true,
- docAuthor: "zhangshan",
- submitTime: "2020-06-06 23:58:49",
- phone: "11288888888",
- },
- {
- id: 2,
- title: "我是title活在风浪里我是title活在风浪里",
- status: true,
- docAuthor: "zhangshan",
- submitTime: "2020-06-07 00:10:59",
- phone: "15888888888",
- },
- {
- id: 3,
- title: "我是title活在风浪里我是title活在风浪里",
- status: false,
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- phone: "16588888888",
- },
- {
- id: 4,
- title: "我是title活在风浪里我是title活在风浪里",
- status: true,
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- phone: "18588888888",
- },
- {
- id: 5,
- title: "我是title活在风浪里我是title活在风浪里",
- status: false,
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- phone: "16988888888",
- },
- {
- id: 6,
- title: "我是title活在风浪里我是title活在风浪里",
- status: false,
- docAuthor: "zhangshan",
- submitTime: "2020-06-11 08:07:26",
- phone: "17688888888",
- },
- ]);
- // 表头数据 辅助tableData的数据 这里是自己定义
-
- let tableHeader = reactive([
- {
- type: "index",
- label: "#",
- fixed: true,
- width: "60px", // 宽度 不指定会 自适应屏幕铺满
- },
- {
- prop: "title",
- label: "稿件名称",
- overHidden: true, // 超出显示省略号 比如某一行字数多,缩小屏幕就省略号显示
- sortable: true,
- },
- {
- prop: "status",
- label: "稿件状态",
- dataType: "switch", // 自定义列类型
- },
- {
- prop: "docAuthor",
- label: "作者",
- },
- {
- prop: "submitTime",
- label: "提交日期",
- sortable: true, // 是否可排序
- align: "left", // 对齐方式
- minWidth: "120px", // 最小宽度
- width: "200px",
- },
- {
- prop: "phone",
- label: "手机号",
- dataType: "phone", // 自定义列类型
- },
- {
- label: "操作",
- sortable: true, // 是否可排序
- minWidth: "120px", // 最小宽度
- width: "300px",
- dataType: "operate", // 自定义列类型 在子组件判断如果是operate就显示操作按钮
- fixed: "right", // 固定列
- },
- ]);
- let operateWidth = ref(300); // 操作列宽度
- let isOperate = ref(false); // 操作列是否显示 这个页面用的操作列第二种方式 指定数据,在循环中添加,关闭默认操作列isOperate
- let pageSize = ref(10); // 每页显示条数
- let pageNum = ref(2); // 当前页码
- let total = ref(100); // 总条数
-
- // 分页改变事件
- const onHandleSizeChange = (val) => {
- pageSize.value = val;
- // getData(); 以上数据来自接口,这里是模拟数据,如果是真实数据页数变了,需要再次调用请求后台的接口,重新刷新页面
- };
- // 当前页改变事件
- const onHandleCurrentChange = (val) => {
- pageNum.value = val;
- // getData(); 以上数据来自接口,这里是模拟数据,如果是真实数据页数变了,需要再次调用请求后台的接口,重新刷新页面
- };
- //
- const onHandleChangeSwitchStatus = (rowId, parameter) => {
- tableData.map((v) => {
- if (v.id === rowId) {
- v.status = parameter;
- }
- });
- store.commit("setTableData", tableData);
- };
- function add() {
- store.commit("increment");
- }
- onMounted(() => {
- if (localStorage.getItem("vuex")) {
- store.commit(
- "setTableData",
- JSON.parse(localStorage.getItem("vuex")).tableData
- );
- } else {
- store.commit("setTableData", tableData);
- }
- });
- </script>
到此基本已经可以了,可根据实际变化,在变动一下