DetailPageTable.vue 3.5 KB

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