index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <template>
  2. <div class="data-table" :style="{ marginTop: marginTop }">
  3. <div :style="{ 'min-height': minHeight + 'vh' }" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)" class="data-table-content">
  4. <div v-if="isBtn" class="data-table-btn flex-end">
  5. <el-button size="small" @click="handleAdd" plain type="primary">新增</el-button>
  6. </div>
  7. <template v-if="tableData.length">
  8. <div>
  9. <el-table :data="filteredTableData" :summary-method="getSummaries" :span-method="tableSpanMethod" stripe :show-summary="showSummary" border ref="table" :height="minHeight - 8 + 'vh'" class="table" style="width: 100%; overflow: auto" @select="selectHandler">
  10. <el-table-column
  11. v-if="selectionEnable"
  12. type="selection"
  13. width="35"
  14. />
  15. <el-table-column v-for="(item, index) in tableColsCopy" :sortable="item.needSort ? true : false" :key="index" :prop="item.columnName" :label="item.columnLabel" :show-overflow-tooltip="showOverflowTooltip">
  16. <template #header>
  17. <span class="colTips">
  18. <el-tooltip :content="item.columnDescribe" placement="top">
  19. <span>{{ item.columnLabel }}</span>
  20. </el-tooltip>
  21. </span>
  22. <span v-if="item.needFilters">
  23. <el-popover placement="bottom" trigger="click" @show="popoverShowHandler(item.columnName)" @hide="popoverHideHandler">
  24. <i slot="reference" :class="[
  25. 'filter-arrow',
  26. 'el-icon-arrow-down',
  27. arrowClass(item.columnName),
  28. ]" />
  29. <el-form>
  30. <el-form-item :label="item.columnLabel">
  31. <el-select v-model="filterValues[item.columnName]" size="small" placeholder="筛选" default-first-option filterable clearable>
  32. <el-option v-for="(option, optionIndex) in tableDataFilters[
  33. item.columnName
  34. ]" :key="option.value + optionIndex" :value="option.value" :label="option.text" />
  35. </el-select>
  36. </el-form-item>
  37. </el-form>
  38. </el-popover>
  39. </span>
  40. </template>
  41. </el-table-column>
  42. <el-table-column fixed="right" label="操作" width="150">
  43. <template slot-scope="scope">
  44. <div class="hd-td">
  45. <div class="hd-tr">
  46. <!-- <el-button class="hrefBtn" type="text" @click="handleHerf(scope.row)" size="small">跳转</el-button> -->
  47. <el-button type="text" @click="handleEdit(scope.row)" size="small" class="rmScs">编辑</el-button>
  48. <el-button class="rmSc" type="text" @click="handleRemove(scope.row)" size="small">删除</el-button>
  49. <el-button class="rmScs" v-if="withColumnSet" type="text" size="small" @click="handleColumnSet(scope.row)">列设置</el-button>
  50. </div>
  51. </div>
  52. </template>
  53. </el-table-column>
  54. </el-table>
  55. <!-- <p v-if="noMore" style="text-align: center">没有更多了</p> -->
  56. </div>
  57. </template>
  58. <template v-else>
  59. <NoData />
  60. </template>
  61. </div>
  62. <div class="data-table-dialog">
  63. <!--新增/编辑-->
  64. <Dialog :width="width" :flag="flag">
  65. <div class="dialog-content">
  66. <div class="title">{{ tableTitle }}</div>
  67. <div class="content">
  68. <el-form ref="ruleForm" :model="tableForm" :label-width="labelWidth">
  69. <el-row :gutter="20">
  70. <el-col v-for="(item, index) in tableColsCopy" :key="index" :span="rows">
  71. <el-form-item :label="item.columnLabel">
  72. <template v-if="
  73. item.listqueryTemplateID ||
  74. item.listqueryTemplateID == 0
  75. ">
  76. <el-select size="small" clearable style="width: 100%" v-model="tableForm[item.columnName]" @change="changeSelect(item.columnName)" placeholder="请选择">
  77. <el-option v-for="item in tableOptions[item.columnName]" :key="item.v" :label="item.k" :value="item.v">
  78. </el-option>
  79. </el-select>
  80. </template>
  81. <template v-else>
  82. <el-input size="small" v-model="tableForm[item.columnName]"></el-input>
  83. </template>
  84. </el-form-item>
  85. </el-col>
  86. </el-row>
  87. </el-form>
  88. </div>
  89. <div class="foot right t30">
  90. <el-button size="medium" @click="handleOk" class="r24" type="primary">确定</el-button>
  91. <el-button @click="flag = false" size="medium">取消</el-button>
  92. </div>
  93. </div>
  94. </Dialog>
  95. <Dialog :flag="rmFlag">
  96. <div class="airportInfoDialog">
  97. <div class="title del-title">删除</div>
  98. <div class="content del-content">
  99. <span class="el-icon-error error r10"></span>您是否确认删除<span class="error l10">{{ rmTitle }}</span>
  100. </div>
  101. <div class="foot right Delfoot t30">
  102. <el-button size="medium" class="r24" @click="tableRemove" type="danger">删除</el-button>
  103. <el-button size="medium" @click="rmFlag = false">取消</el-button>
  104. </div>
  105. </div>
  106. </Dialog>
  107. </div>
  108. </div>
  109. </template>
  110. <script>
  111. import { setTableFilters } from "@/utils/table";
  112. import Dialog from "@/layout/components/Dialog/index.vue";
  113. import NoData from "@/components/nodata";
  114. import { Query, GeneralDataReception } from "@/api/dataIntegration";
  115. export default {
  116. name: "DataTable",
  117. props: {
  118. isDialog: {
  119. type: Boolean,
  120. default: true
  121. },
  122. //接口ID
  123. dataId: {
  124. type: String || Number,
  125. },
  126. editId: {
  127. type: String || Number,
  128. default: ''
  129. },
  130. dataContent: {
  131. type: Object,
  132. default: () => ({})
  133. },
  134. //弹框宽度
  135. width: {
  136. type: String,
  137. default: "560px",
  138. },
  139. //弹框表单-行数
  140. rows: {
  141. type: Number,
  142. default: 3,
  143. },
  144. //弹框-表单文字宽度
  145. labelWidth: {
  146. type: String,
  147. default: "80px",
  148. },
  149. //表格高度
  150. minHeight: {
  151. type: Number,
  152. default: 65,
  153. },
  154. //是否显示新增按钮
  155. isBtn: {
  156. type: Boolean,
  157. default: true,
  158. },
  159. // 是否显示合计行
  160. showSummary: {
  161. type: Boolean,
  162. default: false
  163. },
  164. marginTop: {
  165. type: String,
  166. default: '20px'
  167. },
  168. // 是否可选中行
  169. selectionEnable: {
  170. type: Boolean,
  171. default: false
  172. },
  173. // 懒加载
  174. withLazyLoad: {
  175. type: Boolean,
  176. default: false
  177. },
  178. // 不换行,溢出隐藏
  179. showOverflowTooltip: {
  180. type: Boolean,
  181. default: false
  182. },
  183. // 表格-操作-列设置按钮显示
  184. withColumnSet: {
  185. type: Boolean,
  186. default: false
  187. }
  188. },
  189. components: { Dialog, NoData },
  190. data () {
  191. return {
  192. loading: false,
  193. flag: false,
  194. serviceId: null,
  195. rmFlag: false,
  196. rowTitle: '',
  197. tableCols: [], //表头数据
  198. tableData: [], //表格数据
  199. tableColsCopy: [], //表头数据缓存
  200. tableDataFilters: {}, //表头-下拉数据
  201. filterValues: {}, //表头-下拉-选中数据
  202. tableDataCopy: [], //缓存table数据
  203. tableGroups: [], //表格分组数据
  204. colShowFilter: "", //表头-下拉-箭头
  205. spanArr: [], //表格分组数据缓存
  206. pos: 0, //表格分组计数
  207. tableTitle: "新增", //弹框标题
  208. tableType: "add", //弹框类型=新增/编辑
  209. tableForm: {}, //弹框表单
  210. rmTitle: "", //弹框-删除-标题
  211. tableObj: {}, //增/删/改数据缓存
  212. tableOptions: {}, //弹框-下来数据缓存
  213. tableArrs: [], //重组table-表头下拉
  214. proAll: false //重组时-所有请求是否都完成
  215. };
  216. },
  217. computed: {
  218. //设置表头-下拉-箭头样式
  219. arrowClass () {
  220. return function (prop) {
  221. let classString = "";
  222. if (this.colShowFilter === prop) {
  223. return "arrow-active";
  224. }
  225. if (
  226. Object.entries(this.tableDataFilters).find(
  227. ([key, arr]) => this.filterValues[prop]
  228. )
  229. ) {
  230. classString += "arrow-blue";
  231. }
  232. return classString;
  233. };
  234. },
  235. //设置表头-下拉-选中数据
  236. filteredTableData () {
  237. return this.tableData.filter((item) => {
  238. let flag = true;
  239. Object.entries(this.filterValues).forEach(([key, value]) => {
  240. if (value !== "" && item[key] !== value) {
  241. flag = false;
  242. }
  243. });
  244. return flag;
  245. });
  246. },
  247. },
  248. // watch: {
  249. // proAll: {
  250. // handler (val) {
  251. // if (val) {
  252. // setTimeout(() => {
  253. // this.setTable()
  254. // }, 200);
  255. // }
  256. // },
  257. // deep: true
  258. // }
  259. // },
  260. mounted () {
  261. this.getQuery();
  262. },
  263. updated() {
  264. this.$refs['table']?.doLayout()
  265. },
  266. methods: {
  267. changeSelect(data){
  268. // this.tableForm[this.tableOptions[data][0].setvalue] = this.tableForm[data];
  269. // console.log(this.tableForm)
  270. },
  271. //获取表格数据
  272. async getQuery () {
  273. try {
  274. this.loading = true;
  275. const { code, returnData } = await Query({
  276. id: this.dataId,
  277. dataContent: Object.values(this.dataContent),
  278. });
  279. if (code == 0) {
  280. const titleColumn = returnData.columnSet.find(item => item.needShow === 1)
  281. if (titleColumn) {
  282. this.rowTitle = titleColumn.columnName
  283. }
  284. this.tableData = returnData.listValues;
  285. this.tableCols = returnData.columnSet;
  286. this.serviceId = returnData.submitID;
  287. setTimeout(() => {
  288. this.initTableData();
  289. this.loading = false;
  290. }, 100);
  291. } else {
  292. this.loading = false;
  293. this.$message.error("获取表格数据失败");
  294. }
  295. } catch (error) {
  296. this.loading = false;
  297. console.log(error);
  298. }
  299. },
  300. //表格-增/删/改
  301. async generalDataReception (data) {
  302. try {
  303. data = {
  304. ...data,
  305. ...this.dataContent
  306. }
  307. const { code, message } = await GeneralDataReception({
  308. serviceId: this.serviceId,
  309. dataContent: JSON.stringify(data),
  310. });
  311. if (code == 0) {
  312. this.$message.success("操作成功");
  313. this.getQuery();
  314. this.flag = false;
  315. this.rmFlag = false;
  316. this.tableObj = {};
  317. this.tableForm = {};
  318. } else {
  319. this.$message.error("操作失败");
  320. this.flag = false;
  321. this.rmFlag = false;
  322. this.tableObj = {};
  323. this.tableForm = {};
  324. }
  325. } catch (error) {
  326. console.log(error);
  327. this.flag = false;
  328. this.rmFlag = false;
  329. this.tableObj = {};
  330. this.tableForm = {};
  331. }
  332. },
  333. // 表格勾选
  334. toggleRowSelection(row, isSelected) {
  335. this.$refs['table'].toggleRowSelection(row, isSelected)
  336. },
  337. // 表格初始勾选
  338. selectTableRows(tableData, selectFlagName) {
  339. tableData.forEach(row => {
  340. this.$refs['table'].toggleRowSelection(row, !!row[selectFlagName])
  341. })
  342. },
  343. //初始化表格
  344. initTableData () {
  345. this.tableColsCopy = this.tableCols.filter((item) => item.needShow);
  346. this.tableDataCopy = _.cloneDeep(this.tableData);
  347. const datas = _.cloneDeep(this.tableColsCopy);
  348. const reqUts = []
  349. datas.forEach(async (item) => {
  350. this.tableDataFilters[item.columnName] = [];
  351. if (item.needGroup) {
  352. this.tableGroups.push(item.columnName);
  353. }
  354. if (item.listqueryTemplateID || item.listqueryTemplateID == 0) {
  355. this.tableArrs.push(item.columnName)
  356. // const reqUt = this.getSelectData(item.listqueryTemplateID)
  357. // reqUts.push(reqUt)
  358. this.tableOptions[item.columnName] = await this.getSelectData(item.listqueryTemplateID)
  359. }
  360. // this.filterValues[item.columnName] = ''
  361. });
  362. setTableFilters(this.tableData, this.tableDataFilters);
  363. this.tableGroup(this.tableData);
  364. this.selectTableRows(this.tableData, 'DeployID')
  365. // this.getSelectDataAll(reqUts)
  366. },
  367. //获取所有获取弹框-下拉数据-请求状态
  368. getSelectDataAll (reqUts) {
  369. Promise.all(reqUts).then(res => {
  370. this.proAll = true
  371. }).catch(err => {
  372. this.proAll = false
  373. })
  374. },
  375. //获取弹框-下拉数据
  376. async getSelectData (id) {
  377. const { code, returnData } = await Query({
  378. id,
  379. dataContent: [],
  380. });
  381. if (code == 0) {
  382. return returnData.listValues;
  383. } else {
  384. return [];
  385. }
  386. },
  387. //重组table-显示名称
  388. setTable () {
  389. this.tableArrs.forEach(item => {
  390. this.tableOptions[item].forEach(p => {
  391. this.tableDataCopy.forEach(msg => {
  392. if (msg[item] == p.v) {
  393. msg[item] = p.k
  394. }
  395. })
  396. this.tableDataFilters[item].forEach(cap => {
  397. if (cap.value == p.v) {
  398. cap.text = p.k
  399. cap.value = p.k
  400. }
  401. })
  402. })
  403. })
  404. this.tableData = this.tableDataCopy
  405. },
  406. //分组
  407. tableGroup (tableData) {
  408. const spanArr = [];
  409. let pos = 0;
  410. let ifYj = this.tableGroups[0];
  411. for (let i = 0; i < tableData.length; i++) {
  412. if (i === 0) {
  413. spanArr.push(1);
  414. } else {
  415. if (tableData[i][ifYj] === tableData[i - 1][ifYj]) {
  416. spanArr[pos] += 1;
  417. spanArr.push(0);
  418. } else {
  419. spanArr.push(1);
  420. pos = i;
  421. }
  422. }
  423. }
  424. this.spanArr = spanArr;
  425. this.pos = pos;
  426. },
  427. popoverShowHandler (prop) {
  428. this.colShowFilter = prop;
  429. },
  430. popoverHideHandler () {
  431. this.colShowFilter = "";
  432. },
  433. //分组
  434. tableSpanMethod ({ row, column, rowIndex, columnIndex }) {
  435. if (this.tableGroups.includes(column["property"])) {
  436. const _row = this.spanArr[rowIndex];
  437. const _col = _row > 0 ? 1 : 0;
  438. return {
  439. rowspan: _row,
  440. colspan: _col,
  441. };
  442. }
  443. },
  444. //合计
  445. getSummaries (param) {
  446. const { columns, data } = param;
  447. const sums = [];
  448. columns.forEach((column, index) => {
  449. this.tableColsCopy.forEach((p) => {
  450. if (column.property == p.columnName && p.needCount) {
  451. const values = data.map((item) => Number(item[column.property]));
  452. if (!values.every((value) => isNaN(value))) {
  453. sums[index] = values.reduce((prev, curr) => {
  454. const value = Number(curr);
  455. if (!isNaN(value)) {
  456. return prev + curr;
  457. } else {
  458. return prev;
  459. }
  460. }, 0);
  461. sums[index] += "";
  462. }
  463. }
  464. });
  465. });
  466. return sums;
  467. },
  468. //弹框-确定
  469. handleOk () {
  470. this.submitClickHandler();
  471. },
  472. //滚动
  473. tableLoad () {
  474. console.log("d");
  475. },
  476. //表格-新增
  477. handleAdd () {
  478. // this.flag = true;
  479. // this.tableType = "add";
  480. // this.tableTitle = "新增";
  481. // this.tableForm = {};
  482. if(this.isDialog ){
  483. this.flag = true;
  484. this.tableType = "add";
  485. this.tableTitle = "新增";
  486. this.tableForm = {};
  487. }
  488. else{
  489. this.$emit("handleAdd");
  490. }
  491. // console.log(this.tableOptions);
  492. },
  493. //表格-编辑
  494. async handleEdit (row) {
  495. if(this.isDialog) {
  496. if (this.editId) {
  497. this.tableForm = await this.queryOriginRow(this.editId, row.queryTemplateColumnSetID)
  498. // Object.entries(tableForm).forEach(([key, value]) => {
  499. // this.tableForm[key] = value
  500. // })
  501. } else {
  502. this.tableForm = row;
  503. }
  504. this.flag = true;
  505. this.tableType = "edit";
  506. this.tableTitle = "编辑";
  507. } else{
  508. this.$emit("handleEdit",row);
  509. }
  510. },
  511. async queryOriginRow(editId, queryTemplateColumnSetID) {
  512. try {
  513. const { code, returnData } = await Query({
  514. id: editId,
  515. dataContent: [queryTemplateColumnSetID]
  516. })
  517. if (Number(code) === 0) {
  518. return returnData.listValues[0]
  519. } else {
  520. this.$message.error("失败")
  521. }
  522. } catch (error) {
  523. console.log(error)
  524. }
  525. },
  526. //表格-跳转
  527. handleHerf(row){
  528. alert('跳转')
  529. },
  530. // 新增/编辑-确认
  531. submitClickHandler () {
  532. this.$refs["ruleForm"].validate((valid) => {
  533. if (valid) {
  534. if (this.tableType == "add") {
  535. this.tableForm.event = 1;
  536. } else {
  537. this.tableForm.event = 2;
  538. }
  539. this.generalDataReception(this.tableForm);
  540. } else {
  541. return false;
  542. }
  543. });
  544. },
  545. //表格-删除
  546. handleRemove (row) {
  547. this.rmFlag = true;
  548. // this.rmTitle = row.className || row.username || row.serviceName || row.queryTemplateName;
  549. this.rmTitle = row[this.rowTitle]
  550. this.tableObj = row;
  551. },
  552. //表格-删除-确认
  553. tableRemove () {
  554. this.tableObj.event = 3;
  555. this.generalDataReception(this.tableObj);
  556. },
  557. // 表格-跳转列设置页
  558. handleColumnSet(row) {
  559. this.$router.push({
  560. path: '/systemSettings/queryTemplateChild',
  561. query: {
  562. queryTemplateID: row.queryTemplateID
  563. }
  564. })
  565. },
  566. // 表格-选中行
  567. selectHandler(selection, row) {
  568. this.$emit('selection-change', selection, row)
  569. }
  570. },
  571. };
  572. </script>
  573. <style lang="scss" scoped>
  574. .data-table {
  575. width: 100%;
  576. background-color: #fff;
  577. padding: 20px;
  578. ::v-deep .table {
  579. width: 100%;
  580. .cell {
  581. padding: 0;
  582. text-align: center;
  583. font-size: 14px;
  584. font-family: Helvetica, "Microsoft YaHei";
  585. letter-spacing: 0;
  586. }
  587. .cell-click {
  588. cursor: pointer;
  589. color: #2d7cff;
  590. &.cell-clicked {
  591. color: purple;
  592. }
  593. }
  594. .el-table__header-wrapper {
  595. .cell {
  596. font-weight: bold;
  597. color: #101116;
  598. }
  599. .has-gutter {
  600. tr {
  601. .bgl-huang {
  602. background: #fcf0b1;
  603. }
  604. }
  605. }
  606. .table-column--selection .cell .el-checkbox {
  607. display: none;
  608. }
  609. }
  610. .el-table__body-wrapper {
  611. tr.bgl-hui {
  612. background: #d2d6df;
  613. td {
  614. background: #d2d6df;
  615. }
  616. &.redBorder {
  617. position: relative;
  618. &::after {
  619. content: "";
  620. position: absolute;
  621. left: 0;
  622. bottom: 0;
  623. width: 100%;
  624. height: 2px;
  625. background: #e83f82;
  626. }
  627. }
  628. }
  629. }
  630. .rmScs {
  631. width: 48px;
  632. height: 24px;
  633. border-color: #9ebbf7;
  634. box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.06);
  635. border-radius: 4px;
  636. display: flex;
  637. align-items: center;
  638. justify-content: center;
  639. float: left;
  640. span {
  641. color: #2d67e3;
  642. }
  643. }
  644. .rmSc {
  645. width: 48px;
  646. height: 24px;
  647. background: #eb2f3b;
  648. box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.06);
  649. border-radius: 4px;
  650. display: flex;
  651. align-items: center;
  652. justify-content: center;
  653. span {
  654. color: #ffffff;
  655. }
  656. }
  657. .hrefBtn {
  658. width: 48px;
  659. height: 24px;
  660. background: #6F81BC;
  661. box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.06);
  662. border-radius: 4px;
  663. display: flex;
  664. align-items: center;
  665. justify-content: center;
  666. span {
  667. color: #ffffff;
  668. }
  669. }
  670. .el-table__fixed-right {
  671. thead {
  672. div {
  673. color: #101116;
  674. }
  675. }
  676. }
  677. .hd-td {
  678. .hd-tr{
  679. display: flex;
  680. justify-content: center;
  681. flex-direction: row;
  682. padding: 0 8px;
  683. }
  684. }
  685. }
  686. .data-table-btn {
  687. margin-bottom: 20px;
  688. }
  689. }
  690. .filter-arrow {
  691. cursor: pointer;
  692. transition: 0.3s transform;
  693. &.arrow-active {
  694. transform: rotate(-180deg);
  695. }
  696. &.arrow-blue {
  697. color: #2d7cff;
  698. font-weight: bold;
  699. }
  700. }
  701. .el-select-dropdown__item.hover {
  702. background: #d2d6df;
  703. }
  704. </style>