Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select中labelInValue和optionLabelProp同时使用时如何展示label值? #3401

Closed
LarryLee8 opened this issue Oct 12, 2016 · 9 comments
Closed
Assignees
Labels
🐛 Bug Ant Design Team had proved that this is a bug.

Comments

@LarryLee8
Copy link

LarryLee8 commented Oct 12, 2016

需求

我需要一个下拉框做搜索功能。当用户输入时,通过用户输入模糊查询,当用户选择具体某一项时,通过选择的值精确查询。
在精确查询时,我不希望通过字符串对比的方式搞,而是通过对应的ID来查询,这样提升效率。同时程序流程更加清晰。

发生问题的环境是:

  • antd 版本: 2.0.1
  • 操作系统及其版本: win10
  • 浏览器及其版本: chrome 最新版本

您做了什么?

in render
var props = {
                combobox: true,
                labelInValue: true,
                value: this.state.value,
                optionFilterProp: "children",
                optionLabelProp: "children" ,
                onChange: this.handleSearchChange
            };
<Select {...props}>
        <Option value=“1”>测试1</Option>
</Select>

--------------------------------------------------

in handleSearchChange
handleSearchChange: function (v) {
               this.setState({value: v})
}

您期待的结果是:

当选择一项数据之后,select现实的内容为label值.如当我选择"测试1"这一项时,希望能够展示“测试1”,而不是“1”

实际上的结果是:

实际现实的时value的值

建议

取消这种下拉框中展示value的功能,用户一般需要能够理解的描述,而不是不可理解的value。

@afc163
Copy link
Member

afc163 commented Oct 12, 2016

不要使用 combobox 。combobox 意味着可自行输入。

@LarryLee8
Copy link
Author

LarryLee8 commented Oct 12, 2016

我其实是希望用户输入的。我已经更新了一下,说了我的需求。

@LarryLee8
Copy link
Author

希望能把这个功能做完善咯。目前我使用如下代码跳个这个事情:
改造handleSearchChange函数,如下:

handleSearchChange: function (v) {
    this.setState({
        value: {key:v.label, label: key}
    });
}

注意:这个方式的前提是我记录了label和key,业务逻辑中我使用的是我记录的数据,而不是当前控件对应form表单中的值。

@afc163
Copy link
Member

afc163 commented Oct 12, 2016

  • 用户手动输入『测试1』并搜索
  • 用户在下拉菜单中点击『测试1』并搜索

这两个操作的结果是否一致?

@afc163
Copy link
Member

afc163 commented Oct 12, 2016

http://codepen.io/anon/pen/GjGkvW?editors=0011

@yiminghe 有空来看下,受控模式下有问题。

@afc163 afc163 added the 🐛 Bug Ant Design Team had proved that this is a bug. label Oct 12, 2016
@LarryLee8
Copy link
Author

  • 用户手动输入『测试1』并搜索
  • 用户在下拉菜单中点击『测试1』并搜索

这两个操作的结果是否一致?

在我的应用里面是不一致的。
当用户点击的时候:我是根据key值精确查找;
当用户输入的时候我是根据用户输入进行模糊查找;

当然在任何情况下我都可以根据label中的值查询,但这样后台就是一个模糊查询了。当用户选择【测试1】时,后台查询出了可能包括【11测试111】这样的值。

对于受控模式下的这个问题,我也很头疼的。实际上我们的应用里面有大量的我说的这种使用方式。所以我就封装了一个Combobox的组件,同时在form表单中使用。因为我封装了,如果不是受控模块,我的表单中将接受不到值。this.props.form.getFieldsValue(),的结果中对于的值总是undefined。

@benjycui
Copy link
Contributor

分别监听 onChangeonSelect 不就可以区分值得变化的来源了么?

https://ant.design/components/select/#Select-props

@yesmeck
Copy link
Member

yesmeck commented Nov 1, 2016

@yiminghe 这个我来看下吧

@lock
Copy link

lock bot commented May 3, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐛 Bug Ant Design Team had proved that this is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants