当前位置:   article > 正文

vue3后台管理系统封装的普通表格组件_vue3 表格组件

vue3 表格组件

1.普通的表格组件效果

ComtableListR.vue组件

  1. <template>
  2. <div class="tableBox">
  3. <div class="btn-add">
  4. <a-space>
  5. <a-upload v-model:file-list="fileList" v-if="hasImport" name="file" accept=".xls,.xlsx"
  6. action="" :headers="headers" @change="handleChange">
  7. <a-button type="primary"> 导入 </a-button>
  8. </a-upload>
  9. <a-button type="primary" v-if="hasExport" @click="handleAllExport">导出全部</a-button>
  10. <a-button type="primary" v-if="allDel">全部删除</a-button>
  11. <a-button :disabled="dataSource.length === 0 || flagselectedRowKeys" v-if="rowSelection"
  12. @click="delEvent(selecrowdata, 'many')">批量删除</a-button>
  13. <a-button :disabled="dataSource.length === 0 || flagselectedRowKeys" v-if="markread"
  14. @click="handleFlagRead(selecrowdata, 'hasread')">标记已读</a-button>
  15. <a-button type="primary" v-if="operatingButton?.addbtn" @click="editEvent('add')">{{addbtnName?addbtnName:'添加'}}</a-button>
  16. <a-button type="primary" v-if="hasgoback" @click="handlegoback">返回</a-button>
  17. </a-space>
  18. </div>
  19. <a-table :dataSource="dataSource" :columns="columns" :loading="loading" :scroll="{ x: tableOtherobj.scroll }"
  20. :expandIconColumnIndex="expandIconSet.expandIconColumnIndex" :expandIconAsCell="expandIconSet.expandIconAsCell"
  21. @expand="handleexpand" @expandedRowsChange="expandedRowsChange" :row-selection="rowSelection || markread || rowSelection2
  22. ? { ...objrowSelection }
  23. : null
  24. " :pagination="pagination ? objArray.pagination : false">
  25. <template #bodyCell="{ column, record, index }">
  26. <slot name="customtable" :column="column" :record="record"></slot>
  27. <template v-if="column.key === 'operation'">
  28. <span>
  29. <slot name="operation" :column="column" :record="record"></slot>
  30. <a @click="editEvent('download', record)" v-if="operatingButton?.reportdownload">
  31. 报表下载</a>
  32. <a-divider type="vertical" v-if="dividerbutton?.reportdownload" />
  33. <a @click="editEvent('detail', record)" v-if="operatingButton?.detail">
  34. 查看</a>
  35. <a-divider type="vertical" v-if="dividerbutton?.detail" />
  36. <a @click="editEvent('edit', record)" v-if="operatingButton?.edit">修改</a>
  37. <a-divider type="vertical" v-if="dividerbutton?.edit" />
  38. <a @click="delEvent(record)" v-if="operatingButton?.del" style="color: red">删除</a>
  39. <a-divider type="vertical" v-if="dividerbutton?.del" />
  40. </span>
  41. </template>
  42. <template v-if="column.key === 'index'">
  43. <span>{{
  44. `${(objArray.pagination.current - 1) * objArray.pagination.pageSize +
  45. index +
  46. 1
  47. }`
  48. }}</span>
  49. </template>
  50. <template v-if="column.key === objType.typeName && objType.isshow">
  51. <span :style="{
  52. color: objTypecolor.isshow
  53. ? objTypecolor[record[objType.typeName]]
  54. : null,
  55. }">{{ objType[record[objType.typeName]] }}</span>
  56. <!-- <span :style="{color:`#FF0000`}">{{
  57. objType[record[objType.typeName]]
  58. }}</span> -->
  59. </template>
  60. <template v-if="column.key === obj2Type?.typeName && obj2Type?.isshow">
  61. <span>{{ obj2Type[record[obj2Type.typeName]] }}</span>
  62. </template>
  63. </template>
  64. </a-table>
  65. </div>
  66. </template>
  67. <script setup>
  68. import { reactive, ref, watch, toRefs } from "vue";
  69. const props = defineProps({
  70. addbtnName: {
  71. type: String,
  72. default: ''
  73. },
  74. operatingButton: {
  75. //操作按钮
  76. reportdownload: {
  77. type: Boolean,
  78. default: false,
  79. },
  80. edit: {
  81. type: Boolean,
  82. default: true,
  83. },
  84. del: {
  85. type: Boolean,
  86. default: true,
  87. },
  88. detail: {
  89. type: Boolean,
  90. default: false,
  91. },
  92. },
  93. dividerbutton: {
  94. //操作按钮之间的分割线
  95. reportdownload: {
  96. type: Boolean,
  97. default: false,
  98. },
  99. edit: {
  100. type: Boolean,
  101. default: true,
  102. },
  103. del: {
  104. type: Boolean,
  105. default: false,
  106. },
  107. detail: {
  108. type: Boolean,
  109. default: false,
  110. },
  111. },
  112. allDel: {
  113. //按钮:全部删除
  114. type: Boolean,
  115. default: false,
  116. },
  117. columns: {
  118. //表格表头
  119. type: Array,
  120. default: [],
  121. },
  122. formessagedivider: {
  123. type: Boolean,
  124. default: false,
  125. },
  126. hasImport: {
  127. //按钮:导入
  128. type: Boolean,
  129. default: false,
  130. },
  131. hasExport: {
  132. //按钮:导出
  133. type: Boolean,
  134. default: false,
  135. },
  136. markread: {
  137. // 按钮:标记已读
  138. type: Boolean,
  139. default: false,
  140. },
  141. hasDetail: {
  142. //表格详情
  143. type: Boolean,
  144. default: false,
  145. },
  146. tableOtherobj: {
  147. type: Object,
  148. default: {
  149. // hasAddbtn: true,// 表格上是否有添加按钮
  150. // hasDetail: false,//操作中是否有详情
  151. scroll: false, //表格是否有横向滚动,以及x,方向的 值是多少 number
  152. actionwidth: "120",
  153. },
  154. },
  155. hasedit: {
  156. //表格修改
  157. type: Boolean,
  158. default: true,
  159. },
  160. hasdel: {
  161. //表格删除
  162. type: Boolean,
  163. default: true,
  164. },
  165. hasseeprocess: {
  166. //表格查看过程
  167. type: Boolean,
  168. default: false,
  169. },
  170. pagination: {
  171. //分页
  172. type: Boolean,
  173. default: true,
  174. },
  175. // 是否有批量操作
  176. rowSelection: {
  177. type: Boolean,
  178. default: false,
  179. },
  180. rowSelection2: {
  181. // 是否可以选择
  182. type: Boolean,
  183. default: false,
  184. },
  185. hasgoback: {
  186. //返回按钮
  187. type: Boolean,
  188. default: false,
  189. },
  190. searchkey: {
  191. // 查询的字段
  192. type: Object,
  193. default: {},
  194. },
  195. objType: {
  196. //后台返回摸个字段,不能直接显示,而是根据不同值显示对应的其他内容
  197. type: Object,
  198. default: {
  199. isshow: false, //是否显示
  200. typeName: "type", // 属性值 '需要转义的表格字段,当type=1时显示公司级;当typ=2时显示部门级'
  201. 1: "公司级",
  202. 2: "部门级",
  203. },
  204. },
  205. obj2Type: {
  206. //页面需要两个字段都用枚举时
  207. type: Object,
  208. default: {
  209. isshow: false, //是否显示
  210. typeName: "type", // 属性值 '需要转义的表格字段,当type=1时显示公司级;当typ=2时显示部门级'
  211. 1: "公司级",
  212. 2: "部门级",
  213. },
  214. },
  215. objTypecolor: {
  216. type: Object,
  217. default: {
  218. isshow: false, //是否显示
  219. typeName: "monitorDataReportType", // 属性值 '需要设置颜色的字段'
  220. day: "#FF0000",
  221. month: "#FFFF00",
  222. },
  223. },
  224. expandIconSet: {
  225. // 表格嵌套时,那个控制的展开折叠图表的位置
  226. type: Object,
  227. default: {
  228. expandIconColumnIndex: 2, //想让展开图标放在第几列
  229. expandIconAsCell: false, 想让展开图标放在第几列 设置的配套属性
  230. },
  231. },
  232. loading: false, //表格loading
  233. });
  234. const {
  235. objTypecolor,
  236. obj2Type,
  237. operatingButton,
  238. dividerbutton,
  239. formessagedivider,
  240. markread,
  241. columns,
  242. hasImport,
  243. hasExport,
  244. hasDetail,
  245. tableOtherobj,
  246. hasedit,
  247. hasdel,
  248. hasseeprocess,
  249. pagination,
  250. rowSelection,
  251. hasgoback,
  252. searchkey,
  253. objType,
  254. expandIconSet,
  255. } = toRefs(props);
  256. // 发送给父组件的方法
  257. const emits = defineEmits([
  258. "openModel",
  259. "handleDelTable",
  260. "getData",
  261. "FlagRead",
  262. "handleTableRowSelec",
  263. "exportXlsx",
  264. "importXlsx",
  265. ]);
  266. // 批量删除
  267. let flagselectedRowKeys = ref(true);
  268. let selecrowdata = ref();
  269. const objrowSelection = {
  270. // selectedRowKeys: selectedRowKeys,
  271. //selectedRowKeys 选中行的datasource 中元素key的值; selectedRows为所选元素中datasource 是一个数组。
  272. onChange: (selectedRowKeys, selectedRows) => {
  273. let falg = selectedRowKeys.length ? false : true;
  274. flagselectedRowKeys.value = falg;
  275. selecrowdata.value = [...selectedRowKeys];
  276. // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
  277. emits("handleTableRowSelec", selectedRowKeys, selectedRows);
  278. },
  279. };
  280. // 返回
  281. const handlegoback = () => {
  282. history.back();
  283. };
  284. // 操作
  285. const editEvent = (param1, param2) => {
  286. emits("openModel", param1, param2);
  287. };
  288. const delEvent = (param1, param2) => {
  289. emits("handleDelTable", param1);
  290. };
  291. const handleFlagRead = () => {
  292. emits("FlagRead", selecrowdata);
  293. };
  294. // 分页方法
  295. let objArray = reactive({
  296. // 请求参数
  297. searchParams: {},
  298. // 分页信息
  299. pagination: {
  300. current: 1,
  301. total: 0,
  302. pageSize: 25,
  303. showSizeChanger: true,
  304. showTotal: (total) => `共 ${total} 条`,
  305. pageSizeOptions: ['1',"15", "20", "25", "30", "40"],
  306. onChange: (page, pageSize) => {
  307. handleSizeChange(page, pageSize);
  308. },
  309. },
  310. // 批量选中
  311. selectedRowKeys: [],
  312. // 选中的行数据
  313. selectedRows: [],
  314. });
  315. const handleSizeChange = (page, pageSize) => {
  316. if (objArray.pagination.pageSize != pageSize) {
  317. objArray.pagination.current = 1;
  318. objArray.pagination.pageSize = pageSize;
  319. } else {
  320. objArray.pagination.current = page;
  321. }
  322. emits("getData", props.searchkey, objArray.pagination);
  323. };
  324. // 嵌套子表格
  325. const handleexpand = (expanded, record) => {
  326. console.log(expanded, record, "expanded, record");
  327. };
  328. const expandedRowsChange = (expandedRows) => {
  329. console.log(expandedRows, "expandedRows");
  330. };
  331. // 导出全部
  332. const handleAllExport = () => {
  333. emits("exportXlsx");
  334. };
  335. // 导入
  336. const headers = {
  337. authorization: "authorization-text",
  338. };
  339. const fileList = ref([]);
  340. const handleChange = (info) => {
  341. if (info.file.status !== "uploading") {
  342. console.log(info.file, info.fileList);
  343. }
  344. if (info.file.status === "done") {
  345. message.success(`${info.file.name} file uploaded successfully`);
  346. } else if (info.file.status === "error") {
  347. message.error(`${info.file.name} file upload failed.`);
  348. }
  349. emits("importXlsx", info);
  350. };
  351. const handleImportant = () => { };
  352. const dataSource = ref([]);
  353. const getData = (data, total) => {
  354. dataSource.value = data;
  355. objArray.pagination.total = total;
  356. };
  357. const setpage = (pageojb) => {
  358. objArray.pagination.current = pageojb.current
  359. objArray.pagination.pageSize = pageojb.pageSize
  360. }
  361. defineExpose({ getData, setpage });
  362. </script>
  363. <style lang="less" scoped>
  364. .btn-add {
  365. text-align: right;
  366. /* margin-bottom: 20px; */
  367. /* margin-top: 20px; */
  368. }
  369. :deep(.ant-select-single:not(.ant-select-customize-input)) {
  370. .ant-select-selector {
  371. height: 24px !important;
  372. }
  373. }
  374. :deep(.ant-select-single) {
  375. .ant-select-selector {
  376. .ant-select-selection-item {
  377. line-height: 22px !important;
  378. }
  379. }
  380. }
  381. </style>

 使用:

  1. <ComtableListR ref="tablistRef" :hasImport="false" :loading="loading" :hasExport="false"
  2. :dividerbutton="dividerbutton" :operatingButton="operatingButton" @getData="getData" :columns="columns"
  3. @openModel="openModel">
  4. <template #operation="{ column, record }">
  5. <a @click="openModal('准则类型', record)"> 准则类型</a>
  6. <a-divider type="vertical" />
  7. <a @click="openModal('等级判定', record)"> 等级判定</a>
  8. <a-divider type="vertical" />
  9. </template>
  10. </ComtableListR>
  11. import {
  12. columns,
  13. formListEdit,
  14. dividerbutton,
  15. operatingButton,
  16. } from "./data";
  17. import ComtableListR from "@/components/ComtableListR.vue";
  18. const getData = async (from, param2) => {
  19. let params = {
  20. SkipCount: ((param2?.current ? param2?.current : 1) - 1) * (param2?.pageSize ? param2?.pageSize : 25),
  21. MaxResultCount: (param2?.pageSize ? param2?.pageSize : 25),
  22. };
  23. loading.value = true
  24. const res = await getRiskEvaluation(params);
  25. loading.value = false
  26. tablistRef.value.getData([...res.Items], Number(res.TotalCount));
  27. };
  28. const openModel = (param1, param2) => {
  29. mainedit.value = true
  30. switch (param1) {
  31. case "edit":
  32. formList.value = [...formListEdit];
  33. title.value = "修改";
  34. detailData(param2.Id).then((res) => {
  35. // res.createtime = dayjs(res.createtime);
  36. res.RiskEvaluationMethodDisplayName = res?.RiskEvaluationMethod?.DisplayName ? res?.RiskEvaluationMethod?.DisplayName : ""
  37. refcomDrawer.value.showModal(true, res);
  38. });
  39. visible.value = true;
  40. drawerBtn.value = true;
  41. break;
  42. }
  43. };
  44. const openModal = (type, record) => {
  45. mainedit.value = false
  46. modalId.value = record.Id
  47. switch (type) {
  48. case "准则类型":
  49. modalTitle.value = type;
  50. modalRef.value.showModal(true);
  51. modalColumns.value = typeColumns;
  52. nextTick(() => {
  53. getModalData();
  54. });
  55. break;
  56. case "准则配置":
  57. modalTitle.value = '准则配置';
  58. modalRef2.value.showModal(true);
  59. nextTick(() => {
  60. getConfigData()
  61. });
  62. break;
  63. case "等级判定":
  64. modalTitle.value = type;
  65. modalRef.value.showModal(true);
  66. modalColumns.value = judgeColumns;
  67. nextTick(() => {
  68. getModalData();
  69. });
  70. break;
  71. }
  72. };

