Vue开发学习笔记:el-table绑定自定义select组件

mySelect.vue

<template>
  <el-select v-model="selectValue" placeholder=" " :disabled="isDisabled"
  @change = "selectChange">
    <el-option :value="''" disabled>
        <el-row :gutter="30">
        <el-col :span="12">代码</el-col>
        <el-col :span="12">描述</el-col>
    </el-row>
    </el-option>
    <el-option :value="''">
    </el-option>
    <el-option
      v-for="item in optionData"
      :key="item.ITEM_CODE"
      :label="item.ITEM_CODE_DESC1"
      :value="item.ITEM_CODE"
    >
    <el-row  :gutter="30">
        <el-col :span="12">{{ item.ITEM_CODE }}</el-col>
        <el-col :span="12">{{ item.ITEM_CODE_DESC1 }}</el-col>
    </el-row>
    </el-option>
  </el-select>
</template>

<script>
import axios from 'axios'
import { useStore } from 'vuex'
import qs from 'qs'
import ElMessage from 'element-plus'
import { ref, watchEffect } from 'vue'
export default {
  name: 'myElSelect',
  props: [
    'SVC_NAME',
    'PARM_NAME',
    'PARM_VALUE',
    'FORM_NAME',
    'COMPONENT_NAME',
    'defauleselectValue',
    'defaultOptions',
    'disabled'
  ],
  setup (props, context) {
    /** 定义全局变量管理器 */
    const store = useStore()
    /** 组件下拉选项数据 */
    const optionData = ref([])
    /** 输入参数(下拉数据查询) */
    const inParmes = [{}]
    /** 选中值(初始化时为默认值) */
    // eslint-disable-next-line vue/no-setup-props-destructure
    const selectValue = ref(' ')
    /** 是否禁用 */
    const isDisabled = ref(false)
    watchEffect(() => {
      if (props.disabled !== undefined) {
        isDisabled.value = props.disabled
        // console.log('props.disabled:', props.disabled)
      }
      if (props.defauleselectValue !== undefined && props.defauleselectValue !== ' ') {
        // console.log('传入属性值改变触发selectValue:', props.defauleselectValue)
        selectValue.value = props.defauleselectValue
      }
    })
    if (props.defaultOptions === undefined) {
      if (props.PARM_NAME.includes(',')) {
        const PARM_NAMES = props.PARM_NAME.split(',')
        const PARM_VALUES = props.PARM_NAME.split(',')
        for (let index = 0; index < PARM_NAMES.length; index++) {
          inParmes[0][PARM_NAMES[index]] = PARM_VALUES[index]
        }
      } else {
        if (props.PARM_NAME !== ' ') {
          // eslint-disable-next-line vue/no-setup-props-destructure
          inParmes[0][props.PARM_NAME] = props.PARM_VALUE
        }
      }
      // console.log('inParmes:', inParmes)
      axios.post(store.state.url,
        qs.stringify({
          serviceName: props.SVC_NAME,
          parameters: JSON.stringify(inParmes)
        }, { indices: false })
        , store.state.urlConfig)
        .then((res) => {
          // console.log('成功回调:', res)
          if (res.data[0].returnFlag === 0) {
            optionData.value = res.data[0].Table1
          } else {
            ElMessage({
              showClose: true,
              message: res.data[0].returnMsg,
              type: 'error',
              center: true
            })
          }
        })
        .catch((err) => {
          console.log('失败回调:', err)
        })
    } else {
      // eslint-disable-next-line vue/no-setup-props-destructure
      optionData.value = props.defaultOptions
    }

    /** 选项值发生改变时触发 */
    const selectChange = (value) => {
      // console.log('选项值改变触发selectValue:', selectValue)
      store.commit('SetFormAttrInfo', { formName: props.FORM_NAME, attrName: 'el-select-Value', attrValue: selectValue.value })
    }
    return {
      selectChange,
      optionData,
      selectValue,
      isDisabled
    }
  }
}
</script>

el-table

