ExtJs在时间字段中填充值
ExtJs populating values in timefields
我正在学习ExtJs,我有一个与timefield
s的表单gridpanel,我想用从数据库中提取的JSON数据填充。
这是我要存储的数据:
Ext.create('Ext.data.Store', {
storeId:'employeeStore',
fields:['id', 'app_name', 'open_datetime', 'close_datetime', 'holiday_desc'],
data:[
{id:1, app_name:"A", open_datetime:"08:00", close_datetime:"09:00", holiday_desc:"ADay"},
{id:2, app_name:"B", open_datetime:"00:00", close_datetime:"23:59", holiday_desc:" BDay"}
]
});
现在我的表格中有timefield
,我想用我存储的数据open_datetime
和close_datetime
填充openTime
, closeTime
字段。
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.create('Ext.form.Panel', {
renderTo: Ext.getBody(),
width: 300,
bodyPadding: 10,
title: 'getters and setters',
items: [{
xtype: 'datemenu',
floating: false,
height: 210,
id: 'datePickerMenu',
width: 240
}, {
xtype: 'text',
text: 'openTime',
margin: '5px',
id: 'open',
dataIndex: 'open_datetime'
}, {
xtype: 'timefield',
style: {
margin: '5px'
}
}, {
xtype: 'text',
text: 'closeTime',
margin: '5px'
}, {
xtype: 'timefield',
style: {
margin: '5px'
}
}, {
xtype: 'text',
text: 'Message',
margin: '5px'
}, {
xtype: 'textfield',
style: {
margin: '5px'
}
}]
});
}
});
现在,我如何在 timefield
s中显示数据?在使用列时,我应该使用data_Index显示数据吗?
有几种方法可以做到这一点,根据所使用的Ext JS版本,可能只有一种方法。我假设你用的至少是v5版本。我假设的另一件事是,你有一个网格与你的商店,因为它不会有意义有一个表单加载两个记录…它不知道用哪条记录作为它的表单值。
此外,我认为这是一个很好的做法,避免使用id
字段,特别是如果你正在创建一个表单…如果你想在同一个视图中两次使用那个表单呢?你不能,因为你有两个相同的id。相反,您可能希望使用itemId
或reference
。
这是我想到的一个例子:
Ext.application({
name: 'Fiddle',
launch: function() {
var store = Ext.create('Ext.data.Store', {
fields: ['id', 'app_name', 'open_datetime', 'close_datetime', 'holiday_desc'],
data: [{
id: 1,
app_name: "A",
open_datetime: "08:00",
close_datetime: "09:00",
holiday_desc: "ADay"
}, {
id: 2,
app_name: "B",
open_datetime: "00:00",
close_datetime: "23:59",
holiday_desc: " BDay"
}
]
});
var first = store.first();
var second = store.getAt(1);
/* Non-Bound Form... this doesn't use model binding,
* but it uses the Name property, which relies on loading
* the form with a record */
Ext.define('My.Non.Bound.Form', {
extend: 'Ext.form.Panel',
bodyPadding: 10,
title: 'Non-Bound Form: getters and setters',
items: [{
xtype: 'datemenu',
floating: false
}, {
xtype: 'timefield',
fieldLabel: 'Open Time',
name: 'open_datetime',
margin: 5
}, {
xtype: 'timefield',
fieldLabel: 'Close Time',
margin: 5,
name: 'close_datetime'
}, {
xtype: 'textfield',
text: 'Message',
margin: '5px'
}, {
xtype: 'textfield',
style: {
margin: '5px'
}
}]
});
/* Bound Form... this makes use of ViewModel binding,
* so that means you must supply a ViewModel, and its
* data with a currentRecord property... this has many
* added benefits... I encourage you to read https://docs.sencha.com/extjs/6.0/application_architecture/view_models_data_binding.html */
Ext.define('My.Bound.Form', {
extend: 'Ext.form.Panel',
bodyPadding: 10,
title: 'Bound Form: getters and setters',
items: [{
xtype: 'datemenu',
floating: false
}, {
xtype: 'timefield',
fieldLabel: 'Open Time',
margin: 5,
bind: {
value: '{currentRecord.open_datetime}'
}
}, {
xtype: 'timefield',
fieldLabel: 'Close Time',
margin: 5,
bind: {
value: '{currentRecord.close_datetime}'
}
}, {
xtype: 'textfield',
text: 'Message',
margin: '5px'
}, {
xtype: 'textfield',
style: {
margin: '5px'
}
}]
});
var myNonBoundForm = Ext.create('My.Non.Bound.Form'); // render plain form approach
myNonBoundForm.loadRecord(first);
var myBoundForm = Ext.create('My.Bound.Form', {
viewModel: {
data: {
currentRecord: second
}
}
});
Ext.create('Ext.container.Container', {
renderTo: Ext.getBody(),
layout: {
type: 'hbox',
align: 'stretch'
},
defaults: {
flex: 1
},
items: [myNonBoundForm, myBoundForm]
})
}
});
在这个例子中,我向您展示了两种加载表单数据的方法。第一种方法是利用name
属性。这基本上类似于dataIndex
属性,但是您必须使用表单的loadRecord
方法或其他一些手动方法来填充表单字段值。
第二种方法利用ViewModel绑定…Ext JS 5中的一个新的、强大的概念。无论何时对绑定值进行更改,它都会更新模型的值,并在UI组件中反映该值。我建议您阅读我为ViewModel绑定提供的链接,因为它将涉及更多细节。
相关文章:
- 如何使用与单个查找字段相关的信息填充字段
- 动态填充字段的Jquery验证
- 使用另一个字段的日期+14天自动填充字段
- Mongoose,按填充字段对查询进行排序
- 使用 Codeigniter 和 jquery 在 Modal Bootstrap 3 中的 DB 中填充字段
- 填充字段后更改不透明度
- 根据下拉选择(Javascript / HTML)动态填充字段
- JavaScript 重置 PHP/AJAX 中的填充字段
- 对填充字段的 MongoDB 查询
- 根据 $_POST 值填充字段
- Javascript 自动完成表单并在预填充字段中附加文本
- Jquery 未完成填充字段
- 在客户端使用 javascript、ajax 或 php 计算填充字段的数量
- 使用地理位置坐标填充字段
- 根据同时在另一个字段中输入的内容自动填充字段
- 如何将struts1与angularjs混合以自动填充字段
- 填充字段,该字段是单个查询中嵌入数组的属性
- Phantom.js填充字段,点击提交,现在只能访问下一页
- 如何在预填充字段时清除iOS占位符JS文本
- 我的response.jsp赢了'无法正确填充字段