Table.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="table">
  3. <table>
  4. <thead>
  5. <tr>
  6. <td>时间</td>
  7. <td>上传平台</td>
  8. <td>链接上传数量</td>
  9. <td>操作</td>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr
  14. v-for="(obj, index) in tableData"
  15. :key="index"
  16. :class="{ table_gray: index % 2 === 0 }"
  17. >
  18. <td>{{ obj.publishMonth || "" }}</td>
  19. <td>{{ obj.publishPlatformName || selectedplateForm.platformName }}</td>
  20. <td style="width: 20%">
  21. <p v-if="!editFlag[index]">{{ obj.publishCount || "" }}</p>
  22. <input type="text" v-model="newName" v-else />
  23. </td>
  24. <td style="width: 25%" class="operation">
  25. <span @click="edit(index)" v-if="!editFlag[index]">编辑</span>
  26. <span @click="submitEdit(index)" v-else>确定</span>
  27. <span @click="showModal(index)">删除</span>
  28. </td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. <DeleteModal
  33. @detlet_data="detletData"
  34. @hide_modal="closeModal"
  35. :modalFlag="deleteModalFlag"
  36. ></DeleteModal>
  37. </div>
  38. </template>
  39. <script>
  40. import DeleteModal from "../DeleteModal";
  41. export default {
  42. props: {
  43. // 表数据
  44. tableData: {
  45. type: Array,
  46. default: () => {
  47. return [];
  48. },
  49. },
  50. // 表格一页共有多少数据
  51. pageSize: {
  52. type: Number,
  53. default: 2,
  54. },
  55. selectedplateForm: {
  56. type: Object,
  57. default: () => {
  58. return {}
  59. }
  60. }
  61. },
  62. components: {
  63. DeleteModal,
  64. },
  65. data() {
  66. return {
  67. deleteModalFlag: false,
  68. index: 0,
  69. editFlag: [],
  70. newName: "", // 编辑之后的值
  71. };
  72. },
  73. methods: {
  74. edit: function (i) {
  75. this.getEditArr(); // 确保只有一个在编辑
  76. this.newName = this.tableData[i] && this.tableData[i].publishCount;
  77. console.log(this.newName);
  78. this.editFlag.splice(i, 1, true);
  79. },
  80. // 确认编辑
  81. submitEdit: function (i) {
  82. let newCount = +this.newName;
  83. if (!newCount) {
  84. alert("请填写链接上传数量");
  85. } else if (newCount === this.tableData[i].publishCount) {
  86. this.newName = ""; // 没有改
  87. } else if (Math.ceil(newCount) !== newCount || Math.ceil(newCount) <=0) {
  88. alert("链接上传数量必须是大于零的整数");
  89. } else {
  90. this.$emit("edit", i, newCount);
  91. }
  92. this.newName = "";
  93. this.editFlag.splice(i, 1, false);
  94. },
  95. // 点击删除,展示模态框
  96. showModal: function (i) {
  97. this.deleteModalFlag = true;
  98. this.index = i;
  99. },
  100. // 模态框确认删除
  101. detletData: function () {
  102. this.deleteModalFlag = false;
  103. this.$emit("delet_data", this.index);
  104. },
  105. // 模态框取消
  106. closeModal: function () {
  107. this.deleteModalFlag = false;
  108. },
  109. // 生成点击编辑按钮所用额flag,
  110. getEditArr: function () {
  111. let arr = new Array(this.pageSize).fill(false);
  112. this.editFlag = arr;
  113. },
  114. },
  115. created() {
  116. this.getEditArr();
  117. },
  118. };
  119. </script>
  120. <style scoped lang="less">
  121. .table {
  122. background-color: #fff;
  123. width: 100%;
  124. margin-top: 10px;
  125. text-align: center;
  126. table {
  127. width: 100%;
  128. border-collapse: collapse;
  129. border: none;
  130. thead {
  131. td {
  132. background: #8d9092;
  133. height: 36px;
  134. color: #fff;
  135. border: 1px solid #ccc;
  136. }
  137. }
  138. tbody {
  139. td {
  140. height: 36px;
  141. border: 1px solid #ccc;
  142. }
  143. .operation {
  144. span {
  145. color: #00f;
  146. margin: 0 20px;
  147. &:hover {
  148. cursor: pointer;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. .table_gray {
  156. background: #f5f5f5;
  157. }
  158. </style>