当前位置:   article > 正文

Vue开发学习笔记:el-table绑定自定义select组件_vue/no-setup-props-destructure

vue/no-setup-props-destructure

mySelect.vue

  1. <template>
  2. <el-select v-model="selectValue" placeholder=" " :disabled="isDisabled"
  3. @change = "selectChange">
  4. <el-option :value="''" disabled>
  5. <el-row :gutter="30">
  6. <el-col :span="12">代码</el-col>
  7. <el-col :span="12">描述</el-col>
  8. </el-row>
  9. </el-option>
  10. <el-option :value="''">
  11. </el-option>
  12. <el-option
  13. v-for="item in optionData"
  14. :key="item.ITEM_CODE"
  15. :label="item.ITEM_CODE_DESC1"
  16. :value="item.ITEM_CODE"
  17. >
  18. <el-row :gutter="30">
  19. <el-col :span="12">{{ item.ITEM_CODE }}</el-col>
  20. <el-col :span="12">{{ item.ITEM_CODE_DESC1 }}</el-col>
  21. </el-row>
  22. </el-option>
  23. </el-select>
  24. </template>
  25. <script>
  26. import axios from 'axios'
  27. import { useStore } from 'vuex'
  28. import qs from 'qs'
  29. import ElMessage from 'element-plus'
  30. import { ref, watchEffect } from 'vue'
  31. export default {
  32. name: 'myElSelect',
  33. props: [
  34. 'SVC_NAME',
  35. 'PARM_NAME',
  36. 'PARM_VALUE',
  37. 'FORM_NAME',
  38. 'COMPONENT_NAME',
  39. 'defauleselectValue',
  40. 'defaultOptions',
  41. 'disabled'
  42. ],
  43. setup (props, context) {
  44. /** 定义全局变量管理器 */
  45. const store = useStore()
  46. /** 组件下拉选项数据 */
  47. const optionData = ref([])
  48. /** 输入参数(下拉数据查询) */
  49. const inParmes = [{}]
  50. /** 选中值(初始化时为默认值) */
  51. // eslint-disable-next-line vue/no-setup-props-destructure
  52. const selectValue = ref(' ')
  53. /** 是否禁用 */
  54. const isDisabled = ref(false)
  55. watchEffect(() => {
  56. if (props.disabled !== undefined) {
  57. isDisabled.value = props.disabled
  58. // console.log('props.disabled:', props.disabled)
  59. }
  60. if (props.defauleselectValue !== undefined && props.defauleselectValue !== ' ') {
  61. // console.log('传入属性值改变触发selectValue:', props.defauleselectValue)
  62. selectValue.value = props.defauleselectValue
  63. }
  64. })
  65. if (props.defaultOptions === undefined) {
  66. if (props.PARM_NAME.includes(',')) {
  67. const PARM_NAMES = props.PARM_NAME.split(',')
  68. const PARM_VALUES = props.PARM_NAME.split(',')
  69. for (let index = 0; index < PARM_NAMES.length; index++) {
  70. inParmes[0][PARM_NAMES[index]] = PARM_VALUES[index]
  71. }
  72. } else {
  73. if (props.PARM_NAME !== ' ') {
  74. // eslint-disable-next-line vue/no-setup-props-destructure
  75. inParmes[0][props.PARM_NAME] = props.PARM_VALUE
  76. }
  77. }
  78. // console.log('inParmes:', inParmes)
  79. axios.post(store.state.url,
  80. qs.stringify({
  81. serviceName: props.SVC_NAME,
  82. parameters: JSON.stringify(inParmes)
  83. }, { indices: false })
  84. , store.state.urlConfig)
  85. .then((res) => {
  86. // console.log('成功回调:', res)
  87. if (res.data[0].returnFlag === 0) {
  88. optionData.value = res.data[0].Table1
  89. } else {
  90. ElMessage({
  91. showClose: true,
  92. message: res.data[0].returnMsg,
  93. type: 'error',
  94. center: true
  95. })
  96. }
  97. })
  98. .catch((err) => {
  99. console.log('失败回调:', err)
  100. })
  101. } else {
  102. // eslint-disable-next-line vue/no-setup-props-destructure
  103. optionData.value = props.defaultOptions
  104. }
  105. /** 选项值发生改变时触发 */
  106. const selectChange = (value) => {
  107. // console.log('选项值改变触发selectValue:', selectValue)
  108. store.commit('SetFormAttrInfo', { formName: props.FORM_NAME, attrName: 'el-select-Value', attrValue: selectValue.value })
  109. }
  110. return {
  111. selectChange,
  112. optionData,
  113. selectValue,
  114. isDisabled
  115. }
  116. }
  117. }
  118. </script>

