antd之Input的onChange坑

    首先说一下需求,三个input框的值输入数字,想让其自动加减,然后复制给第四个输入框

 

很简单的一个例子,原本想用onChange结合getFieldValue来使用呢    却发现此onChange非彼onChange的

 

html代码

 1 <Row type={'flex'} style={{ width: '100%' }} align="middle">
 2           <Col span={17}>
 3             <FormItem label="一号"
 4               style={{ display: 'flex' }}
 5               {...formItemLayout}
 6               required={true}
 7             >
 8               {form.getFieldDecorator('one', config)(
 9                 <Input placeholder="one" onChange={onChange} />
10               )}
11             </FormItem>
12           </Col>
13         </Row>
14         <Row type={'flex'} style={{ width: '100%' }} align="middle">
15           <Col span={17}>
16             <FormItem label="二号"
17               style={{ display: 'flex' }}
18               {...formItemLayout}
19               required={true}
20             >
21               {form.getFieldDecorator('two', config)(
22                 <Input placeholder="two" onChange={onChange}/>
23               )}
24             </FormItem>
25           </Col>
26         </Row>
27         <Row type={'flex'} style={{ width: '100%' }} align="middle">
28           <Col span={17}>
29             <FormItem label="三号"
30               style={{ display: 'flex' }}
31               {...formItemLayout}
32               required={true}
33             >
34               {form.getFieldDecorator('three', config)(
35                 <InputNumber placeholder="three" onChange={onChange} />
36               )}
37             </FormItem>
38           </Col>
39         </Row>
40         <Row type={'flex'} style={{ width: '100%' }} align="middle">
41           <Col span={17}>
42             <FormItem label="总数"
43               style={{ display: 'flex' }}
44               {...formItemLayout}
45               required={true}
46             >
47               {form.getFieldDecorator('sum', config)(
48                 <Input placeholder="sum" />
49               )}
50             </FormItem>
51           </Col>
52         </Row>
View Code

 

js

const onChange=(e)=>{
    const one=form.getFieldValue("one")
    const two = form.getFieldValue("two")
    const three = form.getFieldValue("three")
    console.log(one,two,three,"one two three");
  }


//此时你就会发现不能实时的更新  原因是当你动态的改变了值dom结构并没有实时更新过来,所以只有你第二次触发此函数的时候才会达到上一次的值

//那么你非要这样获取怎么办呢    两种思路可以获取到

//一是   将你的打印简单粗暴的放在一个定时器中就可以获取去
//二是  将这个函数封装成一个异步函数也可以获取到


//由此可见这样都比较不好   所以antd给你提供了e   目的就是想让你当前输入用e.target.value来获取 而不是用getFieldValue获取当前输入值

//因此可见from的onSubmit就是一个异步方法可以将你输入的所有值获取到

  

Antd Table 支持使用 Input 组件进行筛选,可以在列的配置中设置 `filterDropdown` 属性来自定义筛选菜单。 例如,在一个列上设置了 Input 筛选,可以这样写列的配置: ```jsx { title: 'Name', dataIndex: 'name', filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => ( <div style={{ padding: 8 }}> <Input placeholder="Search name" value={selectedKeys[0]} onChange={(e) => setSelectedKeys(e.target.value ? [e.target.value] : [])} onPressEnter={() => confirm()} style={{ width: 188, marginBottom: 8, display: 'block' }} /> <Button type="primary" onClick={() => confirm()} size="small" style={{ width: 90, marginRight: 8 }} > Search </Button> <Button onClick={() => clearFilters()} size="small" style={{ width: 90 }}> Reset </Button> </div> ), onFilterDropdownVisibleChange: (visible) => { if (visible) { setTimeout(() => this.searchInput.select()); } }, render: (text) => <Highlighter highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }} searchWords={[this.state.searchText]} autoEscape textToHighlight={text.toString()} />, } ``` 上面的配置中,`filterDropdown` 属性用来自定义筛选菜单。它接收一个对象作为参数,包含四个属性: - `setSelectedKeys`:设置选中的筛选项 - `selectedKeys`:当前选中的筛选项 - `confirm`:确认筛选的回调函数 - `clearFilters`:重置筛选的回调函数 在 `filterDropdown` 中,我们使用了 AntdInput 和 Button 组件来实现筛选菜单。其中,Input 的值使用 `selectedKeys[0]`,并在 `onChange` 事件中通过 `setSelectedKeys` 来设置选中的筛选项。按下 Enter 键后,会触发 `confirm` 回调函数进行筛选。点击 Search 按钮和 Reset 按钮,分别会触发 `confirm` 和 `clearFilters` 回调函数。 除了 `filterDropdown` 属性外,还需要设置 `onFilterDropdownVisibleChange` 属性来控制筛选菜单的显示和隐藏。在 `onFilterDropdownVisibleChange` 中,我们使用了 `setTimeout` 函数来延迟调用 `select` 方法,以便在 Input 出现后立即获取焦点。 最后,在 `render` 函数中,我们使用了 `Highlighter` 组件来高亮匹配的文本。 需要注意的是,上面的示例中使用了 `this.state.searchText` 来表示当前的搜索文本。如果需要将搜索结果保存到状态中,还需要定义 `state` 和 `setState` 方法。另外,如果使用函数式组件,需要使用 `useState` 钩子来实现状态管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗨,玉麟

打赏19.88源码双手奉上

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值