<el-table
        :data="tableData"
        height="500"
        style="width: 100%"
        ref="elTable"
        @select="handleTableRowSelect"
        @cell-click="handleTableCellClick"
        @select-all="handleTableSelectAll"
        :cell-style="{padding: '0',height: '40px'}"
        :row-class-name="tableRowClassName"
        :cell-class-name="tableCellClassName"
        border
        stripe
        fit
        empty-text="未查询到数据" >
          <el-table-column fixed  prop="selected" type="selection"/>
          <el-table-column fixed prop="index" label="序号" type="index"
          :index="rowIndexMethod"
          width='60px'/>
          <el-table-column header-align="center" v-for="(colInfo,index) in tableColInfo"
          :fixed="colInfo.isFixed"
          :key="index"
          :prop="colInfo.colName"
          :label="colInfo.colCname"
          :width="getColWidth(colInfo.colName,colInfo.colCname)"
          >
            <!-- 自定义表项/单元格内容 -->
            <template #default="scope">
              <!-- 双击文字或点击修改图标以更改"show"属性 -->
              <!-- scope.row为元数据-->
              <!-- <span
              >
                {{scope.row[colInfo.colName]}}
              </span> -->
              <span
              v-if="scope.row.rowIndex!==lastRowIndex
              &&scope.column.no!==lastColIndex
              &&colInfo.control_type===''"
              >
                {{scope.row[colInfo.colName]}}
              </span>
              <!-- 当控件类型为Switch控件时绑定 -->
              <el-switch
                v-if="colInfo.control_type==='el-switch'"
                v-model="scope.row[colInfo.colName]"
                :before-change="switchBeforeChange"
                inline-prompt
                active-value="True"
                inactive-value="False"
                active-text="是"
                inactive-text="否"
                active-color="#13ce66"
              />
              <!-- 当控件类型为select控件时绑定 -->
             <my-el-select
              v-if="colInfo.control_type==='el-select'"
              :FORM_NAME="'TestView2'"
              :COMPONENT_NAME="'myElSelect1'"
              :defauleselectValue="scope.row[colInfo.colName]"
              :defaultOptions="defaultOptions"
              :disabled = "selectDisable"
              v-model="scope.row[colInfo.colName]"
              ></my-el-select>
              <el-input
                v-if="scope.row.rowIndex===lastRowIndex
                &&scope.column.no===lastColIndex
                &&colInfo.control_type===''"
                v-model="scope.row[colInfo.colName]"
                :ref = "setItemRef"
                @blur="handleInputLosFocus(scope.row,scope.column)"
              />
            </template>
          </el-table-column>
        </el-table>

注: 遇见的问题

1.el-table获取选中行的数据时无法获取到自定义select组建的选中值

解决方案: 调用自定义组件时绑定v-model属性

不使用v-model 时获取值

使用v-model时获取数据

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个流行的JavaScript框架,它的数据绑定组件化特性使得开发动态Web应用变得更加容易。其中,强大的UI组件库(如Element,iView等)能够极大地提高Web应用开发效率。而el-table是一种用于展示表格数据的组件,它拥有可排序、过滤、分页等多种功能。本文主要介绍如何使用Vue.js实现el-table表头自定义。 在Vue.js中使用el-table组件时,表头(thead)用于显示列名和控制排序、过滤等操作。默认情况下,el-table组件根据数据源中的列名自动生成表头。若需自定义表头,可通过以下方式实现: 1. 使用el-table-column组件el-table中使用el-table-column组件可以实现自定义表头。具体操作如下: ```html <el-table :data="tableData"> <el-table-column prop="date" label="日期"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> ``` 上述代码中,我们为el-table添加了三个el-table-column组件,分别对应表格中的三列数据。同时,我们在每个el-table-column组件上指定了prop和label属性,其中prop属性指定了对应的数据源中的字段名,label属性指定了表头标题。 2. 使用Scoped Slots 如果需要实现更加复杂的表头,可以使用Scoped Slots进行自定义。具体操作如下: ```html <el-table :data="tableData"> <template slot="header"> <el-row> <el-col :span="8">日期</el-col> <el-col :span="8">姓名</el-col> <el-col :span="8">地址</el-col> </el-row> </template> <el-table-column prop="date"></el-table-column> <el-table-column prop="name"></el-table-column> <el-table-column prop="address"></el-table-column> </el-table> ``` 上述代码中,我们使用了el-table的header slot,它可以让我们自定义表头,即在表头中添加任意HTML代码。在header slot中我们使用了el-row和el-col组件创建了一个表头行,然后通过span属性设置每列所占的宽度,最终实现了自定义表头。 总结 以上就是Vue.js实现el-table表头自定义的两种方式。使用el-table-column组件可以快速地实现简单的自定义表头,而使用Scoped Slots可以实现更加复杂的表头需求。选择合适的方式,可以大大提高开发效率和表格的可读性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值