Wtable.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="table_info">
  3. <table class="table">
  4. <thead>
  5. <tr>
  6. <th v-for="(item, index) in thead" :width="item.width" :key="index">
  7. <template v-if="item.name==='全选'">
  8. <input type="checkbox" id="all" :class="classChickbox" :checked="allDisabled" @click="allcheckbox"><label for="all"></label>全选
  9. </template>
  10. <template v-else>
  11. {{item.name}}
  12. </template>
  13. </th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr v-for="(item, index) in data1" :key="index" @dblclick="dblClick(item)">
  18. <td v-for="child in thead" :key="child.id">
  19. <template v-if="child.id === 'checkbox' && child.name==='全选'">
  20. <input type="checkbox" :id="index" :class="classChickbox" :checked="item.isDisabled" @click.stop="checkboxClick(item)"><label :for="index"></label>
  21. </template>
  22. <template v-else>
  23. {{item[child.id] ? (child.type === 'date' ? formatDate(item[child.id]) : item[child.id]) : '-'}}
  24. </template>
  25. </td>
  26. </tr>
  27. <tr v-if="data.length === 0">
  28. <td :colspan="thead.length" style="line-height:50px;">暂无信息!</td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
  33. </template>
  34. <script>
  35. import {formatDate} from "@/utils/date";
  36. export default {
  37. name: 'wtableView',
  38. props: {
  39. // 数据加载
  40. data: {
  41. type: Array,
  42. default: []
  43. },
  44. // 表头信息
  45. thead: {
  46. type: Array,
  47. // default: ["序号", "编号", "公司"]
  48. default: []
  49. },
  50. // 单选框颜色
  51. classChickbox: {
  52. type: String,
  53. default: ''
  54. }
  55. },
  56. methods: {
  57. // 绑定格式化时间
  58. formatDate (type) {
  59. return formatDate(new Date(type), 'yyyy-MM-dd hh:mm:ss')
  60. },
  61. // 全选
  62. allcheckbox () {
  63. let list = new Array()
  64. this.allDisabled = !this.allDisabled
  65. this.data.forEach((value) => {
  66. value.isDisabled = this.allDisabled
  67. if (this.allDisabled === true) {
  68. list.push(value)
  69. }
  70. })
  71. this.$emit('multiple', true, list)
  72. },
  73. //单选
  74. checkboxClick (type) {
  75. type.isDisabled = !type.isDisabled
  76. this.isAllCheck()
  77. this.$emit('clickEvent', type)
  78. },
  79. // 是否全选
  80. isAllCheck () {
  81. let list = new Array()
  82. let count = 0
  83. this.data1.forEach((value) => {
  84. if (value.isDisabled === true) {count++}
  85. })
  86. for (let i = 0; i < this.data1.length; i++) {
  87. if (this.data1[i].isDisabled === true) {
  88. list.push(this.data1[i])
  89. }
  90. }
  91. count === this.data1.length ? this.allDisabled = true : this.allDisabled = false
  92. if (count !== 1) {
  93. this.$emit('multiple', true, list)
  94. } else {
  95. this.$emit('multiple', false, list)
  96. }
  97. },
  98. // 双击事件
  99. dblClick (type) {
  100. this.$emit('dblClickEvent', type)
  101. }
  102. },
  103. watch: {
  104. data: function (newVal) {
  105. this.data1 = newVal
  106. }
  107. },
  108. data () {
  109. return {
  110. data1: this.data,
  111. allDisabled: false
  112. }
  113. }
  114. }
  115. </script>