vue2+TS,el-table表格单选的写法

1.打开表格

javascript 复制代码
//父组件引入 
 <customerChoose ref="customerChooseRef" @onSure="setOrderInfoFn"></customerChoose>

//子传父,接收值,操作
 private async setOrderInfoFn(data) {
    this.form.customerId = data.id
    this.form.customerName = data.customerName
  }

打开dialog表格:

javascript 复制代码
private async chooseMultiFn(type) {
    await this.$nextTick()
    ;(this.$refs.customerChooseRef as any).opendialog(this.titles,this.form.customerId)

  }

2.表格的template写法

javascript 复制代码
<template>
  <el-dialog :show-close="false" title="选择客户" :visible.sync="customerDialogVisible" top="0px" width="1000px" center :close-on-click-modal="false" destroy-on-close append-to-body>
    <div style="width: 100%; height: 500px">
      <el-form :inline="true" :model="customerQueryCondition">
        <el-form-item label="客户编码:">
          <el-input v-model="customerQueryCondition.customerCode" clearable placeholder="请输入客户编码"></el-input>
        </el-form-item>
        <el-form-item label="客户名称:">
          <el-input v-model="customerQueryCondition.customerName" clearable placeholder="请输入客户名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="seachBtn" @click="searchOrReset(false)">
            <i class="el-icon-search"></i>
            {{ $t('i18n.search') }}
          </el-button>
          <el-button class="seachBtn" @click="searchOrReset(true)">
            <i class="el-icon-refresh"></i>
            {{ $t('i18n.reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table size="small" header-row-class-name="monitorTable" stripe :data="tableData" height="400px" highlight-current-row border style="width: 100%" ref="myELTable" @row-click="chooseRadio">
        <el-table-column label="" width="50" align="center">
          <template scope="scope">
            <el-radio :label="scope.$index" name="radiobutton" v-model="radio"></el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="customerCode" label="客户编码" align="center"></el-table-column>
        <el-table-column prop="customerName" label="客户名称" align="center"></el-table-column>
        <el-table-column prop="address" label="客户地址" align="center"></el-table-column>
      </el-table>
    </div>
    <el-pagination :background="true" :current-page.sync="pageInfo.current" :page-size="pageInfo.size" layout="total,sizes, prev, pager, next, jumper, slot" :total="pageInfo.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top:20px;">
      <el-button style="margin-left: 5px" type="primary" @click="handleCurrentChange(pageInfo.current)">跳转</el-button>
    </el-pagination>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onCancelChoose">{{ $t('i18n.cancelBtn') }}</el-button>
      <el-button type="primary" @click="onSureChoose">{{ $t('i18n.sureBtn') }}</el-button>
    </div>
  </el-dialog>
</template>

3 .打开后操作:

打开后的初始化操作:

javascript 复制代码
private titlesOption: string = ''
  public opendialog(titles,customerId) {
    // 用户id,用于一开始勾选回显,没有就不勾
    this.customerId = customerId
    // 标题
    this.titlesOption = titles
    // 客户查询条件 一开始设为空
    this.customerQueryCondition = {}
    this.queryCache = {}
    this.openCustomerChooseDialog()
  }
  public async openCustomerChooseDialog() {
    this.radio = ''
    //获取表格数据
    let res = await getCustomerInfo(this.pageInfo.current, this.pageInfo.size, this.queryCache.customerCode || '', this.queryCache.customerName || '', '')
    this.tableData = res.data
    this.pageInfo.total = res.totalDataCount
    //打开弹窗
    this.customerDialogVisible = true
    if (this.customerId) {
      this.$nextTick(() => {
        // 初始化回显勾选
        this.radio = this.tableData.findIndex((item) => item.id == this.customerId)
      })
    }
  }

表格单选:

javascript 复制代码
//单选下标
private radio: any = ''

//存储选中行的数据
 private currentSelectedRow: any = {}

//@row-click="chooseRadio" 选中某一行
  private async chooseRadio(e) {
    this.tableData.filter((item, index) => {
      if (e.id == item.id) this.radio = index
    })
    this.currentSelectedRow = e
  }

4.点击确定后的操作

javascript 复制代码
//用于子传父  
@Emit('onSure')
  private onSure(row: any) {}

// 确定
  private async onSureChoose(e) {
    //把勾选的值传到父页面操作
    this.onSure(this.currentSelectedRow)
    this.customerDialogVisible = false
  }
相关推荐
无限大.1 小时前
前端知识速记:节流与防抖
前端
十八朵郁金香1 小时前
【VUE案例练习】前端vue2+element-ui,后端nodo+express实现‘‘文件上传/删除‘‘功能
前端·javascript·vue.js
学问小小谢1 小时前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
LCG元2 小时前
Vue.js组件开发-实现全屏图片文字缩放切换特效
前端·javascript·vue.js
还是鼠鼠3 小时前
图书管理系统 Axios 源码__新增图书
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
customer083 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
还是鼠鼠6 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
轻口味6 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
m0_zj7 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css
还是鼠鼠8 小时前
图书管理系统 Axios 源码__编辑图书
前端·javascript·vscode·ajax·前端框架