【发布时间】:2014-01-30 21:10:40
【问题描述】:
我想从文件中读取 json 数据 - json 文件的内容如下所示
{
"form": {
"fields" : [
{
"field":"textfield",
"name": "username",
"constrain": "5-10",
"value": ""
},
{
"field":"textfield",
"name": "password",
"constrain": "5-10",
"value": ""
},
{
"field":"datepickerfield",
"name": "Birthday",
"constrain": "5-10",
"value": "new Date()"
},
{
"field":"selectfield",
"name": "Select one",
"options":[
{"text": "First Option", "value": 'first'},
{"text": "Second Option", "value": 'second'},
{"text": "Third Option", "value": 'third'}
]
},
]
}
}
型号
Ext.define('dynamicForm.model.Form', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'field', type: 'string'},
{name: 'name', type: 'string'},
{name: 'constrain', type: 'string'},
{name: 'value', type: 'string'}
],
hasMany: {model: 'dynamicForm.model.SelectOption', name: 'options'}
}
});
Ext.define('dynamicForm.model.SelectOption', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'text', type: 'string'},
{name: 'value', type: 'string'}
]
}
});
商店
Ext.define('dynamicForm.store.FormStore', {
extend : 'Ext.data.Store',
storeId: 'formStore',
config : {
model : 'dynamicForm.model.Form',
proxy : {
type : 'ajax',
url : 'form.json',
reader : {
type : 'json',
rootProperty : 'form.fields'
}
},
autoLoad: true
}
});
这就是我尝试的目的。
var fromval = Ext.create('dynamicForm.store.FormStore');
fromval.load(function (){
console.log(fromval);
// i added register view which having form panel with id "testForm"
Ext.Viewport.add({
xtype : 'register'
});
for(i=0; i< fromval.getCount(); i++) {
console.log("------");
Ext.getCmp('testForm').add({
xtype: fromval.getAt(i).data.field,
label: fromval.getAt(i).data.name,
value: fromval.getAt(i).data.value,
options: [
{text: "First Option", value: "first"},
{text: "Second Option", value: "second"},
{text: "Third Option", value: "third"}
]
});
}
});
两个文本文件和日期都很好,但我不知道如何从商店中获取选择字段的选项,现在刚刚听到编码。
总而言之基于以上json数据,我需要动态创建煎茶表单。
【问题讨论】:
-
@TDeBailleul 我在那个网站上找不到任何与我的问题相关的帖子。
-
不,与您的问题无关。但是你读过这篇文章吗?来自文章:“如果您是开发人员,并且您要向其他开发人员询问技术问题(在论坛上、通过电子邮件、聊天频道或亲自),您最好准备好回答问题“你试过什么?” [...] 问题在于,这个人解决问题的技巧是寻求解决方案。不是寻求关于如何处理任务的建议,而只是询问代码,完全形成并准备好“
-
@TDeBailleul 我从不在堆栈溢出中寻找现成的代码,我只是想要一个想法,我正在尝试
-
好的,您可以访问 JSON 吗?您可以在发送/检索之前对其进行修改吗?
-
@TDeBailleul,我更新了我尝试过的内容
标签: json extjs sencha-touch sencha-touch-2