index.vue 5.5 KB

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