商品中心:修复商品表格选择对话框,在多选时表头内的多选框不显示选中效果的问题

This commit is contained in:
owen 2023-11-21 10:27:29 +08:00
parent e0a731dd86
commit 1bb9df7b3c
2 changed files with 130 additions and 76 deletions

View File

@ -20,7 +20,7 @@
</el-tooltip>
</div>
<!-- 商品选择对话框表格形式 -->
<SpuTableSelect ref="spuTableSelectRef" multiple @change="handleSpuSelected" />
<SpuTableSelect ref="spuTableSelectRef" :multiple="limit != 1" @change="handleSpuSelected" />
</template>
<script lang="ts" setup>
import * as ProductSpuApi from '@/api/mall/product/spu'
@ -88,8 +88,8 @@ const openSpuTableSelect = () => {
* 选择商品后触发
* @param spus 选中的商品列表
*/
const handleSpuSelected = (spus: ProductSpuApi.Spu[]) => {
productSpus.value = spus
const handleSpuSelected = (spus: ProductSpuApi.Spu | ProductSpuApi.Spu[]) => {
productSpus.value = isArray(spus) ? spus : [spus]
emitSpuChange()
}

View File

@ -1,8 +1,14 @@
<template>
<Dialog v-model="dialogVisible" :appendToBody="true" title="选择商品" width="70%">
<ContentWrap>
<el-row :gutter="20" class="mb-10px">
<el-col :span="6">
<el-form
ref="queryFormRef"
:inline="true"
:model="queryParams"
class="-mb-15px"
label-width="68px"
>
<el-form-item label="商品名称" prop="name">
<el-input
v-model="queryParams.name"
class="!w-240px"
@ -10,19 +16,19 @@
placeholder="请输入商品名称"
@keyup.enter="handleQuery"
/>
</el-col>
<el-col :span="6">
</el-form-item>
<el-form-item label="商品分类" prop="categoryId">
<el-tree-select
v-model="queryParams.categoryId"
:data="categoryTreeList"
:props="defaultProps"
check-strictly
class="w-1/1"
class="!w-240px"
node-key="id"
placeholder="请选择商品分类"
/>
</el-col>
<el-col :span="6">
</el-form-item>
<el-form-item label="创建时间" prop="createTime">
<el-date-picker
v-model="queryParams.createTime"
:default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
@ -32,8 +38,8 @@
type="daterange"
value-format="YYYY-MM-DD HH:mm:ss"
/>
</el-col>
<el-col :span="6">
</el-form-item>
<el-form-item>
<el-button @click="handleQuery">
<Icon class="mr-5px" icon="ep:search" />
搜索
@ -42,30 +48,32 @@
<Icon class="mr-5px" icon="ep:refresh" />
重置
</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
<el-table v-loading="loading" :data="list" show-overflow-tooltip>
<!-- 多选模式 -->
<el-table-column key="2" type="selection" width="55" v-if="multiple">
<!-- 1. 多选模式不能使用type="selection"Element会忽略Header插槽 -->
<el-table-column width="55" v-if="multiple">
<template #header>
<el-checkbox
:value="allChecked && checkedPageNos.indexOf(queryParams.pageNo) > -1"
v-model="isCheckAll"
:indeterminate="isIndeterminate"
@change="handleCheckAll"
/>
</template>
<template #default="{ row }">
<el-checkbox
:value="checkedSpuIds.indexOf(row.id) > -1"
@change="(checked: boolean) => handleCheckOne(checked, row)"
v-model="checkedStatus[row.id]"
@change="(checked: boolean) => handleCheckOne(checked, row, true)"
/>
</template>
</el-table-column>
<!-- 单选模式 -->
<!-- 2. 单选模式 -->
<el-table-column label="#" width="55" v-else>
<template #default="{ row }">
<el-radio :label="row.id" v-model="selectedSpuId" @change="handleSingleSelected(row)"
>&nbsp;</el-radio
>
<el-radio :label="row.id" v-model="selectedSpuId" @change="handleSingleSelected(row)">
<!-- 空格不能省略是为了让单选框不显示label如果不指定label不会有选中的效果 -->
&nbsp;
</el-radio>
</template>
</el-table-column>
<el-table-column key="id" align="center" label="商品编号" prop="id" min-width="60" />
@ -102,54 +110,71 @@
</template>
<script lang="ts" setup>
import { ElTable } from 'element-plus'
import { defaultProps, handleTree } from '@/utils/tree'
import * as ProductCategoryApi from '@/api/mall/product/category'
import * as ProductSpuApi from '@/api/mall/product/spu'
import { propTypes } from '@/utils/propTypes'
import { CHANGE_EVENT } from 'element-plus'
type Spu = Required<ProductSpuApi.Spu>
/**
* 商品表格选择对话框
* 1. 单选模式
* 1.1 点击表格左侧的单选框时结束选择并关闭对话框
* 1.2 再次打开时保持选中状态
* 2. 多选模式
* 2.1 点击表格左侧的多选框时记录选中的商品
* 2.2 切换分页时保持商品的选中的状态
* 2.3 点击右下角的确定按钮时结束选择关闭对话框
* 2.4 再次打开时保持选中状态
*/
defineOptions({ name: 'SpuTableSelect' })
const props = defineProps({
//
defineProps({
//
multiple: propTypes.bool.def(false)
})
const total = ref(0) //
const list = ref<Spu[]>([]) //
const loading = ref(false) //
const dialogVisible = ref(false) //
//
const total = ref(0)
//
const list = ref<Spu[]>([])
//
const loading = ref(false)
//
const dialogVisible = ref(false)
//
const queryParams = ref({
pageNo: 1,
pageSize: 10,
tabType: 0, //
//
tabType: 0,
name: '',
categoryId: null,
createTime: []
}) //
const selectedSpuId = ref() // spuId
})
/** 打开弹窗 */
const open = (spus?: Spu[]) => {
if (spus && spus.length > 0) {
// todo check-box
checkedSpus.value = [...spus]
checkedSpuIds.value = spus.map((spu) => spu.id)
} else {
const open = (spuList?: Spu[]) => {
//
checkedSpus.value = []
checkedSpuIds.value = []
checkedStatus.value = {}
isCheckAll.value = false
isIndeterminate.value = false
//
if (spuList && spuList.length > 0) {
checkedSpus.value = [...spuList]
checkedStatus.value = Object.fromEntries(spuList.map(spu => [spu.id, true]))
}
allChecked.value = false
checkedPageNos.value = []
dialogVisible.value = true
resetQuery()
}
defineExpose({ open }) // open
// open
defineExpose({ open })
/** 查询列表 */
const getList = async () => {
@ -158,6 +183,10 @@ const getList = async () => {
const data = await ProductSpuApi.getSpuPage(queryParams.value)
list.value = data.list
total.value = data.total
// checkboxundefinedbool
list.value.forEach( spu => checkedStatus.value[spu.id] = checkedStatus.value[spu.id] || false)
//
calculateIsCheckAll()
} finally {
loading.value = false
}
@ -174,7 +203,8 @@ const resetQuery = () => {
queryParams.value = {
pageNo: 1,
pageSize: 10,
tabType: 0, //
//
tabType: 0,
name: '',
categoryId: null,
createTime: []
@ -182,65 +212,89 @@ const resetQuery = () => {
getList()
}
const allChecked = ref(false) //
const checkedPageNos = ref<number[]>([]) //
const checkedSpuIds = ref<number[]>([]) //ID
const checkedSpus = ref<Spu[]>([]) //
//
const isCheckAll = ref(false)
// &&
const isIndeterminate = ref(false)
//
const checkedSpus = ref<Spu[]>([])
// keyIDvalue
const checkedStatus = ref<Record<string, boolean>>({})
// spuId
const selectedSpuId = ref()
/** 单选中时触发 */
const handleSingleSelected = (row: Spu) => {
emits('change', row)
const handleSingleSelected = (spu: Spu) => {
emits(CHANGE_EVENT, spu)
//
dialogVisible.value = false
// ID
selectedSpuId.value = row.id
selectedSpuId.value = spu.id
}
/** 多选完成 */
const handleEmitChange = () => {
//
dialogVisible.value = false
emits('change', [...checkedSpus.value])
emits(CHANGE_EVENT, [...checkedSpus.value])
}
/** 确认选择时的触发事件 */
const emits = defineEmits<{
(e: 'change', spu: Spu | Spu[] | any): void
change: [spu: Spu | Spu[] | any]
}>()
/** 全选 */
/** 全选/全不选 */
const handleCheckAll = (checked: boolean) => {
debugger
console.log('checkAll', checked)
allChecked.value = checked
const index = checkedPageNos.value.indexOf(queryParams.value.pageNo)
checkedPageNos.value.push(queryParams.value.pageNo)
if (index > -1) {
checkedPageNos.value.splice(index, 1)
isCheckAll.value = checked
isIndeterminate.value = false
list.value.forEach((spu) => handleCheckOne(checked, spu, false))
}
list.value.forEach((item) => handleCheckOne(checked, item))
}
/** 选中一行 */
const handleCheckOne = (checked: boolean, spu: Spu) => {
/**
* 选中一行
* @param checked 是否选中
* @param spu 商品
* @param isCalcCheckAll 是否计算全选
*/
const handleCheckOne = (checked: boolean, spu: Spu, isCalcCheckAll: boolean) => {
if (checked) {
const index = checkedSpuIds.value.indexOf(spu.id)
if (index === -1) {
checkedSpuIds.value.push(spu.id)
checkedSpus.value.push(spu)
}
checkedStatus.value[spu.id] = true
} else {
const index = checkedSpuIds.value.indexOf(spu.id)
const index = findCheckedIndex(spu)
if (index > -1) {
checkedSpuIds.value.splice(index, 1)
checkedSpus.value.splice(index, 1)
}
checkedStatus.value[spu.id] = false
isCheckAll.value = false
}
}
const categoryList = ref() //
const categoryTreeList = ref() //
//
if(isCalcCheckAll){
calculateIsCheckAll()
}
}
//
const findCheckedIndex = (spu: Spu) => checkedSpus.value.findIndex(item => item.id === spu.id)
//
const calculateIsCheckAll = () => {
isCheckAll.value = list.value.every(spu => {
let valueElement = checkedStatus.value[spu.id];
debugger
return valueElement;
});
// &&
isIndeterminate.value = !isCheckAll.value && list.value.some(spu => checkedStatus.value[spu.id])
}
//
const categoryList = ref()
//
const categoryTreeList = ref()
/** 初始化 **/
onMounted(async () => {
await getList()