index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="table-container">
  3. <div class="table_box">
  4. <div class="table-title-btn">
  5. <slot name="table-title-btn"></slot>
  6. </div>
  7. <el-table
  8. ref="multipleTableRef"
  9. :header-cell-style="{ 'background-color': '#f2f2f2' }"
  10. :data="props.tableData"
  11. style="width: 100%"
  12. row-key="id"
  13. :tree-props="props.treeProps"
  14. @selection-change="selectChange"
  15. @select-all="selectAllChange"
  16. >
  17. <el-table-column
  18. type="selection"
  19. v-if="showSelect"
  20. :selectable="selectable"
  21. width="55"
  22. />
  23. <el-table-column
  24. v-if="showIndex"
  25. type="index"
  26. label="序号"
  27. width="55"
  28. />
  29. <el-table-column
  30. v-for="column in columns"
  31. :key="column.prop"
  32. :prop="column.prop"
  33. :label="column.label"
  34. :sortable="column.sortable ? 'custom' : false"
  35. :width="column.width"
  36. >
  37. <template #header>
  38. <slot name="search" :slotData="column"></slot>
  39. </template>
  40. <template #default="scope">
  41. <component v-if="column.formatter" :is="column.formatter(scope.row, column, scope.row[column.prop], scope.$index)" class="routerlick" />
  42. <span v-else>{{ scope.row[column.prop] }}</span>
  43. </template>
  44. </el-table-column>
  45. <el-table-column label="操作" v-if="showOperation">
  46. <template #default="scope">
  47. <slot name="operation" :operationData="scope"></slot>
  48. </template>
  49. <!-- <template #default="scope">
  50. <div v-if="scope.row._edit">
  51. <el-button type="primary" @click="handleSave(scope)"> 保存 </el-button>
  52. <el-button @click="handleCancle(scope)"> 取消 </el-button>
  53. </div>
  54. <el-button v-else type="primary" @click="handleEdit(scope)"> 编辑 </el-button>
  55. </template> -->
  56. </el-table-column>
  57. </el-table>
  58. </div>
  59. <div class="page_box" v-show="props.pageInfo.show">
  60. <el-pagination
  61. v-model:current-page="props.pageInfo.pageNum"
  62. v-model:page-size="props.pageInfo.pageSize"
  63. :page-sizes="props.pageInfo.sizes"
  64. layout="total, sizes, prev, pager, next, jumper"
  65. :total="props.pageInfo.total"
  66. @size-change="handleSizeChange"
  67. @current-change="handleCurrentChange"
  68. />
  69. </div>
  70. </div>
  71. </template>
  72. <script lang="ts" setup>
  73. import { defineProps, ref, onMounted, defineEmits } from "vue";
  74. import type { TableInstance } from "element-plus";
  75. interface TableRow {
  76. [key: string]: any;
  77. }
  78. interface TagTypes {
  79. [key: string]: string;
  80. }
  81. const emit = defineEmits(['getList', 'selectAllChange','selectChange']);
  82. interface Props{
  83. tableData: Array<TableRow>;
  84. columns: {
  85. prop: string;
  86. label: string;
  87. sortable?: boolean;
  88. width?: string;
  89. style?: string;
  90. formatter?: (row: any, column: any, cellValue: any, index: number) => any;
  91. }[];
  92. pageInfo: {
  93. pageNum: number;
  94. pageSize: number;
  95. sizes: number[];
  96. total: number;
  97. show:boolean;
  98. };
  99. showSelect: boolean;
  100. showIndex: boolean;
  101. showOperation:boolean;
  102. treeProps:{}
  103. }
  104. const props = withDefaults(defineProps<Props>(),{
  105. showOperation:true,
  106. })
  107. const multipleTableRef = ref<TableInstance>();
  108. const TableData = ref<TableRow[]>();
  109. onMounted(() => {
  110. let counter = 1;
  111. TableData.value = props.tableData.map((row) => ({
  112. ...row,
  113. _edit: false, // 设置默认非编辑状态
  114. counter: counter++,
  115. }));
  116. });
  117. const selectable = () => true;
  118. const Tagvalue = ref("");
  119. const tagTypes: TagTypes = {
  120. 公司: "primary",
  121. 甲方: "success",
  122. 合作伙伴: "primary",
  123. };
  124. const handleSizeChange = (val: number) => {
  125. props.pageInfo.pageSize = val;
  126. emit("getList", props.pageInfo);
  127. };
  128. const handleCurrentChange = (val: number) => {
  129. props.pageInfo.pageNum = val;
  130. emit("getList", props.pageInfo);
  131. };
  132. const getTagType = (tag: string) => {
  133. return tagTypes[tag] || tagTypes["公司"];
  134. };
  135. const handleEdit = (scope: { row: TableRow }) => {
  136. Tagvalue.value = scope.row.Tag || "";
  137. scope.row._edit = true;
  138. };
  139. const handleSave = (scope: { row: TableRow }) => {
  140. scope.row.Tag = Tagvalue.value;
  141. scope.row._edit = false;
  142. };
  143. const handleCancle = (scope: { row: TableRow }) => {
  144. scope.row._edit = false;
  145. };
  146. const headClass = (scope: { row: TableRow }) => {
  147. // return 'background:#F8F8F9'
  148. };
  149. //多选
  150. const newSelectionList = ref<string[]>([]);
  151. const selectAllChange = (value: any[]) => {
  152. if (value.length>0) {
  153. value.map(val => {
  154. newSelectionList.value.push(val.id)
  155. })
  156. } else {
  157. newSelectionList.value = [];
  158. }
  159. emit('selectAllChange', newSelectionList.value)
  160. }
  161. const selectChange = (value: any[]) => {
  162. if (value.length>0) {
  163. value.map(val => {
  164. newSelectionList.value.push(val.id)
  165. })
  166. } else {
  167. newSelectionList.value = [];
  168. }
  169. emit('selectChange', newSelectionList.value)
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .table-container {
  174. // width: 100%;
  175. // height: auto;
  176. margin: 5px;
  177. display: flex;
  178. flex-direction: column;
  179. }
  180. .table_box {
  181. flex: 1;
  182. margin-bottom: 10px;
  183. overflow-y: auto;
  184. .table_item_container {
  185. margin-left: 20px;
  186. padding: 0;
  187. .table_item {
  188. margin: 5px 0;
  189. }
  190. }
  191. :deep(.cell) {
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. }
  196. .table-title-btn {
  197. margin-bottom: 20px;
  198. :deep(.el-button) {
  199. margin-right: 20px;
  200. }
  201. }
  202. }
  203. .page_box {
  204. display: flex;
  205. align-items: center;
  206. justify-content: flex-end;
  207. }
  208. </style>