SpuTableSelect.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <Dialog v-model="dialogVisible" :appendToBody="true" title="选择商品" width="70%">
  3. <ContentWrap>
  4. <el-row :gutter="20" class="mb-10px">
  5. <el-col :span="6">
  6. <el-input
  7. v-model="queryParams.name"
  8. class="!w-240px"
  9. clearable
  10. placeholder="请输入商品名称"
  11. @keyup.enter="handleQuery"
  12. />
  13. </el-col>
  14. <el-col :span="6">
  15. <el-tree-select
  16. v-model="queryParams.categoryId"
  17. :data="categoryTreeList"
  18. :props="defaultProps"
  19. check-strictly
  20. class="w-1/1"
  21. node-key="id"
  22. placeholder="请选择商品分类"
  23. />
  24. </el-col>
  25. <el-col :span="6">
  26. <el-date-picker
  27. v-model="queryParams.createTime"
  28. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
  29. class="!w-240px"
  30. end-placeholder="结束日期"
  31. start-placeholder="开始日期"
  32. type="daterange"
  33. value-format="YYYY-MM-DD HH:mm:ss"
  34. />
  35. </el-col>
  36. <el-col :span="6">
  37. <el-button @click="handleQuery">
  38. <Icon class="mr-5px" icon="ep:search" />
  39. 搜索
  40. </el-button>
  41. <el-button @click="resetQuery">
  42. <Icon class="mr-5px" icon="ep:refresh" />
  43. 重置
  44. </el-button>
  45. </el-col>
  46. </el-row>
  47. <el-table v-loading="loading" :data="list" show-overflow-tooltip>
  48. <!-- 多选模式 -->
  49. <el-table-column key="2" type="selection" width="55" v-if="multiple">
  50. <template #header>
  51. <el-checkbox
  52. :value="allChecked && checkedPageNos.indexOf(queryParams.pageNo) > -1"
  53. @change="handleCheckAll"
  54. />
  55. </template>
  56. <template #default="{ row }">
  57. <el-checkbox
  58. :value="checkedSpuIds.indexOf(row.id) > -1"
  59. @change="(checked: boolean) => handleCheckOne(checked, row)"
  60. />
  61. </template>
  62. </el-table-column>
  63. <!-- 单选模式 -->
  64. <el-table-column label="#" width="55" v-else>
  65. <template #default="{ row }">
  66. <el-radio :label="row.id" v-model="selectedSpuId" @change="handleSingleSelected(row)"
  67. >&nbsp;</el-radio
  68. >
  69. </template>
  70. </el-table-column>
  71. <el-table-column key="id" align="center" label="商品编号" prop="id" min-width="60" />
  72. <el-table-column label="商品图" min-width="80">
  73. <template #default="{ row }">
  74. <el-image
  75. :src="row.picUrl"
  76. class="h-30px w-30px"
  77. :preview-src-list="[row.picUrl]"
  78. preview-teleported
  79. />
  80. </template>
  81. </el-table-column>
  82. <el-table-column label="商品名称" min-width="200" prop="name" />
  83. <el-table-column label="商品分类" min-width="100" prop="categoryId">
  84. <template #default="{ row }">
  85. <span>{{ categoryList?.find((c) => c.id === row.categoryId)?.name }}</span>
  86. </template>
  87. </el-table-column>
  88. </el-table>
  89. <!-- 分页 -->
  90. <Pagination
  91. v-model:limit="queryParams.pageSize"
  92. v-model:page="queryParams.pageNo"
  93. :total="total"
  94. @pagination="getList"
  95. />
  96. </ContentWrap>
  97. <template #footer v-if="multiple">
  98. <el-button type="primary" @click="handleEmitChange">确 定</el-button>
  99. <el-button @click="dialogVisible = false">取 消</el-button>
  100. </template>
  101. </Dialog>
  102. </template>
  103. <script lang="ts" setup>
  104. import { ElTable } from 'element-plus'
  105. import { defaultProps, handleTree } from '@/utils/tree'
  106. import * as ProductCategoryApi from '@/api/mall/product/category'
  107. import * as ProductSpuApi from '@/api/mall/product/spu'
  108. import { propTypes } from '@/utils/propTypes'
  109. type Spu = Required<ProductSpuApi.Spu>
  110. defineOptions({ name: 'SpuTableSelect' })
  111. const props = defineProps({
  112. // 多选
  113. multiple: propTypes.bool.def(false)
  114. })
  115. const total = ref(0) // 列表的总页数
  116. const list = ref<Spu[]>([]) // 列表的数据
  117. const loading = ref(false) // 列表的加载中
  118. const dialogVisible = ref(false) // 弹窗的是否展示
  119. const queryParams = ref({
  120. pageNo: 1,
  121. pageSize: 10,
  122. tabType: 0, // 默认获取上架的商品
  123. name: '',
  124. categoryId: null,
  125. createTime: []
  126. }) // 查询参数
  127. const selectedSpuId = ref() // 选中的商品 spuId
  128. /** 打开弹窗 */
  129. const open = (spus?: Spu[]) => {
  130. if (spus && spus.length > 0) {
  131. // todo check-box不显示选中?
  132. checkedSpus.value = [...spus]
  133. checkedSpuIds.value = spus.map((spu) => spu.id)
  134. } else {
  135. checkedSpus.value = []
  136. checkedSpuIds.value = []
  137. }
  138. allChecked.value = false
  139. checkedPageNos.value = []
  140. dialogVisible.value = true
  141. resetQuery()
  142. }
  143. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  144. /** 查询列表 */
  145. const getList = async () => {
  146. loading.value = true
  147. try {
  148. const data = await ProductSpuApi.getSpuPage(queryParams.value)
  149. list.value = data.list
  150. total.value = data.total
  151. } finally {
  152. loading.value = false
  153. }
  154. }
  155. /** 搜索按钮操作 */
  156. const handleQuery = () => {
  157. queryParams.value.pageNo = 1
  158. getList()
  159. }
  160. /** 重置按钮操作 */
  161. const resetQuery = () => {
  162. queryParams.value = {
  163. pageNo: 1,
  164. pageSize: 10,
  165. tabType: 0, // 默认获取上架的商品
  166. name: '',
  167. categoryId: null,
  168. createTime: []
  169. }
  170. getList()
  171. }
  172. const allChecked = ref(false) //是否全选
  173. const checkedPageNos = ref<number[]>([]) //选中的页码
  174. const checkedSpuIds = ref<number[]>([]) //选中的商品ID
  175. const checkedSpus = ref<Spu[]>([]) //选中的商品
  176. /** 单选中时触发 */
  177. const handleSingleSelected = (row: Spu) => {
  178. emits('change', row)
  179. // 关闭弹窗
  180. dialogVisible.value = false
  181. // 记住上次选择的ID
  182. selectedSpuId.value = row.id
  183. }
  184. /** 多选完成 */
  185. const handleEmitChange = () => {
  186. // 关闭弹窗
  187. dialogVisible.value = false
  188. emits('change', [...checkedSpus.value])
  189. }
  190. /** 确认选择时的触发事件 */
  191. const emits = defineEmits<{
  192. (e: 'change', spu: Spu | Spu[] | any): void
  193. }>()
  194. /** 全选 */
  195. const handleCheckAll = (checked: boolean) => {
  196. debugger
  197. console.log('checkAll', checked)
  198. allChecked.value = checked
  199. const index = checkedPageNos.value.indexOf(queryParams.value.pageNo)
  200. checkedPageNos.value.push(queryParams.value.pageNo)
  201. if (index > -1) {
  202. checkedPageNos.value.splice(index, 1)
  203. }
  204. list.value.forEach((item) => handleCheckOne(checked, item))
  205. }
  206. /** 选中一行 */
  207. const handleCheckOne = (checked: boolean, spu: Spu) => {
  208. if (checked) {
  209. const index = checkedSpuIds.value.indexOf(spu.id)
  210. if (index === -1) {
  211. checkedSpuIds.value.push(spu.id)
  212. checkedSpus.value.push(spu)
  213. }
  214. } else {
  215. const index = checkedSpuIds.value.indexOf(spu.id)
  216. if (index > -1) {
  217. checkedSpuIds.value.splice(index, 1)
  218. checkedSpus.value.splice(index, 1)
  219. }
  220. }
  221. }
  222. const categoryList = ref() // 分类列表
  223. const categoryTreeList = ref() // 分类树
  224. /** 初始化 **/
  225. onMounted(async () => {
  226. await getList()
  227. // 获得分类树
  228. categoryList.value = await ProductCategoryApi.getCategoryList({})
  229. categoryTreeList.value = handleTree(categoryList.value, 'id', 'parentId')
  230. })
  231. </script>