🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <template> <el-dialog class="ba-operate-dialog care-dialog" :close-on-click-modal="true" width="98%" @open="openCareFun" v-model="state.dialog" style="overflow: hidden; overflow-y: scroll;" > <template #header> <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'"> 选择货品 </div> </template> <view class="goods-box" > <el-row :gutter="20"> <el-col :span="6" > <el-card class="box-card" > <div class="tree-box"> <el-tree :data="cateTree" :props="defaultProps" :highlight-current="true" @node-click="handleNodeClick" default-expand-all="true" node-key="id" :default-checked-keys="[0]" :current-node-key="0" /> </div> </el-card> </el-col> <el-col :span="18" > <el-card class="box-card" > <view class="search-box"> <el-form :model="form" @keyup.enter="onSearchSubmit" > <el-row :gutter="20" > <el-col :span="12" :offset="4"> <el-form-item label="名称"> <!-- 货号、名称、简码 --> <el-input v-model="form.kw" placeholder="货号、名称、简码"/> </el-form-item> </el-col> <!-- <el-col :span="8"> 16 28 <el-form-item label=""> <el-input v-model="form.sn" placeholder="按条形码查询" /> </el-form-item> </el-col> --> <el-col :span="4"> <el-form-item> <el-button type="primary" @click="onSearchSubmit">查询</el-button> </el-form-item> </el-col> </el-row> </el-form> </view> <!-- 表格 --> <div class="goods-table"> <el-table :data="tableData" v-loading="loading" height="300" style="width: 100%" stripe ref="tableDataRef" @select="handlerPTempSelect" @selection-change="handlePTempSelectionChange" @row-click="rowClickHandlePtemp" > <el-table-column type="selection" width="55" /> <el-table-column prop="goods_no" label="商品编号" width="110" fixed="left"/> <el-table-column label="图片" width="80"> <template #default="scope"> <el-image v-if="scope.row.template.template_images[0]" style="width: 50px; height: 50px;border-radius: 4px;" :src="scope.row.template.template_images[0]" :fit="scope.row.template.template_name" /> </template> </el-table-column> <el-table-column prop="template.template_name" label="商品名称" width="100" /> <el-table-column prop="color_name" label="颜色" width="100" /> <el-table-column prop="template.template_no" label="商品简码" width="100"/> <el-table-column prop="brand_name" label="品牌" width="100"/> <el-table-column prop="unit_price" label="吊牌价" width="100"/> </el-table> <div class="goods-pagination-block"> <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 100]" :disabled="disabled" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </div> </div> <view> <el-row :gutter="20" style="margin-top:80px;"> <el-col :span="6" :offset="6" align="center"> <el-button type="primary" @click="handleConfirm">确定</el-button> </el-col> <el-col :span="6" align="center"> <el-button type="danger" @click="handleCancel" >返回</el-button> </el-col> </el-row> </view> </el-card> </el-col> </el-row> </view> </el-dialog> </template> <script setup lang="ts"> import { reactive, ref, inject,defineExpose,watch,defineEmits,onMounted } from 'vue' import { ElNotification } from 'element-plus' import { useI18n } from 'vue-i18n' import {getGoodsList} from "/@/api/backend/aGoods"; const emit = defineEmits([ "doSth" ]); let props = defineProps({ cateTree:{ type:Object, }, kw:{ type:String, } }) onMounted(() => { }); let state = reactive({ dialog:false, }) // 打开 const openCareFun = ()=>{ tableDataRef.value.clearSelection()// 清空选中 form.kw = props.kw // 默认查询 searchGoods() } const openDialogFun = ()=>{ state.dialog = true } // 选择数据 const tempData = reactive({ selectCheckList:[], // 选择的货品 goodsCateId:0, // 选中分类ID }) interface Tree { label: string children?: Tree[] } const defaultProps = { children: 'children', label: 'label', } const handleNodeClick = (data: Tree) => { console.log(data) tempData.goodsCateId = data.id searchGoods() } const form = reactive({ kw: props.kw || '', sn:"", // 按条形码查询 }) let tableData = ref([]) // 表格数据 const currentPage = ref(1) const pageSize = ref(10) const total = ref(0) const disabled = ref(true) const loading = ref(false) watch(() => tableData, (newValue, oldValue) => { console.log('watch-item',{ newValue:newValue, oldValue:oldValue, }) disabled.value = tableData.value.length>0?false:true; }, {deep:true}) const onSearchSubmit = () => { searchGoods() } const searchGoods = () => { loading.value = true let search = [] if(tempData.goodsCateId >0){ search = [ { field:'template.category_id', operator:'FIND_IN_SET', val:tempData.goodsCateId, } ] } let param = { search, quick_search:form.kw, limit:pageSize.value, page:currentPage.value, } getGoodsList(param).then(res => { console.log(res.data) tableData.value = res.data.list total.value = res.data.total loading.value = false }); } // 页码 const handleSizeChange = (val: number) => { console.log(`${val} items per page`) pageSize.value = val searchGoods() } const handleCurrentChange = (val: number) => { console.log(`current page: ${val}`) currentPage.value = val searchGoods() } const tableDataRef = ref() // 表格某一行的单击事件 function rowClickHandlePtemp(row) { const selectData = tempData.selectCheckList tableDataRef.value.clearSelection() if (selectData.length >= 1) { const [item] = selectData; const shouldSelect = item !== row; tableDataRef.value.toggleRowSelection(row, shouldSelect); } else { tableDataRef.value.toggleRowSelection(row, true); } } // 当用户手动勾选数据行的 Checkbox 时触发的事件 function handlerPTempSelect(selection, row) { tableDataRef.value.clearSelection() // 清除 所有勾选项 if(selection.length == 0) return tableDataRef.value.toggleRowSelection(row,true) } // 当选择项发生变化时会触发该事件 function handlePTempSelectionChange(val) { if(val.length > 1){ return } tempData.selectCheckList = val } // 确定 const handleConfirm = ()=>{ // 判断商品是否选择 if(tempData.selectCheckList.length == 0){ ElNotification({ title: '提示', message: '请选择一个货品!', type: 'warning', }) return false } if(tempData.selectCheckList.length > 1){ ElNotification({ title: '提示', message: '只能选择一个货品!', type: 'warning', }) return false } state.dialog = false emit('doSth',{ goods:tempData.selectCheckList, fun:'doSth' }); } const handleCancel = ()=>{ state.dialog = false } defineExpose({openDialogFun}) </script> <style scoped lang="scss"> .goods-table ::v-deep .el-table th.el-table__cell:nth-child(1) .cell { visibility: hidden; } .ftw{ font-weight: 900; } .o-wrap{ overflow-wrap: break-word; } .tree-box{ overflow: auto; height: 50vh; } .goods-pagination-block{ margin-top: 18px; } </style> ```