cangDateTable.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <template>
  2. <view class="table_box">
  3. <view class="boxlist">
  4. </view>
  5. <scroll-view scroll-x scroll-y :class="tabHeight" @scrolltolower="scrolltolower">
  6. <uni-table stripe border :loading="loading" style="min-height: 100%;">
  7. <uni-tr id="lyy-thead" :class="headerFixed?'fixed-head':''" style="display: flex;">
  8. <uni-th v-for="(item,index) in headers" :key="index" :width="item.widtd" align="center" :style="{
  9. display:item.hidden?'none':'flex',justifyContent: 'center',
  10. left:fixedLeft(index),
  11. position:index<=columnFixed-1?'sticky':'unset',
  12. zIndex:index<=columnFixed-1?999:99,
  13. backgroundColor:'inherit'
  14. }">
  15. <view @click="doSort(item)" style="display: flex;flex-direction: row;justify-content: center;">
  16. <text :style="{lineHeight:'20px'}">{{item.label}}</text>
  17. <view class="header-icon"
  18. style="line-height:8px;display:flex;flex-direction:column;margin-left:10px;justify-content: center;"
  19. v-if="item.sort">
  20. <text class="iconfont icon-arrow-up"
  21. :style="{fontSize:'8px',height:'10px',color:lastSortItem===item.key&&sortWay=='asc'?'#333':'#bcbcbc'}" />
  22. <text class="iconfont icon-arrow-down"
  23. :style="{fontSize:'8px',height:'10px',color:lastSortItem===item.key&&sortWay=='desc'?'#333':'#bcbcbc'}" />
  24. </view>
  25. </view>
  26. </uni-th>
  27. </uni-tr>
  28. <!--<uni-tr v-if="headerFixed" :style="{height: theadHeight}">
  29. </uni-tr>
  30. <!-- <uni-td class="no_data" align="center">暂无数据</uni-td> -->
  31. <!-- <view v-if="contents.length<1" class="no_data">暂无数据</view> -->
  32. <template v-if="contents.length>0">
  33. <uni-tr v-for="(content,sindex) in sortContents" :key="sindex"
  34. style="display:flex;table-layout: fixed;min-width: 100%;" @click.native="rowClick(content)">
  35. <uni-td v-for="(header,hindex) in headers" :key="hindex" :width="header.widtd" align="center"
  36. :style="{display:header.hidden?'none':'table-cell',textAlign:'center',
  37. left:fixedLeft(hindex),
  38. position:hindex<=columnFixed-1?'sticky':'unset',
  39. zIndex:hindex<=columnFixed-1?9:'unset',
  40. backgroundColor:'inherit'}">
  41. <template v-if="header.key == 'isSwap'">
  42. <text>{{content[header.key] === 1 ? '是':content[header.key] === 0?'否':emptyString}}</text>
  43. </template>
  44. <template v-else-if="header.key == 'isCheck'">
  45. <text>{{$checks.cangFitler(content[header.key])}}</text>
  46. </template>
  47. <template v-else>
  48. <view v-if="header.label == '操作'" class="editColor">
  49. <view @click="EditCangData(content)" v-if="isEdit===1">编辑</view>
  50. <view @click="DelCangData(content)" v-if="isEdit===1">删除</view>
  51. </view>
  52. <text v-else>{{content[header.key]||emptyString}}</text>
  53. </template>
  54. </uni-td>
  55. </uni-tr>
  56. </template>
  57. <uni-tr v-if="totalRow.length>0" style="min-width: 100%;display: flex;">
  58. <uni-td v-for="(header,index) in headers" :key="Math.random()" align="center" :style="{textAlign: 'center',display:header.hidden?'none':'table-cell',width:header.width,
  59. left:fixedLeft(index),
  60. zIndex:index<=columnFixed-1?9:'unset',
  61. position:index<=columnFixed-1?'sticky':'unset',
  62. backgroundColor:'inherit'}">
  63. </uni-td>
  64. </uni-tr>
  65. </uni-table>
  66. <uni-load-more v-show="showLoadMore" :status="loadMore"></uni-load-more>
  67. </scroll-view>
  68. </view>
  69. </template>
  70. <script>
  71. import lyyProgress from './lyy-progress'
  72. /**
  73. * lyyTable ver1.3.3
  74. * @description lyyTable表格组件 ver1.3.3
  75. */
  76. export default {
  77. name: "baseDateTable",
  78. components: {
  79. lyyProgress
  80. },
  81. data() {
  82. return {
  83. lastSortItem: '', //上一次排序列
  84. sortWay: 'none', //默认无排序
  85. sortIndex: 0,
  86. sortContents: [], //排序时的表格内容
  87. footContent: {},
  88. scrollHeight: '',
  89. theadHeight: '',
  90. tabHeight: null,
  91. }
  92. },
  93. props: {
  94. faultDrawer: {
  95. type: Function
  96. },
  97. //显示加载
  98. loading: {
  99. type: Boolean,
  100. default: false
  101. },
  102. //上拉加载文字,参考uni-load-more
  103. loadMore: {
  104. type: String,
  105. default: 'more'
  106. },
  107. //是否显示上拉加载组件
  108. showLoadMore: {
  109. default: Boolean,
  110. default: false
  111. },
  112. //固定表头
  113. headerFixed: {
  114. type: Boolean,
  115. default: false
  116. },
  117. //固定首列 ver1.3.3弃用
  118. /*firstColumnFixed: {
  119. type: Boolean,
  120. default: false
  121. },*/
  122. //固定列数 ver1.3.3新增
  123. columnFixed: {
  124. type: Number,
  125. default: 0
  126. },
  127. //排序方式
  128. sortWays: {
  129. type: Array,
  130. default: () => ['none', 'asc', 'desc']
  131. },
  132. //数据为空时的占位符
  133. emptyString: {
  134. type: String,
  135. default: '-'
  136. },
  137. //表头
  138. headers: {
  139. type: Array,
  140. default: [],
  141. },
  142. //表格数据
  143. contents: {
  144. type: Array,
  145. default: []
  146. },
  147. //合计列
  148. totalRow: {
  149. type: Array,
  150. default: () => []
  151. },
  152. //是否编辑
  153. isEdit:{
  154. type:Number,
  155. default:0
  156. }
  157. },
  158. mounted() {
  159. //uni.setStorageSync('contents',this.contents)
  160. this.sortContents = JSON.parse(JSON.stringify(this.contents))
  161. this.renderContents()
  162. this.createTotalRow()
  163. //ver 1.2.0 修复 uni-table width 问题
  164. /*this.$nextTick(() => {
  165. console.log('abc',this.$refs)
  166. const query = uni.createSelectorQuery().in(this)
  167. query.select('.uni-table').boundingClientRect(dom=>{
  168. console.log(123456,dom)
  169. }).exec()
  170. this.$refs['uni-table'].removeAttribute('style')
  171. })*/
  172. //ver 1.2.0 新增 固定表头
  173. /*if (this.headerFixed) {
  174. /*var wHeight = document.body.clientHeight
  175. var tablePoseY = document.getElementById('lyy-tbody').getBoundingClientRect().y
  176. document.getElementById('lyy-tbody').style.height = wHeight - tablePoseY + 'px'
  177. const query = uni.createSelectorQuery().in(this)
  178. query.select('#lyy-thead').boundingClientRect(dom => {
  179. console.log(dom)
  180. this.theadHeight = dom.height + 'px'
  181. }).exec()
  182. }*/
  183. },
  184. watch: {
  185. contents: {
  186. handler(value) {
  187. // console.log(value)
  188. this.sortContents = JSON.parse(JSON.stringify(value))
  189. this.renderContents()
  190. this.createTotalRow()
  191. this.lastSortItem = ''
  192. this.sortWay = 'none'
  193. for (var header of this.headers) {
  194. this.renderTotalRow(header)
  195. }
  196. this.$forceUpdate()
  197. if (value.length > 9) {
  198. this.tabHeight = 'container'
  199. } else {
  200. this.tabHeight = null
  201. }
  202. },
  203. deep: true
  204. },
  205. //监听排序变化
  206. sortChange(value) {
  207. var that = this
  208. var contents = JSON.parse(JSON.stringify(that.contents))
  209. switch (value.sortWay) {
  210. case 'none':
  211. that.sortContents = contents
  212. this.renderContents()
  213. break
  214. case 'asc': //正序
  215. that.sortContents = that.sortContents.sort(function(a, b) {
  216. //需要排序的列为数字时直接计算
  217. if (!isNaN(parseFloat(a[that.lastSortItem])) && !isNaN(parseFloat(b[that
  218. .lastSortItem]))) {
  219. return a[that.lastSortItem] - b[that.lastSortItem]
  220. }
  221. //非数字转为ASCII排序
  222. else {
  223. return a[that.lastSortItem].charCodeAt() - b[that.lastSortItem].charCodeAt()
  224. }
  225. })
  226. break
  227. case 'desc': //倒序
  228. that.sortContents = that.sortContents.sort(function(a, b) {
  229. if (!isNaN(parseFloat(a[that.lastSortItem])) && !isNaN(parseFloat(b[that
  230. .lastSortItem]))) {
  231. return b[that.lastSortItem] - a[that.lastSortItem]
  232. } else {
  233. return b[that.lastSortItem].charCodeAt() - a[that.lastSortItem].charCodeAt()
  234. }
  235. })
  236. break
  237. }
  238. that.$forceUpdate()
  239. }
  240. },
  241. computed: {
  242. //将排序方式、上次排序列作为一个整体进行监听,不然会出现切换排序列不排序的现象
  243. sortChange() {
  244. var {
  245. sortWay,
  246. lastSortItem
  247. } = this
  248. return {
  249. sortWay,
  250. lastSortItem
  251. }
  252. }
  253. },
  254. methods: {
  255. //触发充电机仓位编辑
  256. EditCangData(row){
  257. this.$emit('EditCangData', row)
  258. },
  259. //删除仓位信息
  260. DelCangData(row) {
  261. this.$emit('DelCangData', row.id)
  262. },
  263. //点击排序表头时存储上次排序列名,并循环切换排序方式
  264. doSort(item) {
  265. if (item.sort) {
  266. if (this.lastSortItem !== item.key) {
  267. this.lastSortItem = item.key
  268. this.sortIndex = 0
  269. this.sortIndex++
  270. this.sortWay = this.sortWays[this.sortIndex]
  271. } else {
  272. if (this.sortIndex < 2) {
  273. this.sortIndex++
  274. this.sortWay = this.sortWays[this.sortIndex]
  275. } else {
  276. this.sortIndex = 0
  277. this.sortWay = this.sortWays[0]
  278. }
  279. }
  280. }
  281. },
  282. //表格内容渲染
  283. renderContents() {
  284. const headers = this.headers
  285. //防止修改穿透
  286. var contents = JSON.parse(JSON.stringify(this.contents))
  287. //var contents=uni.getStorageSync('contents')
  288. let sortContents = JSON.parse(JSON.stringify(this.sortContents))
  289. var newArr = []
  290. var result = ''
  291. sortContents.forEach(function(content, index) {
  292. var item = content
  293. headers.forEach(function(header) {
  294. //字符类型格式化
  295. if (typeof header.format !== 'undefined' && (header.format.type === 'string' ||
  296. header.format.type === 'html')) {
  297. var template = header.format.template
  298. var keys = header.format.keys
  299. //console.log(typeof template)
  300. if (typeof template === 'function') {
  301. var arg = []
  302. keys.forEach((el, i) => {
  303. arg.push(contents[index][el])
  304. })
  305. result = template(arg)
  306. //console.log(result)
  307. } else {
  308. keys.forEach((el, i) => {
  309. var value = contents[index][el]
  310. var reg = new RegExp('\\{' + i + '}', 'g')
  311. template = template.replace(reg, value)
  312. })
  313. result = template
  314. }
  315. item[header.key] = result
  316. }
  317. //计算类型格式化
  318. else if (typeof header.format !== 'undefined' && (header.format.type ===
  319. 'compute' || header.format.type === 'progress')) {
  320. //console.log(header.format.template)
  321. var temp = header.format.template
  322. var keys = header.format.keys
  323. keys.forEach((el, i) => {
  324. var reg = new RegExp('\\{' + i + '}', 'g')
  325. temp = temp.replace(reg, contents[index][el])
  326. })
  327. //console.log(temp)
  328. item[header.key] = eval(temp)
  329. //this.sortContents[index][header.key]=result
  330. }
  331. })
  332. newArr.push(item)
  333. })
  334. this.sortContents = newArr
  335. },
  336. createTotalRow() {
  337. if (this.totalRow.length > 0 && this.sortContents[0] !== undefined) {
  338. /*var obj = {...this.contents[0]}
  339. console.log(obj)
  340. for (var i in obj) {
  341. this.footContent[i] = obj[i]
  342. }*/
  343. this.footContent = JSON.parse(JSON.stringify(this.sortContents[0]))
  344. for (var i in this.footContent) {
  345. var result = 0
  346. if (this.sortContents.length > 0) {
  347. for (var j in this.sortContents) {
  348. result += parseFloat(this.sortContents[j][i]) || 0
  349. }
  350. }
  351. this.footContent[i] = result
  352. }
  353. }
  354. },
  355. //合计列渲染
  356. renderTotalRow(header) {
  357. var content = JSON.parse(JSON.stringify(this.footContent))
  358. var result = this.emptyString
  359. if (this.totalRow.indexOf(header.key) > -1) {
  360. if (typeof header.format !== 'undefined' && header.format.type === 'progress') {
  361. var temp = header.format.template
  362. var keys = header.format.keys
  363. for (var index in keys) {
  364. var reg = new RegExp('\\{' + index + '}', 'g')
  365. temp = temp.replace(reg, content[keys[index]])
  366. }
  367. result = eval(temp)
  368. result = isNaN(result) ? 0 : result.toFixed(2)
  369. } else {
  370. if (content[header.key] != null && !isNaN(content[header.key])) {
  371. result = content[header.key]
  372. }
  373. }
  374. }
  375. return result
  376. },
  377. //行点击事件
  378. rowClick(data) {
  379. this.$emit('rowClick', data)
  380. },
  381. //上拉加载事件
  382. scrolltolower(e) {
  383. if (e.detail.direction == 'bottom') {
  384. this.$emit('onPullup')
  385. }
  386. },
  387. //固定列left计算
  388. fixedLeft(index) {
  389. var headers = this.headers.filter(item => !item.hidden)
  390. var left = 'calc(0px'
  391. for (var i = 1; i < index + 1; i++) {
  392. left += ' + ' + headers[i - 1].width
  393. }
  394. left += ')'
  395. return left
  396. }
  397. }
  398. }
  399. </script>
  400. <style scoped lang="scss">
  401. @import './css/iconfont.css';
  402. .boxlist {
  403. width: 100%;
  404. height: 5px;
  405. position: sticky;
  406. z-index: 999;
  407. background: #111827;
  408. top: -5px;
  409. }
  410. .table_box {
  411. height: 464px;
  412. overflow: hidden;
  413. }
  414. .fixed-head {
  415. position: sticky;
  416. top: 0px;
  417. z-index: 99;
  418. height: 50px;
  419. }
  420. .warnColor {
  421. color: #636C97;
  422. }
  423. .editColor {
  424. color: #55B693;
  425. display: flex;
  426. justify-content:space-between;
  427. }
  428. .container {
  429. height: 100%;
  430. }
  431. .uni-table-scroll {
  432. overflow: unset !important;
  433. height: 576px;
  434. }
  435. .no_data {
  436. position: absolute;
  437. width: 100%;
  438. background-color: #FFF;
  439. height: 50px;
  440. text-align: center;
  441. line-height: 50px;
  442. }
  443. .uni-table-th {
  444. width: 142px;
  445. }
  446. .uni-table-td {
  447. width: 142px;
  448. white-space: nowrap;
  449. text-overflow: ellipsis;
  450. overflow: hidden;
  451. }
  452. ::v-deep .uni-table-text {
  453. position: fixed;
  454. right: 0;
  455. left: 240px;
  456. text-align: center;
  457. font-size: 14px;
  458. color: #999;
  459. width: 1001px;
  460. }
  461. /*修复滑动时偏移1px问题*/
  462. .table--border {
  463. border-top: none;
  464. border-left: none;
  465. }
  466. ::v-deep .table--border {
  467. border: none !important;
  468. }
  469. ::v-deep .uni-load-more {
  470. position: fixed;
  471. bottom: 20px;
  472. z-index: 9999;
  473. width: 1000px;
  474. text-align: center;
  475. .uni-load-more__text {
  476. font-size: 14px;
  477. color: #636C97 !important;
  478. }
  479. }
  480. ::v-deep .uni-table {
  481. background: none;
  482. .empty-border {
  483. border: none;
  484. height: 80px;
  485. line-height: 80px;
  486. }
  487. .uni-table-th {
  488. color: #FFFFFF;
  489. }
  490. .uni-table-td {
  491. font-weight: 400;
  492. color: #FFFFFF;
  493. font-size: 14px;
  494. height: 50px;
  495. line-height: 32px;
  496. }
  497. .uni-table-tr:nth-child(n + 2):hover {
  498. background-color: #1e2f36;
  499. }
  500. }
  501. #lyy-thead {
  502. /*min-width: 750upx;*/
  503. display: table;
  504. height: 50px;
  505. background: #1D283E;
  506. box-shadow: 0px 1px 0px 0px #2F3C86;
  507. font-weight: 600;
  508. color: #FFFFFF;
  509. font-size: 14px;
  510. }
  511. // ::v-deep .table--stripe .uni-table-tr:nth-child(2n + 1) {
  512. // background-color: #141B2B;
  513. // box-shadow: 0px 1px 0px 0px #292D46, 0px -1px 0px 0px #292D46;
  514. // }
  515. .table--stripe .uni-table-tr:nth-child(2n + 3) {
  516. background-color: #111827;
  517. }
  518. ::v-deep .uni-table-tr {
  519. background-color: #141B2B;
  520. // box-shadow: 0px 1px 0px 0px #292D46;
  521. border-bottom: #292D46 solid 1px;
  522. }
  523. </style>