amis icon indicating copy to clipboard operation
amis copied to clipboard

textarea没有clearable属性?

Open foxfire881 opened this issue 4 years ago • 10 comments

实现场景:

点击按钮清空textarea

存在的问题:

其他控件都有clearable属性,textarea没有clearable

当前方案:

不想清空整个form,只清空单独textarea项,怎么清空呢?

code here...

foxfire881 avatar Jan 19 '22 13:01 foxfire881

补充:我的button在form外面,用onClick事件的props.formStore无效。如何点击button把textarea清空呢?

foxfire881 avatar Jan 19 '22 13:01 foxfire881

再补充,出现这个问题是因为form是在弹窗dialog内部的。本来想在form的actions中加个button用onclick清空textarea,结果测试发现如果form在dialog内部,则form的actions无效(渲染不出来),只能把actions放在外层作为dialog的actions才有效,但这样一来就不能用props.formStore了,会报错。

foxfire881 avatar Jan 19 '22 14:01 foxfire881

“则form的actions无效(渲染不出来)“,是不是表单配置了wrapWithPanel:false?可以隐藏掉dialog的actions,使用表单的。

gauliang avatar Jan 20 '22 01:01 gauliang

“则form的actions无效(渲染不出来)“,是不是表单配置了wrapWithPanel:false?可以隐藏掉dialog的actions,使用表单的。

你测试过吗?我测试了,显示不出来

{
  "type": "page",
  "title": "Test",
  "body": {
    "type": "button",
    "label": "Test",
    "actionType": "dialog",
    "dialog": {
      "title": "DialogTitle",
      "size": "lg",
      "body": [
        {
          "type": "form",
          "title": "FormTitle",
          "body": [
            {
              "type": "input-text",
              "label": "标题",
              "name": "title",
              "required": true
            }
          ],
          "actions": [                               //form的actions
            {
              "type": "button",
              "label": "B1"
            },
            {
              "type": "button",
              "label": "B2"
            }
          ]
        }
      ],
      "actions": []    //即使屏蔽dialog的actions,上面form的actions也显示不出来
    }
  }
}

foxfire881 avatar Jan 20 '22 06:01 foxfire881

搞不懂为什么要弄个formStore出来,直接props.setValues控制与data绑定的控件挺好的,js里面也是直接通过id或者name就可以控制页面上所有的组件。弄个formStore出来反而不能跨表单设置表单项的值了。

foxfire881 avatar Jan 20 '22 06:01 foxfire881

配置 wrapWithPanel:true 后会用 panel 包裹组件,此时可以配置actions 或 footer,可参考:

{
  "type": "page",
  "title": "Test",
  "body": {
    "type": "button",
    "label": "Test",
    "actionType": "dialog",
    "dialog": {
      "title": "DialogTitle",
      "size": "lg",
      "body": [
        {
          "type": "form",
          "wrapWithPanel": true,
          "api":"/",
          "title": false,
          "body": [
            {
              "type": "input-text",
              "label": "标题",
              "name": "title",
              "required": true
            }
          ],
          "actions": [ //form的actions
            {
              "type": "button",
              "label": "B1"
            },
            {
              "type": "button",
              "label": "B2"
            }
          ],
          "footer": [ //form的actions
            {
              "type": "submit",
              "label": "footer-button"
            },
            {
              "type": "button",
              "label": "B2"
            }
          ]
        }
      ],
      "actions": [] //即使屏蔽dialog的actions,上面form的actions也显示不出来
    }
  }
}

gauliang avatar Jan 20 '22 06:01 gauliang

配置 wrapWithPanel:true 后会用 panel 包裹组件,此时可以配置actions 或 footer,可参考:

这么测试是有了,不过还是有些问题。form文档中说wrapWithPanel默认值就是true,也就是不配置wrapWithPanel属性的话,默认会把这个属性设为true;但实际测试下来,必须显式明确指定"wrapWithPanel": true才可以正常渲染出来,如果不配置"wrapWithPanel"或者"wrapWithPanel": false的话就显示不出来了。我之前就是没配置"wrapWithPanel": true这项所以渲染不来。

另外,设置"wrapWithPanel": true,的话,整个表单会有边框存在,整个dialog下面也会留一段空白,看着不太好看。不知道大佬有没有啥办法可以跨表单设置form项的值不?能用dialog的actions控制表单项值最好了,那个formStore看着真扎眼也很难用,不知道有啥方法可以绕过?

foxfire881 avatar Jan 20 '22 07:01 foxfire881

可以的,参考:

{
    "type": "page",
    "title": "Test",
    "body": {
        "type": "button",
        "label": "Test",
        "actionType": "dialog",
        "dialog": {
            "title": "DialogTitle",
            "size": "lg",
            "body": [
                {
                    "type": "form",
                    "body": [
                        {
                            "type": "input-text",
                            "label": "标题",
                            "name": "title",
                            "required": true
                        }
                    ]
                }
            ],
            "actions": [
                {
                    "type": "button",
                    "onClick": "props.store.children[0].setValueByName('title','')",
                    "label": "B1-clear"
                },
                {
                    "type": "button",
                    "label": "B2"
                }
            ]
        }
    }
}

gauliang avatar Jan 20 '22 07:01 gauliang

@foxfire881 通过 props 中的 store 找出表单的 formStore 后,删除即可

gauliang avatar Jan 20 '22 07:01 gauliang

                "onClick": "props.store.children[0].setValueByName('title','')",

测试好用,多谢大佬~~ 不懂就问,继续请教:请问这个setValueByName方法是js内置的吗?如何得知children[i]有setValueByName方法,在哪里查看?我之前在chrome的console中把props打印出来看到props.store.form属性,没注意到有children属性。尝试用了以下几个方案去控制表单项中url的值:

1、props.store.form.url='' //报错 2、props.store.form.setValues({'url':''}) //报错,form没有setValues方法 3、onClick:"console.log(document.getElementsByName("url")[0].value)"可以正确获取并打印出url的值,但是onClick: "document.getElementsByName("url")[0].value=''"无效,不能把url置空。

为啥只有children[i]才有setValueByName方法呢?如何在chorme控制台中查看DOM结构中各属性节点有哪些可以调用的方法?

捕获

foxfire881 avatar Jan 20 '22 08:01 foxfire881