• vxe-table编辑单元格动态插槽slot的使用


    业务场景:表格中只有特定某一行的的单元格可以编辑,列很多,为每个列写个插槽要写很多重复代码,所以这里使用动态插槽,简化代码量。显示编辑图标,点击编辑图标隐藏。失去焦点保存调后台接口。

    方法一、

    1、后端返回的数据里可以编辑的行数据添加属性 edit: true;不可编辑的行数据里添加属性 edit: false;

    2、把列数组里的插槽和field提取出来为循环使用做准备,如果直接使用导入进来的columns无法显示,所以需要处理后使用。

    方法二、可以为每个列名使用相同的插槽名,使代码更简洁。

    列名文件示例columns.js

    1. export const columns2 = [{
    2. title: '温差',
    3. field: 'pumpCon',
    4. align: 'center',
    5. width: "160",
    6. editRender: {},
    7. slots: {
    8. edit: 'input_edit',
    9. default: 'input_default',
    10. },
    11. },
    12. {
    13. title: '昨日水',
    14. field: 'yesterdayWater',
    15. align: 'right',
    16. width: "110",
    17. editRender: {},
    18. slots: {
    19. edit: 'input_edit',
    20. default: 'input_default',
    21. },
    22. },
    23. {
    24. title: '累计',
    25. field: 'yesterdayWaterSum',
    26. align: 'right',
    27. width: "110",
    28. },
    29. ]
    1. <template #input_edit="{ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex, _columnIndex }">
    2. <div
    3. v-if="[1].includes(rowIndex) && ['pumpCon'].includes(column.field)"
    4. style="display: flex; align-items: center;justify-content: flex-end;"
    5. >
    6. <vxe-input
    7. v-model="row[column.field]"
    8. type="float"
    9. :min="0"
    10. :max="99999999"
    11. :digits="getDigits(column.title)"
    12. @prev-number="onClickBtn(row, rowIndex, column.field, time)"
    13. @next-number="onClickBtn(row, rowIndex, column.field, time)"
    14. @blur="onBlur(row, rowIndex, column.field, time)"
    15. >vxe-input>
    16. div>
    17. <div v-else>
    18. {{ row[column.field] }}
    19. div>
    20. template>
    21. // Js代码 这里失去焦点就调接口,所以当使用输入框的上下箭头时,加入防抖使用
    22. // 防抖
    23. onClickBtn: debounce((row, rowIndex, field, time)=>{
    24. methods.onBlur(row, rowIndex, field, time);
    25. }, 300),
    26. onBlur(row, rowIndex, field, time) {
    27. let value = row?.[field] || '';
    28. let key = `${rowIndex}-${field}`;
    29. ...
    30. }

    方法一、列名文件示例columns.js

    1. export const columns1 = [
    2. {
    3. title: '名称',
    4. field: "heatSourceName",
    5. align: 'left',
    6. width: "160",
    7. slots: { header: 'header_heatSourceName', default: '_heatSourceName' },
    8. },
    9. {
    10. title: "日流量(t/h)",
    11. field: "supTemp",
    12. width: "140",
    13. align: 'right',
    14. sortable: false,
    15. editRender: { autofocus: '.vxe-input--inner' },
    16. slots: { default: '_supTemp', edit: 'edit_supTemp' },
    17. },
    18. {
    19. title: "日热量(GJ/h)",
    20. field: "supPres",
    21. width: "140",
    22. align: 'right',
    23. sortable: false,
    24. editRender: { autofocus: '.vxe-input--inner' },
    25. slots: { default: '_supPres', edit: 'edit_supPres' },
    26. },
    27. {
    28. title: "日压力(Mpa)",
    29. field: "instFlowSup",
    30. width: "160",
    31. align: 'right',
    32. sortable: false,
    33. editRender: { autofocus: '.vxe-input--inner' },
    34. slots: { default: '_instFlowSup', edit: 'edit_instFlowSup' },
    35. }, ...]

    循环插槽HTML写法

    1. <vxe-grid ref="xGrid1" v-bind="gridOptions1" :span-method="spanMethods">
    2. <template #header_heatSourceName>
    3. <div class="first-col">
    4. <div class="first-col-top">指标div>
    5. <div class="first-col-bottom">热源div>
    6. div>
    7. template>
    8. <template v-for="item in defaultSlots1" :key="item.field" v-slot:[item.slot]="{ row }">
    9. <div style="display: flex; align-items: center;`justify-content: ${row[item.field] === 'heatSourceName' ? flex-start : flex-end}`"
    10. v-if="row.edit && timeInfo === timeInfo1">
    11. <i class="vxe-cell--edit-icon vxe-icon-edit" style="margin-right: 5px;">i>{{ row[item.field] }}
    12. div>
    13. <div v-else>
    14. {{ row[item.field] }}
    15. div>
    16. template>
    17. <template v-for="item in editSlots1" :key="item.field" v-slot:[item.slot]="{ row }">
    18. <div v-if="row.edit && timeInfo === timeInfo1">
    19. <vxe-input v-model="row[item.field]" type="number" :min="0" :max="99999999">vxe-input>
    20. div>
    21. <div v-else>
    22. {{ row[item.field] }}
    23. div>
    24. template>
    25. vxe-grid>

     Js写法

  • 相关阅读:
    Wireshark数据包分析——Teardrop泪滴攻击
    最新NVIDIA英伟达GPU显卡算力表
    “智慧”北京,人工智能引领“新风尚”
    828. 统计子串中的唯一字符
    Knowledge Editing for Large Language Models: A Survey
    (未整理完)十月每日一题打卡
    周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录
    MySQL碎片整理小节--实例演示
    探索arkui(2)--- 布局(列表)--- 2(支持分组/实现响应滚动位置)
    ps2021神经ai滤镜无法使用,ps2021神经滤镜出现错误
  • 原文地址:https://blog.csdn.net/m0_62323730/article/details/136388869