KtCommonTable.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <template>
  2. <div style="position: relative" ref="commonTable">
  3. <!--表格栏-->
  4. <el-table id="commonTable" ref="doLayout" :data="data.content" :highlight-current-row="highlightCurrentRow"
  5. @selection-change="selectionChange" @row-click="rowClick" @current-change="handleCurrentChange"
  6. :element-loading-text="$t('action.loading')" :border="border" :stripe="stripe" :show-summary="showSummary"
  7. :summary-method="getSummaries" :show-overflow-tooltip="showOverflowTooltip" :height="tableHeights? tableHeights:tableHeight" :size="size"
  8. :align="align" :header-cell-style="{
  9. background: '#F2F7FC',
  10. fontWeight: '900',
  11. color: '#606266',
  12. }" style="width: 100%" :row-class-name="tableRowClassName">
  13. <el-table-column type="selection" width="50" fixed align="center"
  14. v-if="showBatchDelete || showBatchSettled || showBatchPrint"></el-table-column>
  15. <el-table-column v-if="SerialShow" label="序号" type="index" width="50" align="center"></el-table-column>
  16. <template v-for="column in columns">
  17. <el-table-column header-align="center" align="center" :prop="column.prop" :label="column.label"
  18. :width="column.width" :min-width="column.minWidth" :fixed="column.fixed" :key="column.prop" :type="column.type"
  19. :formatter="column.formatter" :sortable="column.sortable ? column.sortable : false"
  20. v-if="column.signIcon || column.isImg || column.isBtn">
  21. <template slot-scope="scope">
  22. <el-switch :disabled="column.isDisabled" v-if="column.signIcon" v-model="scope.row[column.prop]"
  23. active-value="1" inactive-value="0" @change="signStateChanged(scope.$index, scope.row)"></el-switch>
  24. <img v-if="column.isImg" :width="column.imgWidth" :height="column.imgHeight" :src="scope.row[column.prop]" />
  25. <kt-button v-if="column.isBtn" icon="fa fa-edit" :label="column.btnName" :size="size"
  26. :perms="column.permsEdit" @click="handleBtnEdit(scope.$index, scope.row)" />
  27. </template>
  28. </el-table-column>
  29. <el-table-column header-align="center" align="center" :prop="column.prop" :label="column.label"
  30. :width="column.width" :min-width="column.minWidth" :fixed="column.fixed" :key="column.prop" :type="column.type"
  31. :formatter="column.formatter" :sortable="column.sortable ? column.sortable : false" v-else>
  32. <template v-for="columnchildren in column.children">
  33. <el-table-column v-if="columnchildren" header-align="center" align="center" :prop="columnchildren.prop"
  34. :label="columnchildren.label" :width="columnchildren.width" :min-width="columnchildren.minWidth"
  35. :fixed="columnchildren.fixed" :key="columnchildren.prop" :type="columnchildren.type"
  36. :formatter="columnchildren.formatter" :sortable="columnchildren.sortable == null ? true : columnchildren.sortable
  37. ">
  38. </el-table-column>
  39. </template>
  40. </el-table-column>
  41. </template>
  42. <el-table-column :label="$t('action.operation')" :width="operationWidth" fixed="right" v-if="showOperation"
  43. header-align="center" align="center">
  44. <template slot-scope="scope">
  45. <kt-button
  46. type="primary"
  47. v-if="editButtonName"
  48. icon="fa fa-edit"
  49. :label="editButtonName"
  50. :perms="permsEdit"
  51. :size="size"
  52. @click="handleEdit(scope.$index, scope.row)"
  53. />
  54. <kt-button
  55. v-if="deleteButtonName"
  56. icon="fa fa-trash"
  57. :label="deleteButtonName"
  58. :perms="permsDelete"
  59. :size="size"
  60. type="danger"
  61. @click="handleDeleteFlag(scope.$index, scope.row)"
  62. />
  63. <kt-button
  64. :style="{
  65. background: button1Background + '',
  66. border: '1px solid' + button1Background,
  67. color: '#fff',
  68. }"
  69. v-if="button1Name"
  70. :label="button1Name"
  71. :perms="permsButton1"
  72. :size="size"
  73. :icon="button1Icon"
  74. @click="handleButton1(scope.$index, scope.row)"
  75. />
  76. <kt-button
  77. :style="{
  78. background: button2Background,
  79. border: '1px solid' + button2Background,
  80. color: '#fff',
  81. }"
  82. v-if="button2Name"
  83. :label="button2Name"
  84. :perms="permsButton2"
  85. :size="size"
  86. :icon="button2Icon"
  87. @click="handleButton2(scope.$index, scope.row)"
  88. />
  89. <kt-button
  90. :style="{
  91. background: button3Background,
  92. border: '1px solid' + button3Background,
  93. color: '#fff',
  94. }"
  95. v-if="button3Name"
  96. :label="button3Name"
  97. :perms="permsButton3"
  98. :size="size"
  99. :icon="button3Icon"
  100. @click="handleButton3(scope.$index, scope.row)"
  101. />
  102. <kt-button
  103. :style="{
  104. background: button4Background,
  105. border: '1px solid ' + button4Background,
  106. color: '#ffffff',
  107. }"
  108. v-if="button4Name"
  109. :icon="button4Icon"
  110. :label="button4Name"
  111. :perms="permsButton4"
  112. :size="size"
  113. @click="handleButton4(scope.$index, scope.row)"
  114. />
  115. </template>
  116. </el-table-column>
  117. </el-table>
  118. <!--分页栏-->
  119. <!-- 底部分页 -->
  120. <div class="bottomFixed" ref="bottomFixed">
  121. <kt-button class="debutton" :label="$t('action.batchDelete')" :perms="permsDelete" :size="size" type="danger"
  122. @click="handleBatchDelete()" :disabled="selections.length === 0" style="float: left"
  123. v-if="showBatchDelete & showOperation" />
  124. <kt-button :label="settledName" :perms="permsDelete" :size="size" type="danger" @click="handleBatchSettled()"
  125. :disabled="selections.length === 0" style="float: left" v-if="showBatchSettled" />
  126. <!-- <kt-button label="导出Excel" :perms="permsDelete" :size="size" type="danger" @click="handleBatchPrint()"
  127. :disabled="selections.length === 0" style="float: left" v-if="showBatchPrint" /> -->
  128. <kt-button label="导出Excel" :perms="permsDelete" :size="size" type="danger" @click="handleBatchPrint()"
  129. style="float: left" v-if="showBatchPrint" />
  130. <el-pagination @size-change="handleSizeChange" layout="total, sizes, prev, pager, next, jumper"
  131. @current-change="refreshPageRequest" :current-page="data.pageNum" :page-size="data.pageSize"
  132. :page-sizes="[10, 20, 50, 100, 200, 300, 400, 500]" :total="data.totalSize" style="float: left; margin-left: 20px"
  133. v-if="isshowpagination">
  134. </el-pagination>
  135. </div>
  136. </div>
  137. </template>
  138. <script>
  139. import KtButton from "@/views/Core/KtButton";
  140. import BigNumber from "bignumber.js";
  141. export default {
  142. name: "KtCommonTable",
  143. components: {
  144. KtButton,
  145. },
  146. props: {
  147. tableHeights:{
  148. type: Number,
  149. },
  150. operationWidth: {
  151. type: Number,
  152. default: 200,
  153. },
  154. showBatchSettled: {
  155. //显示批量结算
  156. type: Boolean,
  157. default: false,
  158. },
  159. SerialShow: {
  160. type: Boolean,
  161. default: true,
  162. },
  163. settledName: {
  164. //批量按钮name
  165. type: String,
  166. },
  167. showBatchPrint: {
  168. //显示批量导出
  169. type: Boolean,
  170. default: false,
  171. },
  172. //显示合计
  173. showSummary: {
  174. type: Boolean,
  175. default: false,
  176. },
  177. button1Name: {
  178. type: String,
  179. },
  180. button2Name: {
  181. type: String,
  182. },
  183. button3Name: {
  184. type: String,
  185. },
  186. button4Name: {
  187. type: String,
  188. },
  189. permsButton1: {
  190. type: String,
  191. },
  192. permsButton2: {
  193. type: String,
  194. },
  195. permsButton3: {
  196. type: String,
  197. },
  198. permsButton4: {
  199. type: String,
  200. },
  201. button1Background: {
  202. type: String,
  203. default: "#f56c6c",
  204. },
  205. button2Background: {
  206. type: String,
  207. default: "rgb(222, 80, 76)",
  208. },
  209. button3Background: {
  210. type: String,
  211. default: "rgb(14, 194, 22)",
  212. },
  213. button4Background: {
  214. type: String,
  215. default: "#1e87ed",
  216. },
  217. button1Icon: {
  218. type: String,
  219. default: "fa fa-trash",
  220. },
  221. button2Icon: {
  222. type: String,
  223. default: "fa fa-edit",
  224. },
  225. button3Icon: {
  226. type: String,
  227. default: "fa fa-edit",
  228. },
  229. button4Icon: {
  230. type: String,
  231. default: "fa fa-trash",
  232. },
  233. editButtonName: {
  234. type: String,
  235. },
  236. deleteButtonName: {
  237. type: String,
  238. },
  239. isshowpagination: {
  240. //是否显示分页栏
  241. type: Boolean,
  242. default: true,
  243. },
  244. showSummaryFieldList: Array,//自定义合计列配置
  245. keyName: String, //主键字段名
  246. columns: Array, // 表格列配置
  247. data: {}, // 表格分页数据
  248. permsEdit: String, // 编辑权限标识
  249. permsDelete: String, // 删除权限标识
  250. size: {
  251. // 尺寸样式
  252. type: String,
  253. default: "small ",
  254. },
  255. align: {
  256. // 文本对齐方式
  257. type: String,
  258. default: "left",
  259. },
  260. // maxHeight: { // 表格最大高度
  261. // type: Number,
  262. // default: 1420
  263. // },
  264. // height: { // 表格最大高度
  265. // type: Number,
  266. // default: 250
  267. // },
  268. showOperation: {
  269. // 是否显示操作组件
  270. type: Boolean,
  271. default: true,
  272. },
  273. border: {
  274. // 是否显示边框
  275. type: Boolean,
  276. default: true,
  277. },
  278. stripe: {
  279. // 是否显示斑马线
  280. type: Boolean,
  281. default: true,
  282. },
  283. highlightCurrentRow: {
  284. // 是否高亮当前行
  285. type: Boolean,
  286. default: true,
  287. },
  288. showOverflowTooltip: {
  289. // 是否单行显示
  290. type: Boolean,
  291. default: true,
  292. },
  293. showBatchDelete: {
  294. // 是否显示操作组件
  295. type: Boolean,
  296. default: false,
  297. },
  298. },
  299. data() {
  300. return {
  301. tableHeight: 0,
  302. // 分页信息
  303. pageRequest: {
  304. pageNum: 1,
  305. pageSize: 20,
  306. },
  307. loading: false, // 加载标识
  308. selections: [], // 列表选中列
  309. };
  310. },
  311. methods: {
  312. tableRowClassName({ row, rowIndex }) {
  313. if (!!row.fieldName24 && row.fieldName24.indexOf("未") >= 0) {
  314. return "warningRow";
  315. } else if (!!row.fieldName24 && row.fieldName24.indexOf("已") >= 0) {
  316. return "successRow";
  317. }
  318. // return '';
  319. },
  320. getSummaries(param) {
  321. const { columns, data } = param;
  322. const sums = [];
  323. columns.forEach((column, index) => {
  324. if (index === 0) {
  325. sums[index] = "合计"; // 假设第一列显示文本'合计'
  326. return;
  327. }
  328. if (this.showSummaryFieldList) {
  329. // 如果当前列名不在需要计算的列名数组中,直接返回空值或其他标记
  330. if (!this.showSummaryFieldList.includes(column.property)) {
  331. sums[index] = "-";
  332. return;
  333. }
  334. }
  335. let sum = new BigNumber(0);
  336. data.forEach((item) => {
  337. let value = item[column.property];
  338. let number = new BigNumber(value);
  339. if (!number.isNaN()) {
  340. sum = sum.plus(number);
  341. }
  342. });
  343. sums[index] = sum.toFixed(); // 转换成字符串避免精度问题
  344. });
  345. return sums;
  346. },
  347. // 分页查询
  348. findPage: function () {
  349. this.loading = true;
  350. let callback = (res) => {
  351. this.loading = false;
  352. //解决操作栏位置偏移问题
  353. this.$nextTick(() => {
  354. this.$refs.doLayout.doLayout();
  355. }, 200);
  356. };
  357. this.$emit("findPage", {
  358. pageRequest: this.pageRequest,
  359. callback: callback,
  360. });
  361. },
  362. hideLoading() {
  363. this.loading = false;
  364. },
  365. // 选择切换
  366. selectionChange: function (selections) {
  367. this.selections = selections;
  368. this.$emit("selectionChange", {
  369. selections: selections,
  370. });
  371. },
  372. // 选择切换
  373. handleCurrentChange: function (val) {
  374. this.$emit("handleCurrentChange", {
  375. val: val,
  376. });
  377. },
  378. rowClick: function (row) {
  379. this.$emit("rowClick", {
  380. row: row,
  381. });
  382. },
  383. // 换页刷新
  384. refreshPageRequest: function (pageNum) {
  385. this.pageRequest.pageNum = pageNum;
  386. this.findPage();
  387. },
  388. // 每页条数发生改变
  389. handleSizeChange(pageSize) {
  390. this.pageRequest.pageSize = pageSize;
  391. this.findPage();
  392. },
  393. signStateChanged(index, row) {
  394. this.$emit("signStateChanged", {
  395. index: index,
  396. row: row,
  397. });
  398. },
  399. // 按钮编辑
  400. handleBtnEdit: function (index, row) {
  401. this.$emit("handleBtnEdit", {
  402. index: index,
  403. row: row,
  404. });
  405. },
  406. // 编辑
  407. handleEdit: function (index, row) {
  408. this.$emit("handleEdit", {
  409. index: index,
  410. row: row,
  411. });
  412. // this.$emit('findLeftTreeData')
  413. },
  414. // 删除
  415. handleDelete: function (index, row) {
  416. let _keyName = "id";
  417. if (!!this.keyName) {
  418. _keyName = this.keyName;
  419. }
  420. this.delete(row[_keyName]);
  421. },
  422. handleButton1: function (index, row) {
  423. this.$emit("handleButton1", {
  424. index: index,
  425. row: row,
  426. });
  427. },
  428. handleButton2: function (index, row) {
  429. this.$emit("handleButton2", {
  430. index: index,
  431. row: row,
  432. });
  433. },
  434. handleButton3: function (index, row) {
  435. this.$emit("handleButton3", {
  436. index: index,
  437. row: row,
  438. });
  439. },
  440. handleButton4: function (index, row) {
  441. this.$emit("handleButton4", {
  442. index: index,
  443. row: row,
  444. });
  445. },
  446. //删除单个
  447. handleDeleteFlag: function (index, row) {
  448. this.$emit("handleDeleteFlag", { index: index, row: row });
  449. // var _keyName = "id";
  450. // if (!!this.keyName) {
  451. // _keyName = this.keyName;
  452. // }
  453. // let ids = row[_keyName];
  454. // this.$confirm("确认删除吗?", "提示", {
  455. // type: "warning",
  456. // })
  457. // .then(() => {
  458. // let params = [];
  459. // let idArray = (ids + "").split(",");
  460. // for (var i = 0; i < idArray.length; i++) {
  461. // var obj = {};
  462. // obj[_keyName] = idArray[i];
  463. // params.push(obj);
  464. // }
  465. // this.loading = true;
  466. // let callback = (res) => {
  467. // if (res.code == 200) {
  468. // this.$message({
  469. // message: "操作成功",
  470. // type: "success",
  471. // });
  472. // this.findPage();
  473. // // this.$emit('findLeftTreeData')
  474. // } else {
  475. // this.$message({
  476. // message: "操作失败, " + res.msg,
  477. // type: "error",
  478. // });
  479. // }
  480. // };
  481. // this.$emit("handleDeleteFlag", {
  482. // params: params,
  483. // callback: callback,
  484. // });
  485. // this.loading = false;
  486. // })
  487. // .catch(() => {
  488. // this.loading = false;
  489. // });
  490. },
  491. // 批量删除
  492. handleBatchDelete: function () {
  493. let idlist = [];
  494. this.selections.map((item) => idlist.push(item.id));
  495. this.$emit("handleDelete", idlist);
  496. },
  497. // 删除操作
  498. delete: function (ids) {
  499. var _keyName = "id";
  500. if (!!this.keyName) {
  501. _keyName = this.keyName;
  502. }
  503. this.$confirm("确认删除吗?", "提示", {
  504. type: "warning",
  505. })
  506. .then(() => {
  507. let params = [];
  508. let idArray = (ids + "").split(",");
  509. for (var i = 0; i < idArray.length; i++) {
  510. var obj = {};
  511. obj[_keyName] = idArray[i];
  512. params.push(obj);
  513. }
  514. let callback = (res) => {
  515. if (res.code == 200) {
  516. this.$message({
  517. message: "操作成功",
  518. type: "success",
  519. });
  520. this.findPage();
  521. // this.$emit('findLeftTreeData')
  522. } else {
  523. this.$message({
  524. message: "操作失败, " + res.msg,
  525. type: "error",
  526. });
  527. }
  528. };
  529. this.$emit("handleDelete", {
  530. params: params,
  531. callback: callback,
  532. });
  533. })
  534. .catch(() => { });
  535. },
  536. //批量结算
  537. handleBatchSettled: function () {
  538. let _keyName = "orderno";
  539. if (!!this.keyName) {
  540. _keyName = this.keyName;
  541. }
  542. let idlist = [];
  543. this.selections.map((item) => idlist.push(item[_keyName]));
  544. this.$emit("handleSettled", idlist);
  545. },
  546. //批量打印
  547. handleBatchPrint: function () {
  548. // let _keyName = "orderno";
  549. // if(!!this.keyName){
  550. // _keyName = this.keyName;
  551. // }
  552. // let ids = this.selections.map(item => item[_keyName]).toString()
  553. // console.log(ids);
  554. this.$emit("handlePrint");
  555. },
  556. },
  557. mounted() {
  558. const that = this;
  559. that.refreshPageRequest(1);
  560. setTimeout(() => {
  561. let tableTop = that.$refs.commonTable.offsetTop;
  562. let bottomTop = that.$refs.bottomFixed.offsetTop;
  563. that.tableHeight = bottomTop - tableTop - 120 + "px";
  564. }, 10);
  565. // window.onresize = function () {
  566. // let tableTop = that.$refs.commonTable.offsetTop;
  567. // let bottomTop = that.$refs.bottomFixed.offsetTop;
  568. // that.tableHeight = bottomTop - tableTop - 120 + "px";
  569. // };
  570. },
  571. };
  572. </script>
  573. <style scoped>
  574. /* 底部固定分页 */
  575. .bottomFixed {
  576. height: 40px;
  577. position: fixed;
  578. bottom: 0;
  579. width: 100%;
  580. background: #fff;
  581. /* margin-top: 10px; */
  582. /* box-shadow: 0 -2px 0 0 #efefef; */
  583. }
  584. .el-table .warningRow {
  585. background: oldlace;
  586. }
  587. .el-table .successRow {
  588. background: #f0f9eb;
  589. }
  590. </style>