baseDateTable.vue 16 KB

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