index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div class="table-layout">
  3. <!-- 表头 -->
  4. <ul class="t-header" :class="{'bottom-line':showLine}">
  5. <!-- 复选框 -->
  6. <li v-if="selection" class="check-cls"><span @click="lock=false,selectAll=!selectAll" class="fdcheck" :class="{check_active:selectAll}"></span></li>
  7. <!-- -->
  8. <li v-for="(item,i) in header" :key="i" :style="{textAlign: item.textAlign, width:item.width ?item.width+'px':(100/header.length)+'%'}">
  9. <slot :data='item' name='header'></slot>
  10. </li>
  11. </ul>
  12. <!-- 表格内容区域 -->
  13. <div class="t-con">
  14. <ul class="t-item" active-txt :class="{'bottom-line': showLine}" v-for="(item, i) in fixdata" :key="i">
  15. <!-- 复选框 -->
  16. <li v-if="selection" class="check-cls" ><span @click="selectItem(item,i)" class="fdcheck" :class="{check_active:item.hasAuth}"></span></li>
  17. <!-- -->
  18. <li v-for="(sub,j) in header" :class="{ themetxt: sub.fontweight }" :key='j' :style="{textAlign:item.textAlign, width:sub.width ? sub.width+'px':(100/header.length)+'%'}">
  19. <slot :data='item[sub.key]' :item="item" :sub='sub' name='item'></slot>
  20. </li>
  21. </ul>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import { mapState } from 'vuex'
  27. export default {
  28. props: [
  29. // 每一项可配置textAlign
  30. 'data',
  31. // 表头数据:各列名称和格列的配置(fontweight: Boolean,某一列是否是关键列;canclick: Booean, 是否可进行各种操作;)。
  32. 'header',
  33. // 是否显示复选框
  34. 'selection',
  35. // 是否显示每行之间的分隔线
  36. 'showLine'
  37. ],
  38. data () {
  39. return {
  40. innerW: window.innerWidth,
  41. selectAll: false,
  42. lock: true
  43. }
  44. },
  45. computed: {
  46. ...mapState({
  47. // props中data中hasAuth一律写成false,即未选中。
  48. fixdata () {
  49. let data = this.data && this.data.map(item => {
  50. item.hasAuth = false
  51. return item
  52. })
  53. return data
  54. }
  55. })
  56. },
  57. watch: {
  58. data () {
  59. this.selectAll = false
  60. },
  61. selectAll: function (newVal) {
  62. if (!this.lock) {
  63. this.fixdata.forEach(item => {
  64. item.hasAuth = newVal
  65. })
  66. this.handleSelect()
  67. }
  68. }
  69. },
  70. methods: {
  71. handleSelect () {
  72. let arr = this.fixdata.filter(item => {
  73. return item.hasAuth
  74. })
  75. this.$emit('selection-change', arr)
  76. },
  77. selectItem (item, i) {
  78. item.hasAuth = !item.hasAuth
  79. this.$set(this.fixdata, i, item)
  80. this.lock = true
  81. this.selectAll = true
  82. this.fixdata.forEach(sub => {
  83. if (!sub.hasAuth) {
  84. this.selectAll = false
  85. }
  86. })
  87. this.handleSelect()
  88. }
  89. },
  90. mounted () {
  91. }
  92. }
  93. </script>
  94. <style lang="less" scoped>
  95. @import './style.less';
  96. </style>