cangDateTable.vue 15 KB

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