首先我们的把年月季的控件拉到界面上来:
<ext:ComboBox ID="cbTime" FieldLabel="月/季/年" runat="server" LabelWidth="60" Width="160"
LabelAlign="Right" Editable="false">
<Items>
<ext:ListItem Text="年度查询" Value="0" />
<ext:ListItem Text="季度查询" Value="1" />
<ext:ListItem Text="月查询" Value="2" />
</Items>
<SelectedItems>
<ext:ListItem Text="年度查询" Value="0" />
</SelectedItems>
<Listeners>
<Select Handler="if (records.length > 0) {CbTimeSelect(records[0].data.field1);}">
</Select>
</Listeners>
</ext:ComboBox>
FieldLabel="月" AllowBlank="false" Editable="false" StyleSpec="color:red" LabelAlign="Right"
Width="120" LabelWidth="20" AutoFocus="true">
<Listeners>
<Change Handler="startMonthBuild(item,newValue,oldValue);">
</Change>
</Listeners>
</ext:DateField>
<ext:DateField runat="server" ID="MonthEnd" Name="CheckDate" Format="yyyy-MM" Type="Month"
FieldLabel="至" AllowBlank="false" Editable="false" StyleSpec="color:red" LabelAlign="Right"
Width="120" LabelWidth="20" AutoFocus="true">
<Listeners>
<Change Handler="endMonthBuild(item,newValue,oldValue)">
</Change>
</Listeners>
</ext:DateField>
LabelAlign="Right" Editable="false" Hidden="true" AllowBlank="false">
<Items>
</Items>
<Listeners>
<Change Handler="if(App.cbEDate.getValue() && newValue>App.cbEDate.getValue()){
Ext.Msg.alert('提示', '开始时间不能大于结束时间');
App.cbBDate .clear()
}
" />
</Listeners>
</ext:ComboBox>
<ext:ComboBox ID="cbEDate" FieldLabel="至" runat="server" LabelWidth="20" Width="130"
LabelAlign="Right" Editable="false" Hidden="true" AllowBlank="false">
<Items>
</Items>
<Listeners>
<Change Handler="if(App.cbBDate .getValue() && newValue<App.cbBDate .getValue()){
Ext.Msg.alert('提示', '开始时间不能大于结束时间');
App.cbEDate.clear()
}
" />
</Listeners>
</ext:ComboBox>
然后就是后台的代码了:
//下拉框季度
function quarterBuild() {
App.cbBDate.setValue();
App.cbEDate.setValue();
var length = App.cbBDate.store.data.length;
for (var i = 0; i < length; i++) {
App.cbBDate.removeByIndex(0);
App.cbEDate.removeByIndex(0);
}
var date = new Date();
var year = date.getFullYear();
var quarter = Math.ceil((date.getMonth() + 1) / 3);
var firstQuarter = quarter;
for (var i = 0; i < 12; i++) {
App.cbBDate.addItem(year + "年" + quarter + "季度", year + "" + quarter);
App.cbEDate.addItem(year + "年" + quarter + "季度", year + "" + quarter);
if (i == 0) {
App.cbEDate.setValue(year + "" + quarter, year + "年" + quarter + "季度");
}
if (i == 5) {
App.cbBDate.setValue(year + "" + quarter, year + "年" + quarter + "季度");
}
quarter--;
if (quarter <= 0) {
quarter = 4;
year--;
}
}
}
//下拉框年
function yearBuild() {
App.cbBDate.setValue();
App.cbEDate.setValue();
var length = App.cbBDate.store.data.length;
for (var i = 0; i < length; i++) {
App.cbBDate.removeByIndex(0);
App.cbEDate.removeByIndex(0);
}
var date = new Date();
var year = date.getFullYear();
for (var i = 0; i < 12; i++) {
App.cbBDate.addItem(year + "年", "" + year + "");
App.cbEDate.addItem(year + "年", "" + year + "");
if (i == 0) {
App.cbEDate.setValue("" + year + "", year + "年");
}
if (i == 5) {
App.cbBDate.setValue("" + year + "", year + "年");
}
year--;
}
}
//下拉初始月份
function startMonthBuild(item, newValue, oldValue) {
if (App.MonthEnd.getValue() && newValue > App.MonthEnd.getValue()) {
Ext.Msg.alert("提示", "开始时间不能大于结束时间");
App.MonthStart.clear();
}
else {
var year = newValue.getFullYear();
var month = newValue.getMonth() + 1;
App.hideMonthStart.value = year + "" + month;
}
}
//下结束月份
function endMonthBuild(item, newValue, oldValue) {
if (App.MonthStart.getValue() && newValue < App.MonthStart.getValue()) {
Ext.Msg.alert("提示", "开始时间不能大于结束时间");
App.MonthEnd.clear();
}
else {
var year = newValue.getFullYear();
var month = newValue.getMonth() + 1;
App.hideMonthEnd.value = year + "" + month;
}
}
// 下拉框 月/年/季度
function CbTimeSelect(type) {
if (type == "2") {
App.cbBDate.hide();
App.cbEDate.hide();
App.MonthStart.show();
App.MonthEnd.show();
} else if (type == "0") {
App.cbBDate.setFieldLabel("年");
App.MonthStart.hide();
App.MonthEnd.hide();
App.cbBDate.show();
App.cbEDate.show();
yearBuild();
}
else {
App.cbBDate.setFieldLabel("季度");
App.MonthStart.hide();
App.MonthEnd.hide();
App.cbBDate.show();
App.cbEDate.show();
quarterBuild();
}
}
取值直接名称.value即可。