经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
react的ui库antd中form表单使用SelectTree反显问题及解决
来源:jb51  时间:2023/1/18 8:41:52  对本文有异议

react ui库antd中form表单使用SelectTree反显问题

最近遇到这个问题,后来找到原因

1.formItem 需要使用initialValue赋值。

2.这个组件需要一开始就存在不能是条件渲染,非要用条件渲染需要让它先显示,然后根据条件让它不显示。

例子:

  1. state={
  2. ?treeList:[],
  3. ?showTree:ture,
  4. ?value:[]
  5. }
  6. componentDidMount(){
  7. ? ?//这里请求数据用定时器代替
  8. ? ?setTimeOut(()=>{ ?//一般会先拿到listTree先渲染
  9. ? ? ? this.setState({
  10. ? ? ? ? treeList:[{value:'aaa',title:'你好react'}]
  11. ? ? ? })
  12. ? ? ? setTimeOut(()=>{ //第二次请求反显的值和是否显示
  13. ? ? ? ? ?if(需要显示tree){ //下面俩条if一般不会有同时出现的时候
  14. ? ? ? ? ? ? ?this.setState({
  15. ? ? ? ? ? ? ? ?value:['aaa'], ?
  16. ? ? ? ? ? ? ?})?
  17. ? ? ? ? ?}
  18. ? ? ? ? ?if(不需要显示tree){
  19. ? ? ? ? ? ? this.setState({
  20. ? ? ? ? ? ? ? ?showTree:false, ?
  21. ? ? ? ? ? ? ?})?
  22. ? ? ? ? ?}
  23. ? ? ? ??
  24. ? ? ? })
  25. ? ?},2000)
  26. }
  27. ?render() {
  28. ? ? const {treeList,value,showTree}=this.state;
  29. ? ? const tProps = {
  30. ? ? ? treeData:treeList,
  31. ? ? ? value:value
  32. ? ? };
  33. ? ? return?
  34. ? ? <Form>
  35. ? ? ? <Form.Item>
  36. ? ? ? ? ? {getFieldDecorator('selectTree', {
  37. ? ? ? ? ? ? initialValue:[]
  38. ? ? ? ? ? })(
  39. ? ? ? ? ? ? ?{showTree&&<TreeSelect {...tProps} />}
  40. ? ? ? ? ? )}
  41. ? ? ? ? </Form.Item>
  42. ? ? ?</Form>
  43. ? ? ?
  44. ? }

react antd form表单回显踩坑

需求如下

在弹窗里显示一个表单,并进行回显,涉及到的组件有,简单的input框,inputNumber框,select,此处前端懒得让后端写接口点击自己获取到form表单里的数据,方法也不难 在链接处添加点击事件 代码如下(简单记录)

  1. onClick={() => this.getInformation(info)}
  2. //此处是点击事件的方法
  3. getInformation = (info) => {
  4. //此处打印的东西见下图
  5. //此处存在问题如果强制转换了一次重复转换会报错 参数undefined
  6. if (
  7. typeof info.app == "string" &&
  8. typeof info.file == "string"
  9. )
  10. {
  11. console.log(info);
  12. //把select多选的类型强制转成object类型并且赋值给原来的属性
  13. var newObj1 = eval('('+info.app+')');
  14. var newObj = eval('('+info.file+')');
  15. info.file=newObj
  16. info.app=newObj1
  17. store.getInformation();
  18. //储存到当前state中
  19. this.setState({getInform:info})
  20. }else{
  21. this.setState({getInform:info})
  22. }
  23. };
  24. //此处通过组件通信暴露给父组件
  25. <Get putfile={this.state.getInform}/>
  26. //父组件处接收参数 在render函数处接收
  27. const form = this.props["putfile"];

此处为上面log打印的东西

打印的数据格式都是string类型的,对于select的多选string类型的属性当然不满足了,所以需要进行数据处理

  1. //此处添加了 回显实例 此代码antd版本为 3 必填校验已经实现直接cv即可
  2. //此处为input框
  3. <Form.Item
  4. label="应用名称"
  5. // hasFeedback
  6. required
  7. >
  8. {getFieldDecorator(
  9. "name",
  10. { initialValue: form["name"] },
  11. {
  12. rules: [{ required: true, message: "请输入应用名称!" }],
  13. }
  14. )(<Input placeholder="请输入应用名称" />)}
  15. </Form.Item>
  16. // 此处为number框
  17. <Form.Item label="金额" required>
  18. {getFieldDecorator(
  19. "money",
  20. { initialValue: form["money"] },
  21. {
  22. rules: [{ required: true, message: "请输入应用上架金额!" }],
  23. }
  24. )(
  25. <InputNumber
  26. style={{ width: "100%" }}
  27. min="0"
  28. step="1"
  29. precision="2"
  30. placeholder="请输入应用上架金额"
  31. />
  32. )}
  33. </Form.Item>
  34. // 此处为select多选框 (此处未做必填校验)
  35. <Col span={12}>
  36. <Form.Item label="文件类型" hasFeedback validateStatus="">
  37. {getFieldDecorator("file", {
  38. initialValue: form["file"],
  39. })(
  40. <Select
  41. mode="tags"
  42. style={{ width: "100%" }}
  43. placeholder="请选择文件类型"
  44. onChange={this.handleChange}
  45. >
  46. {children}
  47. </Select>
  48. )}
  49. </Form.Item>
  50. </Col>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号