首页 > 其他分享 >低代码平台amis学习 五:添加「日期范围」参数,解决起止日期提取问题

低代码平台amis学习 五:添加「日期范围」参数,解决起止日期提取问题

时间:2022-09-07 14:46:43浏览次数:123  
标签:status name data 起止日期 label 添加 date type amis

之前写过一个造数接口,它需要传递日期参数,如下

前端暴露一个「月份范围」组件,选好日期后点击提交,会提交「起始月份」和「终止月份」2个参数

 

接下来我要把这个功能移植到amis平台上

 

通过查看文档,发现官方有提供「月份范围」功能,传送门:InputMonthRange 月份范围

 

根据描述,先创建如下表单

对应代码

{
                    "title": "产值相关",
                    "hash": "tab3",
                    "body": [
                        {
                            "type": "grid",
                            "columns": [


                                {
                                    "type": "wrapper",
                                    "style": {

                                    },
                                    "body": [{
                                        "type": "form",
                                        "title": "创建产值计划",
                                        "mode": "horizontal",
                                        "autoFocus": false,
                                        "horizontal": {
                                            "leftFixed": "md"
                                        },


                                        "body": [
                                            {
                                                "label": "合同编号",
                                                "type": "input-text",
                                                "size": "md",
                                                "name": "contractid"
                                            },
                                            {
                                                "type": "input-month-range",
                                                "name": "date",
                                                "format": "YYYY-MM",
                                                "_format": "设置值的格式,https://aisuda.bce.baidu.com/amis/zh-CN/components/form/input-date",
                                                "size": "md",
                                                "label": "月份范围",
                                                "labelRemark": "月份范围"
                                            },


                                            {
                                                "label": "选择状态",
                                                "type": "radios",
                                                "size": "md",
                                                "name": "status",
                                                "options": [
                                                    {
                                                        "label": "草稿",
                                                        "value": "1"
                                                    },
                                                    {
                                                        "label": "已上报",
                                                        "value": "2"
                                                    }
                                                ]
                                            },
                                            {
                                                "type": "control",
                                                "name": "response",
                                                "label": "接口返回结果",
                                                "body": {
                                                    "type": "json",
                                                    "levelExpand": 100
                                                }
                                            }
                                        ],
                                        "actions": [
                                            {
                                                "//": "type为submit时, 表示该按钮是一个行为按钮, 点击可以提交请求",
                                                "type": "submit",

                                                "label": "提交",

                                                "//": "level配置按钮颜色, https://aisuda.bce.baidu.com/amis/zh-CN/components/action?page=1#%E4%B8%BB%E9%A2%98",
                                                "level": "primary",

                                                "api": {
                                                    "method": "get",
                                                    "url": "http://localhost:8000/data_factory/create_output_plan",
                                                    "data": {
                                                        "code": "${contractid}",
                                                        "status": "${status}"
                                                        "start_date": "${date}",
                                                        "end_date": "${date}"
                                                    },
                                                    "adaptor": "return {\n    ...payload,\n    data: {\n        ...payload.data,\n        response: response.data\n    }\n}"
                                                }
                                            },

                                            {
                                                "type": "reset",
                                                "label": "重置"
                                            }
                                        ]
                                    }
                                    ]
                                }
                            ]
                        }
                    ]
                }

 

关于月份范围参数,做了如下处理

{
    "type": "input-month-range",
    "name": "date",
    "format": "YYYY-MM",
    "_format": "设置值的格式,https://aisuda.bce.baidu.com/amis/zh-CN/components/form/input-date",
    "size": "md",
    "label": "月份范围",
    "labelRemark": "月份范围"
},

添加format属性,设置提交值的格式,默认为时间戳,这样设置后会改为"年-月"

api请求参数设置如下

"api": {
    "method": "get",
    "url": "http://localhost:8000/data_factory/create_output_plan",
    "data": {
        "code": "${contractid}",
        "status": "${status}",
        "start_date": "${date}",
        "end_date": "${date}"
    },

因为后端接口需要接收2个参数:开始日期和结束日期,这里先试验一下实际发送请求时,${date}的值是什么样的

可以发现${date}是是一个由起止月份组成的字符串,正常情况应该把开始月份赋给start_date,结束月份赋给end_date

尝试做如下修改

"api": {
    "method": "get",
    "url": "http://localhost:8000/data_factory/create_output_plan",
    "data": {
        "code": "${contractid}",
        "status": "${status}",

        "start_date": "${date}[0]",
        "end_date": "${date}[1]"
    },

结果如下

没有得到预期结果,看来${date}并不是一个数组,而是一个字符串,所以不能直接这样取值

经过多番试验,终于在官方文档中找到了一个办法(太不容易了

标签:status,name,data,起止日期,label,添加,date,type,amis
From: https://www.cnblogs.com/hanmk/p/16664545.html

相关文章