【Ant Design】Modal 中Select组件下拉选项随页面滚动与Select框分离异常

背景:

使用Ant Design,在Modal组件中使用Select组件,当点击Select组件出现下拉选项后滚动原始页面,出现下拉选项和Select框分离的异常。

异常图片:

异常代码:

<Select>
    // some <Option></Option>
</Select>

问题点:

Select组件有个API  getPopupContainer,Ant Design官方文档给出的用途解释为:“菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位”。同时官网也强调一个特殊情况需要注意:“如果发现下拉菜单跟随页面滚动,或者需要在其他弹层中触发 Select,请尝试使用 getPopupContainer={triggerNode => triggerNode.parentNode} 将下拉弹层渲染节点固定在触发器的父元素中”。

解决方案:

在Select组件中添加“getPopupContainer={triggerNode => triggerNode.parentNode}”属性即可。

正确代码:

<Select getPopupContainer={triggerNode => triggerNode.parentNode}>
    // some <Option></Option>
</Select>

修改后效果:

您可以在 Ant Design Vue 的弹窗中使用 Table 组件Select 组件来实现行编辑中的下拉选择。首先,您需要在 Table 组件中设置 editable 属性为 true,这样就可以开启行编辑功能。然后,您可以在需要进行下拉选择编辑的列中,使用 scoped slot 的方式自定义单元格内容,并在该单元格中使用 Select 组件来实现下拉选择。具体实现方法可以参考以下代码示例: ```html <template> <div> <a-button type="primary" @click="showModal">打开弹窗</a-button> <a-modal v-model="visible" title="编辑表格" :width="800"> <a-form :form="form"> <a-table :columns="columns" :data-source="data" :row-key="record => record.id" :editable="true"> <template slot-scope="text, record, index"> <a-form-item> <a-select v-model="record.selectValue" style="width: 100%"> <a-select-option v-for="option in options" :key="option.value" :value="option.value">{{ option.label }}</a-select-option> </a-select> </a-form-item> </template> </a-table> </a-form> </a-modal> </div> </template> <script> import { Modal, Form, Button, Table, Select } from 'ant-design-vue' export default { components: { 'a-modal': Modal, 'a-form': Form, 'a-button': Button, 'a-table': Table, 'a-form-item': Form.Item, 'a-select': Select, 'a-select-option': Select.Option, }, data() { return { visible: false, form: this.$form.createForm(this), data: [ { id: 1, name: '张三', age: 18, selectValue: 'option1' }, { id: 2, name: '李四', age: 22, selectValue: 'option2' }, { id: 3, name: '王五', age: 25, selectValue: 'option3' }, ], options: [ { label: '选项1', value: 'option1' }, { label: '选项2', value: 'option2' }, { label: '选项3', value: 'option3' }, ], columns: [ { title: 'ID', key: 'id', dataIndex: 'id', editable: false }, { title: '姓名', key: 'name', dataIndex: 'name', editable: true }, { title: '年龄', key: 'age', dataIndex: 'age', editable: true }, { title: '下拉选择', key: 'selectValue', dataIndex: 'selectValue', editable: true }, ], } }, methods: { showModal() { this.visible = true }, }, } </script> ``` 在上面的代码示例中,我们使用了 Ant Design Vue 的 Modal、Form、Button、Table 和 Select 组件来实现弹窗、表单、按钮、表格和下拉选择。在 Table 组件中,我们使用了 scoped slot 来自定义单元格内容,并在该单元格中使用 Select 组件来实现下拉选择。同时,我们还需要在该列的配置项中设置 editable 属性为 true,以开启该列的编辑功能。最终,我们将 Table 组件放置在 Modal 组件中,实现了在弹窗中编辑带有下拉选择的表格的功能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值