Json Schema Form Condition show/hide based on enum selected item

好久不见. 提交于 2019-12-24 18:13:08

问题


I have an angular project in which I'm using JSON Schema Form (angular6-json-schema-form) to build forms in JSON schema.

The json schema form specification allows the use of a condition switch to selectively show/hide elements based on the value of another form element. The only examples given in the docs though are simple boolean's (if X has a value or not, then show Y).

In my example, I need to show/hide a "placeholder" text input when the text input type selected from a select list is one of (text, email, url) but NOT show it when its (password, color). See the enum array below that contains the options I need to test against.

{
schema: {
type: 'object',
required: ['title'],
properties: {
type: {
    title: 'Input Type',
    description: 'Input type assists browser/phone UI behavior',
    type: 'string',
    enum: ['color', 'email', 'integer', 'number', 'password', 'tel', 'text']
  },
placeholder: {
    title: 'Placeholder',
    description: 'The placeholder is shown inside the text element by default',
    type: 'string'
  }
},
layout: [
{ items: ['title', 'type'] },
{
  key: 'placeholder',
  condition: 'model.type.enum[selectedValue]!=="color"'
},
}

In the example above, the only thing that I can get to work to show/hide the placeholder element is below:

{
  key: 'placeholder',
  condition: 'model.type'
}

Which simply shows the element when ANYTHING other than NONE is selected.

I've tried:

condition: 'model,type.modelValue !== "color"'
condition: 'type.modelValue !== "color"'
condition: 'model.type !== "color"'

And none of these trigger the appearance of the placeholder element, regardless what is selected in the type select element.


回答1:


Here is the working solution I've implemented that resolves my question:

layout: [
 { items: ['title', 'type'] },
 {
 type: 'section',
  condition: {
    functionBody: 'try { return model.type && model.type!=="color" && model.type!=="password" } catch(e){ return false }'
  },
  items: ['placeholder']
 },
...
]


来源:https://stackoverflow.com/questions/54653168/json-schema-form-condition-show-hide-based-on-enum-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!