Extjs 4创建工具栏项目宽度ajax请求
我必须创建一个工具栏,根据 ajax 请求计算其项目。看看函数 initComponent 和 buildItems,我没有得到相同的 Toolbar.items 元素结果,甚至 Ajax 请求的运行也如您在调用 console.log 后在 builsItems 方法中看到的那样正确完成。 请帮助(我是初学者,我正在使用 Extjs 4)非常感谢。
Ext.define('Dev.view.layout.Toolbarapp',{
extend:'Ext.toolbar.Toolbar',
alias :'widget.toolbarapp',
border:false,
height:35,
initComponent:function(){
this.items=[];
Ext.Ajax.disableCaching=false;
Ext.Ajax.request({
url : '/gdev/jsontest',
callback:this.buildItems,
scope:this
});
console.log(this.items); //THE RESULT IS []
this.callParent(arguments);
},
buildItems:function(options, sucess, response){
if (sucess==true) {
var listItems=[];
var applist=Ext.JSON.decode(response.responseText);
Ext.each(applist, function(rec){
listItems.push({'text' :rec.appid,
'iconCls' :rec.iconcls,
'operation':rec.appcode
}
);
});
this.items=listItems;
console.log(this.items); //THE RESULT IS [Object { text=100,
iconCls="appsysadmin", operation="appsysadmin"}, Object { text=101,
iconCls="appmailxe", operation="appmailxe"}]
}
else {
Ext.MessageBox.alert('Error','not found');
}
}
});
I have to create a toolbar that calculates its items from an ajax request. look at both functions initComponent and buildItems, I don't get the same result of toolbar.items elements, even the run of ajax request done correctly as you see in the builsItems method after calling console.log.
please Help (I am beginner, I am using Extjs 4) Thanks a lot.
Ext.define('Dev.view.layout.Toolbarapp',{
extend:'Ext.toolbar.Toolbar',
alias :'widget.toolbarapp',
border:false,
height:35,
initComponent:function(){
this.items=[];
Ext.Ajax.disableCaching=false;
Ext.Ajax.request({
url : '/gdev/jsontest',
callback:this.buildItems,
scope:this
});
console.log(this.items); //THE RESULT IS []
this.callParent(arguments);
},
buildItems:function(options, sucess, response){
if (sucess==true) {
var listItems=[];
var applist=Ext.JSON.decode(response.responseText);
Ext.each(applist, function(rec){
listItems.push({'text' :rec.appid,
'iconCls' :rec.iconcls,
'operation':rec.appcode
}
);
});
this.items=listItems;
console.log(this.items); //THE RESULT IS [Object { text=100,
iconCls="appsysadmin", operation="appsysadmin"}, Object { text=101,
iconCls="appmailxe", operation="appmailxe"}]
}
else {
Ext.MessageBox.alert('Error','not found');
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将对象表示法组件分配给 this.items 仅在 initComponent 中的 callParent 之前有效。
之后,这些项目已经初始化,ext 将不会接受对其的更改。您应该使用工具栏组件的 添加方法 来代替。
在你的 buildItems 方法中,而不是构建一个数组,只需使用你的对象符号组件调用 add :
我还没有测试过它,但我很确定我就是这样做的(从我的头脑中)
编辑:添加了我的变量范围
Assigning object notation components to this.items only works before the callParent in initComponent.
After that, the items are already initialised, ext won't pick up changes to it. You should use the Toolbar component's add method instead.
In your buildItems method, rather than building an array, simply call add with your object notation component:
I haven't tested it but I'm pretty sure that's how I did it (from top of my head)
Edit: added me variable for scope
为什么不利用组件加载器呢?请参阅: http://dev.sencha .com/deploy/ext-4.0.2a/examples/component-loader/component-loader.html
Why not take advantage of the component loader? See: http://dev.sencha.com/deploy/ext-4.0.2a/examples/component-loader/component-loader.html
您的第一个
console.log
调用位于回调之外,因此它会在 Ajax 请求完成之前被调用。在您的第二个
console.log
调用中,它是在回调中调用的...回调是在 Ajax 请求完成后启动的。基本上,如果您希望使用从 Ajax 请求返回的数据,则需要在回调中完成所有工作。Your first
console.log
call is outside of your callback, and therefore it gets called before the Ajax request ever completes.With your second
console.log
call, it's being called inside the callback... which is initiated after the Ajax request has finished. Basically, you need to do all of your work in your callback if you're expecting to use data returned from the Ajax request.