extjs5 store.load()之后无法获取数据问题

本文介绍在ExtJS框架中解决异步加载数据时的问题,通过调整代码逻辑,在数据加载完成后正确执行后续操作,确保应用程序正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:页面打开的时候,主视图展示所有,辅视图不显示,在增加搜索条件之后,辅视图需要默认展示主视图第一条记录.简略页面如下


页面是用extjs5搭建的,我查了下百度,extjs store.load()是异步请求,所以导致接下来的渲染失败,失败的代码如下:

search_order_txnNo = orderTxnNo.getValue();
        search_detail_order_id = "";
        search_order_payment_id = "";
        orderPaymentDetailStore.clearData();
        orderPaymentDetailStore.removeAll();
        orderPaymentDetailStore.load();
        var count = orderPaymentDetailGrid.getStore().getCount();
        if(count > 0){
            var record = orderPaymentGrid.getStore().getAt(1);
            search_order_id = record.data.orderId;
            alert("orderId: "+search_order_id);
        }
        orderPaymentStore.load();
查了下百度之后,需要在load()的回调函数中实现自己的代码,才能执行操作

改造后:

search_order_txnNo = orderTxnNo.getValue();
        search_detail_order_id = "";
        search_order_payment_id = "";
        orderPaymentDetailStore.clearData();
        orderPaymentDetailStore.removeAll();
        //数据源加载,因为store是异步的,如果需要使用里面的数据,必须得在callback的回调函数里实现
        orderPaymentDetailStore.load({
            callback : function (records , options, success) {
                //获取当前数据源的数据量
                var count = orderPaymentDetailGrid.getStore().getCount();
                alert("加载回调事件:"+count);
                if(count > 0){
                    //var record = records.getSelectionModel().select(0,true);
                    //上面注释的代码无法有效执行,请忽略
                    var record = orderPaymentGrid.getStore().getAt(1);
                    search_order_id = record.data.orderId;
                    alert("orderId: "+search_order_id);
                }
                orderPaymentStore.load();
            }
        });

同时附加一段官方说明:

store.load({
    params: {start:0,limit:20},
    callback: function(records, options, success){
        Ext.Msg.alert('info', '加载完毕');
    },
    scope: store,
    add: true
});
1. params是在store加载时发送的附加参数。
2. callback是加载完毕时执行的回调函数,它包含3个参数:records参数表示获得的数据,options表示执行load()时传递的参数,success表示是否加载成功。
3. Scope用来指定回调函数执行时的作用域。
4. Add为true时,load()得到的数据会添加在原来的store数据的末尾,否则会先清除之前的数据,再将得到的数据添加到store中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值