여기서는 JSON 데이터가 다양한 동적 데이터를 나타내며 그리드 또는 고정 된 양식과 같은 사전 미리 준비된 컨트롤을 단순히 사용할 수 없다고 가정합니다.
JSON 콘텐츠를 기반으로 위젯을 동적으로 생성하는 컨테이너 클래스를 만드는 것이 필요합니다. 물론 직접 작성해야합니다.
극단적 인 점은 상점의 JSON 콘텐츠를 유효한 인수 (예 : Ext.widget
)로 만드는 것입니다.하지만 실현 가능성이 낮거나 바람직하지는 않습니다.
좀 더 중간 위치를 얻으려면 JSON 데이터를 사용하여 조건에 따라 추가 할 위젯을 결정하십시오.
Ext.define('MyFormContainer', {
extend: 'Ext.form.FormPanel',
config: {
// A store or MixedCollection of JSON data objects, keyable by id.
formData: null
},
layout: 'vbox',
initComponent: function() {
this.callParent(arguments);
this.getFormData().each(this.addField, this)
},
addField: function(fieldData) {
var widgetConfig = this.buildWidgetConfig(fieldData);
this.add(widgetConfig);
},
buildWidgetConfig: function(fieldData) {
// The heart of the factory. You need a way to determine what sort of widget to make for
// the field. For the example config, a fieldset with three fields would probably be
// appropriate:
var fieldSet = { xtype: 'fieldset', layout: 'hbox' };
var items = [];
items[0] = { xtype: 'textfield', name: fieldData['FieldName'] };
// this would be a link to a custom widget to handle the operator. Or maybe you could
// just spit out text, if it's not meant to be alterable.
items[1] = { xtype: 'myoperator_' + fieldData['operator'], name: 'operator' };
items[2] = { xtype: fieldData['widget'], name: 'value' }
fieldSet.items = items;
return fieldSet;
}
})
이 간단하고 인위적인 예이지만 (의 누락 등의 빈칸에 작성해야 후에는 필요하고 사용자 정의 연산자 위젯 :
은 대략적인 윤곽, 당신이 뭔가를 원하는) JSON 데이터를 기반으로 양식을 렌더링합니다.
(개인적으로이 접근법을 사용합니다 - 간단한 예제에서 볼 수있는 더 정교함 - 서버 제공 양식 설명을 기반으로 동적 양식을 생성합니다)