el-table

  1. <el-table
  2. :data="tableData"
  3. height="500"
  4. style="width: 100%"
  5. ref="elTable"
  6. @select="handleTableRowSelect"
  7. @cell-click="handleTableCellClick"
  8. @select-all="handleTableSelectAll"
  9. :cell-style="{padding: '0',height: '40px'}"
  10. :row-class-name="tableRowClassName"
  11. :cell-class-name="tableCellClassName"
  12. border
  13. stripe
  14. fit
  15. empty-text="未查询到数据" >
  16. <el-table-column fixed prop="selected" type="selection"/>
  17. <el-table-column fixed prop="index" label="序号" type="index"
  18. :index="rowIndexMethod"
  19. width='60px'/>
  20. <el-table-column header-align="center" v-for="(colInfo,index) in tableColInfo"
  21. :fixed="colInfo.isFixed"
  22. :key="index"
  23. :prop="colInfo.colName"
  24. :label="colInfo.colCname"
  25. :width="getColWidth(colInfo.colName,colInfo.colCname)"
  26. >
  27. <!-- 自定义表项/单元格内容 -->
  28. <template #default="scope">
  29. <!-- 双击文字或点击修改图标以更改"show"属性 -->
  30. <!-- scope.row为元数据-->
  31. <!-- <span
  32. >
  33. {{scope.row[colInfo.colName]}}
  34. </span> -->
  35. <span
  36. v-if="scope.row.rowIndex!==lastRowIndex
  37. &&scope.column.no!==lastColIndex
  38. &&colInfo.control_type===''"
  39. >
  40. {{scope.row[colInfo.colName]}}
  41. </span>
  42. <!-- 当控件类型为Switch控件时绑定 -->
  43. <el-switch
  44. v-if="colInfo.control_type==='el-switch'"
  45. v-model="scope.row[colInfo.colName]"
  46. :before-change="switchBeforeChange"
  47. inline-prompt
  48. active-value="True"
  49. inactive-value="False"
  50. active-text="是"
  51. inactive-text="否"
  52. active-color="#13ce66"
  53. />
  54. <!-- 当控件类型为select控件时绑定 -->
  55. <my-el-select
  56. v-if="colInfo.control_type==='el-select'"
  57. :FORM_NAME="'TestView2'"
  58. :COMPONENT_NAME="'myElSelect1'"
  59. :defauleselectValue="scope.row[colInfo.colName]"
  60. :defaultOptions="defaultOptions"
  61. :disabled = "selectDisable"
  62. v-model="scope.row[colInfo.colName]"
  63. ></my-el-select>
  64. <el-input
  65. v-if="scope.row.rowIndex===lastRowIndex
  66. &&scope.column.no===lastColIndex
  67. &&colInfo.control_type===''"
  68. v-model="scope.row[colInfo.colName]"
  69. :ref = "setItemRef"
  70. @blur="handleInputLosFocus(scope.row,scope.column)"
  71. />
  72. </template>
  73. </el-table-column>
  74. </el-table>

注: 遇见的问题

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

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

不使用v-model 时获取值

使用v-model时获取数据

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号