Vue3+Element+TypeScript可编辑表格

10 篇文章 0 订阅

1、html代码

                <el-button type="primary" @click="add" size="small">添加</el-button>
                <el-table 
                    :data="TableData" 
                    
                    style="width: 100%" 
                    ref="TableRef"
                    @cell-click="getCell"
                    border
                    show-summary
                    size="small"
                >
                    <!-- <el-table-column type="selection" width="30" /> -->
                    <el-table-column label="序号" type="index"  width="50">
                        
                        <template #default="scope">
                            <div>{{scope.row.index+1}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="分类" min-width="150">
                        <template #default="scope">
                            <el-select v-model="scope.row.baseCategory" filterable size="small" placeholder="请选择" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex" ref="tabSelect" @change="fenleiChange">
                            <el-option
                                v-for="item in chanpinfenleiOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                            </el-select>
                            <div v-else>{{ scope.row.baseCategory }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="型号" min-width="150">
                        <template #default="scope">
                            <el-select v-model="scope.row.modelNumber" filterable size="small" placeholder="请选择" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex" ref="tabSelect" @change="leixingChange">
                            <el-option
                                v-for="item in chanpinleixingData"
                                :key="item.modelNumber"
                                :label="item.modelNumber"
                                :value="item.modelNumber"
                            />
                            </el-select>
                            <div v-else>{{ scope.row.modelNumber }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="规格" min-width="150">
                        <template #default="scope">
                            <el-select v-model="scope.row.specification" filterable  size="small" placeholder="请选择" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex" ref="tabSelect" @change="guigeChange">
                            <el-option
                                v-for="item in guigeData"
                                :key="item.specification"
                                :label="item.specification"
                                :value="item.specification"
                            />
                            </el-select>
                            <div v-else>{{ scope.row.specification }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="厚度" min-width="150">
                        <template #default="scope">
                            <el-select v-model="scope.row.thickness" filterable size="small" placeholder="请选择" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex" ref="tabSelect" @change="houduChange">
                            <el-option
                                v-for="item in houduOptions"
                                :key="item.thickness"
                                :label="item.thickness"
                                :value="item.thickness"
                            />
                            </el-select>
                            <div v-else>{{ scope.row.thickness }}</div>
                        </template>
                    </el-table-column>

                    <el-table-column label="单位" min-width="70">
                        <template #default="scope">
                            <div>{{ scope.row.unit }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="数量" prop="quantity" sortable min-width="100">
                        <template #default="scope" >
                            <el-input v-model="scope.row.quantity" ref="tabInput" size="small" type="number" placeholder="请输入" @blur="inputBlur" @input="inputOnNum" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex"/>
                            <div  v-else>{{scope.row.quantity}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" min-width="100">
                        <template #default="scope" >
                            <el-input v-model="scope.row.description" ref="tabInput" size="small" placeholder="请输入" @blur="inputBlur" v-if="scope.row.index == tabRowIndex && scope.column.no == tabColumnIndex"/>
                            <div  v-else>{{scope.row.description}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" align="right" width="80">
                        <template #default="scope">
                            <!-- <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
                            <el-popconfirm title="确定要删除吗?" @confirm="tabDelete(scope.$index, scope.row)" confirm-button-text="是"
                                cancel-button-text="否">
                                <template #reference>
                                    <el-button size="small" type="danger">删除
                                    </el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>

2、js代码

//可编辑表格逻辑代码》》》》》》》》》》》》》
//定义选中行键值对以及表格内input和select标签声明变量,方便调用
const tabRowIndex = ref();
const tabColumnIndex = ref();
const tabInput=ref()
const tabSelect=ref()


//监听单击单元格
const getCell = (row:any,column:any,cell:any,event:any) =>{
  tabRowIndex.value=row.index;
  tabColumnIndex.value=column.no
  //单机单元格的时候聚焦输入框
  nextTick(()=>{
    //input框监听
    if( column.label == '数量' || column.label == '单价' || column.label == '折扣' || column.label == '备注' ){
        tabInput.value.focus()
        tabInput.value.select()
    }else if(column.label == '分类' ){//下拉框监听
        
        tabSelect.value.focus()//聚焦
    }
  })
}


//点击单元时根据此一行选择的数据,将与关联的本行数据更新
const getOrderJingBan = (selectText:any,sortType:any) =>{
    const tDFenLei = TableData.value[tabRowIndex.value].baseCategory
    const tD1 = TableData.value[tabRowIndex.value].modelNumber
    const tD2 = TableData.value[tabRowIndex.value].specification
    const tD3 = TableData.value[tabRowIndex.value].thickness


}


//表格输入框失焦监听
const inputBlur = () =>{
  tabRowIndex.value=null;
  tabColumnIndex.value=""
}


//表格输入框值实时监听
const inputOnNum = (e:any) =>{


}


//下拉框数据
const chanpinleixingData=ref<any>([])
const guigeData=ref<any>([])
const houduOptions=ref<any>([])
const wujinOptions=ref<any>([])


//下拉框改变时,将选中的键值对清空
const fenleiChange = async () =>{
    tabRowIndex.value=null;
    tabColumnIndex.value=""
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3是Vue.js的最新版本,它带来了许多令人兴奋的新特性和改进。Element Plus是一套基于Vue 3的UI组件库,它是对Element UI的升级和重构。TypeScript是一种静态类型检查的编程语言,可以帮助我们在开发过程中更加安全和高效地编写代码。Vite是一个新型的前端构建工具,它可以提供快速的开发体验和更好的性能。 如果你想在Vue 3项目中使用Element Plus和TypeScript,可以按照以下步骤进行: 1. 创建一个新的Vue 3项目。你可以使用Vue CLI来创建项目: ``` $ vue create my-project ``` 2. 在项目中安装Element Plus。你可以使用npm或yarn来安装: ``` $ npm install element-plus 或 $ yarn add element-plus ``` 3. 在项目的入口文件(通常是main.ts)中引入Element Plus的样式和组件: ```typescript import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(App); app.use(ElementPlus); app.mount('#app'); ``` 4. 开始在项目中使用Element Plus的组件。你可以在Vue组件中按需引入所需的组件,例如: ```vue <template> <el-button type="primary">Button</el-button> </template> <script> import { defineComponent } from 'vue'; import { ElButton } from 'element-plus'; export default defineComponent({ components: { ElButton } }); </script> ``` 5. 如果你想在项目中使用TypeScript,可以将项目的脚手架配置为支持TypeScript。在Vue CLI创建项目时,可以选择TypeScript作为预设选项。如果你已经创建了项目,可以手动添加TypeScript支持,具体步骤可以参考Vue官方文档。 希望以上步骤对你有帮助!如果还有其他问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值