zoukankan      html  css  js  c++  java
  • Ant Design 表单中getFieldDecorator、getFieldValue、setFieldValue用法

    Ant Design 表单中getFieldDecorator、getFieldValue、setFieldValue用法

    一、getFieldDecorator

    getFieldDecorator是一个方法,这个方法接收两个参数,第一个是表单的字段对象,第二个是验证规则。这个方法本身返回一个方法,需要将需要获取值的标签包裹进去。

    <From>
      <FormItem>
          //JS代码书写时需要用 { } 包裹起来,不能直接写在代码块中  
          {
              getFieldDecorator('userName',{
                initialValue:'Jack',
                 rules:[]
                 })(
                        <Input placeholder='请输入用户名'/>
                    )
            }
      </FormItem>
    </From>
    
    

    第一个参数是用户自定义的、用于识别该控件的变量名,这样便于在获取或设置该控件的值。
    2019.3.12补充:值得注意的是,getFieldDecorator是一个非常智能的方法,它可以获得自定义组件的value值,在提交表单时会很方便。其次,initialValue的值会覆盖子组件中的placeHolder,如果子组件是下拉框,也会根据initialValue的值来匹配自己的value,并显示相应的值,可以说非常智能了。

    二、getFieldValue

      handleSubmit = e => {
        const { dispatch } = this.props;
        e.preventDefault();
        var date_juban = this.props.form.getFieldValue('date_juban');
        this.state.open_time_start = date_juban[0];
        this.state.open_time_end = date_juban[1];
        if (this.refs.pics.state.fileList.length > 0)
          this.state.image = this.refs.pics.state.fileList[0].response.url;
        this.state.location_longitude = this.props.form.getFieldValue('location_longitude');
        this.state.location_latitude = this.props.form.getFieldValue('location_latitude');
        }
    

    在提交表单或是与后端交互时,如果需要一个控件的值,那么就用this.props.form.getFieldValue('变量名')的方式进行获取,注意:‘变量名’这个参数只能由getFieldDecorator所设定的。

    注意:getFieldValue不能获取没有使用getFieldDecorator绑定的控件(即:如果控件值标注了id属性,用这个方法无效)。应使用document.getElementById("id名").value的方式进行获取值。

    三、setFieldValue

    <FormItem {...formItemLayout} label={<span>地图</span>}>
                  <AMapModule
                    lng={''}
                    lat={''}
                    address={getFieldValue('position')}
                    getMapPoint={point => {
                      setFieldsValue({
                        location_latitude: point.lat,
                        location_longitude: point.lng,
                      });
                    }}
                    getMapAddress={address => {
                      setFieldsValue({
                        position: address,
                      });
                    }}
                  />
                </FormItem>
    

    上述代码是我在表单中截取的一部分,该控件是一个地图控件。用户点击地图中的某个位置,会在同页面的input框中实时显示经度、维度、位置描述。这三个input框的id分别是location_latitude、location_longitude、position。

  • 相关阅读:
    HTML5学习之画布和SVG(四)
    HTML5学习之视频与音频(三)
    HTML5学习之智能表单(二)
    HTML5学习之文档结构和语义(一)
    WCF分布式开发必备知识(3):Web Service 使用
    比较各大挪动门户网站淘宝、京东、网易、新浪、腾讯meta标签的异同
    jQuery Mobile学习之grid、等待显示的ajax效果、页面跳转、页面跳转传递参数等(二)
    jQuery Moblie 学习之page、button、theme、panel、listview、controlgroup、navbar等(一)
    使用Modernizr探测HTML5/CSS3新特性(转载)
    设计模式学习之模板方法模式(TemplateMethod,行为型模式)(9)
  • 原文地址:https://www.cnblogs.com/tian874540961/p/10237713.html
Copyright © 2011-2022 走看看