react 中 如何给日期加条件并传值

react

日期查询原先是这样:

    {/* 日期 */}

    <FormItem label="日期">

        {getFieldDecorator('range-time-picker', rangeConfig)(

            <RangePicker showTime format="YYYY-MM-DD HH:mm:ss" />

        )}

    </FormItem>

    // 条件查询

    handleSearch = (e,key) => {

        const {

            dispatch,

            form

        } = this.props;

        form.validateFields((err, fieldsValue) => {

            if (err) return;

            const rangeTimeValue = fieldsValue['range-time-picker'] ? fieldsValue['range-time-picker'] : [];

            // console.log(rangeTimeValue);

            if (rangeTimeValue.length>0) {

                var values = {

                    ...fieldsValue,

                    'range-time-picker': [

                        rangeTimeValue[0].format('YYYY-MM-DD HH:mm:ss'),

                        rangeTimeValue[1].format('YYYY-MM-DD HH:mm:ss')

                    ],

                    'starttime': rangeTimeValue[0].format('YYYY-MM-DD HH:mm:ss'),

                    'endtime': rangeTimeValue[1].format('YYYY-MM-DD HH:mm:ss'),

                };

            } else {

               var values = {

                   ...fieldsValue,

               }

            }

            dispatch({

                type: ‘xxx/fetch',

                payload: values

            });

        });

    }

现在想要实现这样的日期查询,见图:

这种写法如下:

    {/* 日期 */}

    <FormItem

            label="日期"

        >

            {

                getFieldDecorator('rangtime', {

                    // initialValue: [moment(starttime), moment(endtime)],

                    rules: [{

                        type: 'array',

                        message: '请选择起始时间'

                    }]

                })(

                    <RangePicker

                        ranges={{

                            '7天': [moment(), moment().add(7, 'days')],

                            '15天': [moment(), moment().add(15, 'days')],

                            '自然月': [moment(), moment().add(1, 'month')],

                            '半年': [moment(), moment().add(183, 'days')],

                            '一年': [moment(), moment().add(366, 'days')],

                            '一季度': [moment(), moment().add(3, 'month')],

                            '两季度': [moment(), moment().add(6, 'month')],

                            '三季度': [moment(), moment().add(9, 'month')],

                            '至月底': [moment(), moment().endOf('month')],

                            '至年底': [moment(), moment().endOf('year')]

                        }}

                    />

                )

            }

    </FormItem>

    // 查询

    handleSearch = (e) => {

        e.preventDefault();

        const {

            dispatch,

            form

        } = this.props;

        form.validateFields((err, fieldsValue) => {

            if (err) return;

            // 日期

            const rangeTimeValue = fieldsValue['rangtime'] ? fieldsValue['rangtime'] : [];

            var values = {

                'starttime': '',

                'endtime': '',

                'customercode': this.state.customerID,

                'commoditycode': fieldsValue['commoditycode'],

                'billcode': fieldsValue['billcode'],

                'searchtype': fieldsValue['searchtype'],

            };

            if (rangeTimeValue.length > 0) {

                values['starttime'] = rangeTimeValue[0].format("Y-M-d");

                values['endtime'] = rangeTimeValue[1].format("Y-M-d");

            }

            this.props.handleSetState({

                formValues: values,

            })

            dispatch({

                type: ‘xxxxx/fetch',

                payload: values

            });

        });

    }

如果要默认显示,最近 30的的,怎么写?

以上是 react 中 如何给日期加条件并传值 的全部内容, 来源链接: utcz.com/z/384458.html

回到顶部