data.js

  1. export const columns = [
  2. {
  3. title: '序号',
  4. dataIndex: 'index',
  5. key: 'index', align: 'center',
  6. width: 80,
  7. ellipsis: true
  8. },
  9. {
  10. title: '名称',
  11. dataIndex: 'DisplayName',
  12. key: 'DisplayName', align: 'center',
  13. width: 80,
  14. ellipsis: true
  15. },
  16. {
  17. title: '函数地址',
  18. dataIndex: 'ResultCalculateApi',
  19. key: 'ResultCalculateApi', align: 'center',
  20. width: 80,
  21. ellipsis: true
  22. },
  23. {
  24. title: '创建人',
  25. dataIndex: 'CreatorDisplayName',
  26. key: 'CreatorDisplayName', align: 'center',
  27. customRender: ({ text ,record}) => {
  28. // console.log(record,'rrrrrrrrr')
  29. return record?.Creator?.DisplayName?record?.Creator?.DisplayName:""
  30. },
  31. width: 80,
  32. ellipsis: true
  33. },
  34. {
  35. title: '创建时间',
  36. dataIndex: 'CreationTime',
  37. key: 'CreationTime', align: 'center',
  38. customRender: ({text})=>{
  39. return dayjs(text).format('YYYY-MM-DD HH:mm:ss')
  40. },
  41. width:160,
  42. ellipsis: true
  43. },
  44. {
  45. title: '上次修改人',
  46. dataIndex: 'LastModifierDisplayName',
  47. key: 'LastModifierDisplayName', align: 'center',
  48. customRender: ({ text ,record}) => {
  49. // console.log(record,'rrrrrrrrr')
  50. return record?.LastModifier?.DisplayName?record?.LastModifier?.DisplayName:""
  51. },
  52. width: 80,
  53. ellipsis: true
  54. },
  55. {
  56. title: '上次修改时间',
  57. dataIndex: 'LastModificationTime',
  58. key: 'LastModificationTime',
  59. align: 'center',
  60. customRender: ({ text }) => {
  61. let onetext=dayjs(text).format('YYYY-MM-DD HH:mm:ss')
  62. let TwoText= text?onetext :""
  63. return TwoText
  64. },
  65. width: 160,
  66. ellipsis: true
  67. },
  68. { title: "操作", dataIndex: "operation", key: "operation", align: 'center',
  69. width: 200},
  70. ]
  71. // operating button 操作按钮
  72. export const operatingButton = {
  73. reportdownload: false,
  74. edit: true,
  75. del: false,
  76. detail: false,
  77. addbtn: false
  78. }
  79. export const dividerbutton = {
  80. reportdownload: false,
  81. edit: false,
  82. detail: false
  83. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/621644
推荐阅读
相关标签
  

闽ICP备14008679号