我定义了一个combobox,在它的on方法里定义了一个函数,将每次选择的value传过去,然后当做参数放入dataStore里,并load.
方法代码如下
[code="java"]
function loadDS(value){
var baseparams = dataStore.baseParams;
baseparams.method='cConditionService.getValueConditions';
baseparams.params=value;
dataStore.baseParams=baseparams;
dataStore.load();
}
[/code]
遇到的问题是,当我第一次进入页面并首次选择一个值后,后台hibernate打印出语句,但前台使用dataStore.getCount()的值是0,返回的数据没有插入到dataStore里,当我第二次选择一个值,无论选的是和首次一样还是不一样,返回的数据个数都是正确的,请问这种情况如何解决?
[b]问题补充:[/b]
完整代码
[code="java"]
var monthsStore = new Ext.data.SimpleStore({
fields:['state','abbr'],
data:[['1月','1'],['2月','2'],['3月','3'],['4月','4'],['5月','5'],['6月','6'],['7月','7'],['8月','8'],['9月','9'],['10月','10'],['11月','11'],['12月','12']]
});
var dataStore = new Ext.data.Store({//这是数据源
proxy : new Ext.data.HttpProxy({
url: yoururl
}),
reader: new Ext.data.JsonReader(
{
root: 'result.list',
totalProperty: '',
id: 'id'
},
['id','colDesc']
)
});
function loadDS(value){
var baseparams = dataStore.baseParams;
baseparams.method='cConditionService.getValueConditions';
baseparams.params=value;
dataStore.baseParams=baseparams;
dataStore.load();
alert("第二次:"+dataStore.getCount());
}
var calcSelect = new Ext.form.ComboBox({
fieldLabel : "测试",
store : methodDS,
displayField : 'state',
valueField : 'abbr',
typeAhead : true,
mode : 'local',
allowBlank : false,
blankText : "请选择",
readOnly : true,
triggerAction : 'all',
emptyText : "-未选择-",
selectOnFocus : true
});
calcSelect.on("select",function (object,record,index){loadDS(object.getValue());
alert("第二次:"+dataSotre.getCount());
});
[/code]
[b]问题补充:[/b]
目前测试的结果是两次alert的结果都是0,单是第二次选择的时候就有值了,并且无乱第几次点,查询结果后台都返回了,打印的hibernate语句也是正确的
[b]问题补充:[/b]
加入lazyInit:false,lazyRender:false
后问题依旧,我觉得问题不是出在combobox身上,而是dataStore的加载机制,导致首次加载延迟,但是同样的方法,比如说做个二级联动,第二个combobox的sotre使用上面定义的dataStore,选择第一个combo后,第二个combo的值可以正确加载,alert的值也都是正确的,很奇怪
这必然有问题, load是一个异步的过程,你接下来的alert肯定不对.
应该是
dataStore.on('load',function(){
alert("第二次:"+dataStore.getCount());
})
dataStore.load();
1.给出更完整一点的代码
2.试着对combo设置lazyInit:false,lazyRender:false
改写了下,
另外,你的后台必须根据传递的参数的不同来回传不同的json
[code="javascript"]
var monthsStore = new Ext.data.SimpleStore({
fields: ['state', 'abbr'],
data: [
['1月', '1'],
['2月', '2'],
['3月', '3'],
['4月', '4'],
['5月', '5'],
['6月', '6'],
['7月', '7'],
['8月', '8'],
['9月', '9'],
['10月', '10'],
['11月', '11'],
['12月', '12']]
});
var dataStore = new Ext.data.Store({ //这是数据源
proxy: new Ext.data.HttpProxy({
url: 'xx'
}),
reader: new Ext.data.JsonReader({
root: 'result.list',
totalProperty: '',
id: 'id'
},
['id', 'colDesc'])
});
function loadDS(value) {
var baseparams = dataStore.baseParams;
baseparams.method = 'cConditionService.getValueConditions';
baseparams.params = value;
dataStore.baseParams = baseparams;
alert('查询参数:' + Ext.encode(baseparams))
dataStore.on('load', function () {
alert("载入数据:" + dataStore.getCount());
//后台必须根据上面的参数的不同来回传不同的json
})
dataStore.load();
}
var calcSelect = new Ext.form.ComboBox({
fieldLabel: "测试",
store: monthsStore,
displayField: 'state',
valueField: 'abbr',
typeAhead: true,
mode: 'local',
allowBlank: false,
blankText: "请选择",
readOnly: true,
triggerAction: 'all',
emptyText: "-未选择-",
selectOnFocus: true,
renderTo: Ext.getBody()
});
calcSelect.on("select", function (object, record, index) {
loadDS(object.getValue());
});[/code]