index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <!-- 本组件用于table台账的显示 -->
  3. <div class="tableDiv">
  4. <el-table
  5. ref="table1"
  6. class="tableCom"
  7. :cell-class-name="this.startCellClass ? setCellClass : ''"
  8. :row-class-name="tableRowClassName"
  9. :data="dataList"
  10. style="width: 100%"
  11. height="calc(100% - 66px)"
  12. stripe
  13. highlight-current-row
  14. @row-dblclick="rowDblclick"
  15. @row-click="rowClick"
  16. @select="selectRow"
  17. @select-all="selectRow"
  18. >
  19. <template slot="empty">
  20. <img src="@/assets/icon/null.png" alt="" />
  21. <p class="empty-p">暂无数据</p>
  22. </template>
  23. <el-table-column v-if="config.check" align="center" type="selection" width="55" />
  24. <el-table-column v-if="config.index" align="center" type="index" width="50" label="序号" />
  25. <template v-for="(item, index) in config.fieldList">
  26. <el-table-column
  27. :key="currentDate + 'header_' + index"
  28. align="center"
  29. :prop="item.field"
  30. :width="item.width ? item.width : ''"
  31. :min-width="item.minWidth"
  32. :formatter="formatter"
  33. sortable
  34. :label="item.label"
  35. show-overflow-tooltip
  36. >
  37. <template slot-scope="scope">
  38. <span>{{ formatter(scope.row[item.field]) }}</span>
  39. </template>
  40. </el-table-column>
  41. </template>
  42. <el-table-column v-if="config.actionList" fixed="right" align="center" label="操作" width="150">
  43. <template slot-scope="scope">
  44. <template v-for="(item, index) in config.actionList">
  45. <el-button
  46. :key="currentDate + 'scope_' + index"
  47. type="text"
  48. :class="item.class"
  49. size="small"
  50. @click="item.method(scope.row)"
  51. >{{ item.label }}</el-button
  52. >
  53. </template>
  54. </template>
  55. </el-table-column>
  56. </el-table>
  57. <el-row class="paginationDiv">
  58. <el-pagination
  59. small
  60. background
  61. layout="total, sizes, prev, pager, next, jumper"
  62. :page-sizes="[10, 20, 30, 50, 100, 1000]"
  63. :page-size="size"
  64. :current-page="current"
  65. :total="total"
  66. @size-change="changeSize"
  67. @current-change="changecurrent"
  68. />
  69. </el-row>
  70. </div>
  71. </template>
  72. <script>
  73. import commonMe from '@/utils/common.js'
  74. export default {
  75. props: {
  76. config: {
  77. type: Object,
  78. default: () => ({
  79. check: true, // 是否展示选中框
  80. index: true, // 是否展示序号
  81. // dataList:[],//同步数据
  82. testData: false, // 是否使用测试数据
  83. fieldList: [
  84. {
  85. label: '公司', // 表头显示
  86. field: 'company', // 对应的字段
  87. setCellClassMethod: (e) => {}, // 通过设置cell的类名来改变cell的显示
  88. actionMethod: (e) => {} // 点击时的事件
  89. },
  90. {
  91. label: '部门', // 表头显示
  92. field: 'department', // 对应的字段
  93. setCellClassMethod: (e) => {}, // 通过设置cell的类名来改变cell的显示
  94. actionMethod: (e) => {} // 点击时的事件
  95. },
  96. {
  97. label: '姓名', // 表头显示
  98. field: 'user', // 对应的字段
  99. setCellClassMethod: (e) => {}, // 通过设置cell的类名来改变cell的显示
  100. actionMethod: (e) => {} // 点击时的事件
  101. }
  102. ], // 对应的字段列表
  103. actionList: [
  104. {
  105. label: '处理', // 展示标签
  106. method: (e) => {} // 触发的方法
  107. }
  108. ], // 每列的一些操作
  109. searchDate: {}, // 查询条件(除开翻页以外的其它查询条件)
  110. method: (e) => {}, // 获取数据的方法
  111. click: (e) => {}, // 单击事件
  112. dbClick: (e) => {}, // 双击方法
  113. selectClick: (e) => {} // 选中事件
  114. })
  115. }
  116. },
  117. data() {
  118. return {
  119. testDataList: [], // 存储的测试数据
  120. dataList: [], // 展示的数据列表
  121. total: 0, // 总共的数据
  122. currentDate: '', // 当前的时间点
  123. current: 1, // 当前页
  124. size: 30, // 当前一页多少数据
  125. formatter: commonMe.formatter, // 表格数据的过滤
  126. checkRow: [], // 选择的行数
  127. currentRow: null, // 当前所在的行数
  128. startCellClass: false, // 是否开启cell类设置方法
  129. cellClassList: [] // 保存cell设设置的相关方法
  130. }
  131. },
  132. created() {
  133. this.currentDate = commonMe.getCurrentDateToMS() + ''
  134. },
  135. mounted() {
  136. this.startCellClassMethod()
  137. if (this.config.testData) {
  138. this.getTestData()
  139. } else {
  140. this.getTableData()
  141. }
  142. this.setTableInfo()
  143. },
  144. methods: {
  145. // 翻页改变
  146. changecurrent(data) {
  147. this.current = data
  148. if (this.config.testData) {
  149. this.getCurrentTestData()
  150. } else {
  151. this.getTableData()
  152. }
  153. },
  154. // 页数
  155. changeSize(data) {
  156. this.size = data
  157. if (this.config.testData) {
  158. this.getCurrentTestData()
  159. } else {
  160. this.getTableDataByOne()
  161. }
  162. },
  163. /**
  164. * 重第一页开始获取表格数据
  165. * @param data 是否将页面重置为1
  166. */
  167. getTableDataByOne() {
  168. if (this.config) this.getTableData(true)
  169. },
  170. /**
  171. * 获取表格数据
  172. * @param data 是否将页面重置为1
  173. */
  174. getTableData(data) {
  175. console.log('更新数据')
  176. if (data) {
  177. this.current = 1
  178. }
  179. if (this.config.method) {
  180. const searchData = {
  181. size: this.size,
  182. current: this.current
  183. }
  184. this.config
  185. .method(commonMe.notJsonCopy(searchData))
  186. .then((res) => {
  187. this.dataList = res.records || []
  188. this.total = Number(res.total) || 0
  189. this.reSetTableInfo()
  190. })
  191. .catch((err) => {
  192. this.dataList = []
  193. this.total = 0
  194. this.reSetTableInfo()
  195. console.log(err)
  196. })
  197. }
  198. },
  199. /**
  200. * 更新表格数据
  201. * @param params 界面查询参数
  202. */
  203. update(params = {}) {
  204. if (this.config.method) {
  205. const pageAndSize = { size: this.size, current: 1 }
  206. this.config
  207. .method(commonMe.notJsonCopy({ ...params, ...pageAndSize }))
  208. .then((res) => {
  209. this.dataList = res.records || []
  210. this.total = Number(res.total) || 0
  211. this.reSetTableInfo()
  212. })
  213. .catch((err) => {
  214. this.dataList = []
  215. this.total = 0
  216. this.reSetTableInfo()
  217. console.log(err)
  218. })
  219. }
  220. },
  221. /**
  222. * 通过check选择的数据
  223. */
  224. selectRow(data) {
  225. console.log('选择的信息')
  226. this.checkRow = data
  227. this.setTableInfo()
  228. this.$nextTick((e) => {
  229. if (this.config.selectClick) {
  230. this.config.selectClick(data)
  231. }
  232. })
  233. },
  234. /**
  235. * 单击选中的行
  236. * */
  237. rowClick(data) {
  238. this.currentRow = data
  239. this.setTableInfo()
  240. this.$nextTick((e) => {
  241. if (this.config.click) {
  242. this.config.click(data)
  243. }
  244. })
  245. },
  246. /**
  247. * 双击选中的行
  248. * */
  249. rowDblclick(data) {
  250. this.currentRow = data
  251. this.setTableInfo()
  252. this.$nextTick((e) => {
  253. if (this.config.dbClick) {
  254. this.config.dbClick(data)
  255. }
  256. })
  257. },
  258. /**
  259. * 重置table的值
  260. * */
  261. reSetTableInfo() {
  262. this.currentRow = []
  263. this.checkRow = null
  264. this.setTableInfo()
  265. this.$refs['table1'].doLayout()
  266. },
  267. /**
  268. * 更新table返回的值
  269. * */
  270. setTableInfo() {
  271. const tableData = commonMe.notJsonCopy(this.dataList)
  272. const config = {
  273. currentRow: commonMe.notJsonCopy(this.currentRow),
  274. checkRow: commonMe.notJsonCopy(this.checkRow),
  275. size: commonMe.notJsonCopy(this.size),
  276. current: commonMe.notJsonCopy(this.current),
  277. tableData
  278. }
  279. this.$emit('input', config)
  280. },
  281. /**
  282. * 是否开启cellClass设置函数
  283. * @param data 渲染的cell元素
  284. */
  285. startCellClassMethod() {
  286. this.cellClassList = []
  287. this.config.fieldList.forEach((item) => {
  288. if (item.setCellClassMethod) {
  289. this.startCellClass = true
  290. this.cellClassList[item.field] = item.setCellClassMethod
  291. }
  292. })
  293. },
  294. /**
  295. * 根据不同数据设置cell的类名,通过类名控制其颜色展示
  296. * @param data 渲染的cell元素
  297. */
  298. setCellClass(data) {
  299. const field = data.column.property || ''
  300. if (this.cellClassList[field]) {
  301. return this.cellClassList[field](data)
  302. }
  303. },
  304. /**
  305. * 奇偶行却分
  306. * */
  307. tableRowClassName({ row, rowIndex }) {
  308. if (rowIndex % 2 == 0) {
  309. return 'rowEven'
  310. } else {
  311. return 'rowOdd'
  312. }
  313. },
  314. /**
  315. * 生成测试数据
  316. */
  317. getTestData() {
  318. const testData = []
  319. for (let i = 0; i < 100; i++) {
  320. const testItem = {}
  321. this.config.fieldLit.forEach((item) => {
  322. testItem[item.field] = item.label + '' + i
  323. })
  324. ;('')
  325. tetData.push(testItem)
  326. }
  327. this.testDataList = testData
  328. this.total = this.testDataLit.length
  329. this.getCurrentTestData()
  330. },
  331. /**
  332. * 获取当前测试数据
  333. */
  334. getCurrentTestData() {
  335. this.dataList = this.testDataList.slice((this.current - 1) * this.size, this.size)
  336. }
  337. }
  338. }
  339. </script>
  340. <style lang="scss" scoped>
  341. @import './src/assets/default';
  342. /deep/.tableCom {
  343. @include queryTableRowStyle;
  344. .rowEven {
  345. background: $queryTableRowEvenBckgroundColor;
  346. }
  347. .rowOdd {
  348. background: $queryTableRowOddBckgroundColor;
  349. }
  350. .current-row {
  351. background: $queryTableRowSelectBckgroundColor !important;
  352. td {
  353. background: rgba(0, 0, 0, 0) !important;
  354. }
  355. }
  356. .el-table__body tr.current-row > td.el-table__cell {
  357. background-color: #ecf5ff !important;
  358. transform: none;
  359. }
  360. }
  361. .tableDiv {
  362. position: relative;
  363. float: left;
  364. width: 100%;
  365. height: 100%;
  366. }
  367. .paginationDiv {
  368. margin-top: 20px;
  369. width: 100%;
  370. text-align: right;
  371. }
  372. .overtime span {
  373. //超时
  374. color: #fa541c !important;
  375. }
  376. .notOvertime span {
  377. //未超时
  378. color: #67c23a !important;
  379. }
  380. .notCom span {
  381. //未提交
  382. color: #fa541c !important;
  383. border: 1px solid #ffbb96;
  384. background: #fff2e8;
  385. }
  386. .wait span {
  387. //待发起的样式
  388. color: #f65252 !important;
  389. border: 1px solid #ffa39e;
  390. background: #fef0ef;
  391. }
  392. .inHand span {
  393. //处理中
  394. color: #2d74e7 !important;
  395. border: 1px solid #91d5ff;
  396. background: #e6f7ff;
  397. }
  398. .finished span {
  399. //已完成
  400. color: #67c23a !important;
  401. border: 1px solid #b7eb8f;
  402. background: #f6ffed;
  403. }
  404. /deep/.deleteInfo,
  405. /deep/.updateInfo,
  406. /deep/.lookInfo {
  407. span {
  408. color: #2d74e7 !important;
  409. }
  410. }
  411. </style>