index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="list-page">
  3. <el-tabs v-model="tabsValue" type="card" class="demo-tabs" @tab-change="changeTab" v-if="tabList && tabList.length > 0">
  4. <el-tab-pane v-for="item in tabList" :key="item.name" :label="item.title" :name="item.name"></el-tab-pane>
  5. </el-tabs>
  6. <div class="search-div">
  7. <Search :column="searchColumn" @submit="searchSubmit" @reset="resetForm" :searchBtns="searchBtns" :tabsValue="tabsValue"/>
  8. </div>
  9. <div class="table-div">
  10. <Table :isScope="isScope" :tableKey="tableKey" ref="tableRef" :column="tableColumn" :data="tableData.list" :loading="loading" :scopeBtns="scopeBtns" :isSelect="isSelect" />
  11. </div>
  12. <div class="pagination-div">
  13. <slot name="footerLeft"></slot>
  14. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="queryParams.pageNum"
  15. v-model:limit="queryParams.pageSize" @pagination="getList" />
  16. </div>
  17. </div>
  18. </template>
  19. <script setup>
  20. import { computed, onMounted, reactive, watch } from 'vue';
  21. import Search from './Search.vue';
  22. import Table from './Table.vue';
  23. import { ElMessage } from 'element-plus'
  24. import { ref } from 'vue';
  25. const props = defineProps({
  26. column: {
  27. type: Object,
  28. default: [],
  29. required: true
  30. },
  31. scopeBtns: {
  32. type: Array,
  33. default: [],
  34. },
  35. tableApi: {
  36. type: Function,
  37. default: () => { },
  38. required: true
  39. },
  40. searchBtns: {
  41. type: Array,
  42. default: [],
  43. },
  44. tableKey:{
  45. type: String,
  46. default: 'id',
  47. },
  48. tabList:{
  49. type: Array,
  50. default:[]
  51. },
  52. tabsearchKey:{
  53. type: String,
  54. default: ''
  55. },
  56. isSelect:{
  57. type: Boolean,
  58. default: true
  59. },
  60. defaultTab:{
  61. type: String,
  62. default: '2'
  63. },
  64. isScope:{
  65. type: Boolean,
  66. default: true
  67. },
  68. })
  69. const tabsValue = ref('2')
  70. const queryParams = reactive({
  71. pageNum: 1,
  72. pageSize: 10
  73. })
  74. const tableData = ref({
  75. total: 0,
  76. list: []
  77. })
  78. const loading = ref(false)
  79. const tableRef = ref(null)
  80. const searchColumn = computed(() => {
  81. return props.column.filter(item => item.isSearch);
  82. })
  83. const tableColumn = computed(() => {
  84. return props.column
  85. })
  86. //表单提交
  87. const searchSubmit = (parmas) => {
  88. getList(parmas)
  89. }
  90. //表单重置
  91. const resetForm = () => {
  92. Object.assign(queryParams, {
  93. pageNum: 1,
  94. pageSize: 10
  95. })
  96. //清除选中的row
  97. tableRef.value.resetIds();
  98. getList({})
  99. }
  100. const changeTab = (tab) => {
  101. console.log('tab', tab);
  102. resetForm();
  103. }
  104. watch(() =>tabsValue.value,()=>{
  105. sessionStorage.setItem('tabsValue',tabsValue.value)
  106. } )
  107. const getList = async (parmas) => {
  108. try {
  109. loading.value = true;
  110. const searchData = {...queryParams, ...parmas}
  111. if(props.tabsearchKey){
  112. searchData[props.tabsearchKey] = tabsValue.value;
  113. }
  114. console.log('查询条件',queryParams,parmas);
  115. const response = await props.tableApi(searchData);
  116. tableData.value = {
  117. total: response.total,
  118. list: response.rows
  119. }
  120. } catch (error) {
  121. console.error('error', error);
  122. } finally {
  123. loading.value = false;
  124. }
  125. }
  126. const ids = computed(() =>{
  127. const _d = tableRef.value.ids;
  128. if(_d && _d.length <= 0){
  129. ElMessage({
  130. message: '请选择需要操作的数据!',
  131. type: 'warning',
  132. })
  133. return []
  134. }
  135. return tableRef.value.ids
  136. })
  137. const handleExist = (value) => {
  138. const f = props.tabList.filter(item => item.name === value);
  139. return f && f.length > 0
  140. }
  141. onMounted(() => {
  142. const tabs = sessionStorage.getItem('tabsValue');
  143. if(tabs && handleExist(tabs)){
  144. tabsValue.value = tabs;
  145. }else{
  146. tabsValue.value = props.defaultTab;
  147. }
  148. getList();
  149. })
  150. defineExpose({
  151. ids,
  152. resetForm,
  153. tabsValue
  154. })
  155. </script>
  156. <style lang='scss' scoped>
  157. .list-page {
  158. padding: 20px;
  159. }
  160. </style>