javascript 将商店加载到 ExtJS 4 中的组合框时如何发送额外参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17763154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:31:24  来源:igfitidea点击:

How to send extra parameters when loading a store to a combobox in ExtJS 4?

javascriptextjsextjs4extjs4.1extjs4.2

提问by Daniele Testa

How do I send extra parameters when using a store for a combobox in ExtJS 4?

在 ExtJS 4 中为组合框使用商店时如何发送额外参数?

I know that I can use "extraParams" in the proxy-settings, but that will affect ALL elements that is using the same store.

我知道我可以在代理设置中使用“extraParams”,但这会影响使用同一存储的所有元素。

I.e if I have a Grid that is using a store called "Users" that will list all users in a system. At the same time, I have a combobox, that also uses the store "Users", but this time I want to list all Users that has "status=2", thus I want to send the param "&status=2" on the Ajax call to the back-end.

即,如果我有一个使用名为“用户”的商店的网格,它将列出系统中的所有用户。同时,我有一个组合框,它也使用商店“Users”,但这次我想列出所有具有“status=2”的用户,因此我想在Ajax 调用后端。

If I use something like:

如果我使用类似的东西:

store.getProxy().extraParams = {
  status: 2
};

It will work, but the Grid will at the same time be updated to also use "&status=2". I ONLY want the combobox to use the param.

它会工作,但网格将同时更新为也使用“&status=2”。我只希望组合框使用参数。

I guess I could turn off "autoupdate" on the Grid, then set the "extraParams" on the "render" event on the combobox and then unset it when the combobox gets hidden, but what would be a very ugly solution.

我想我可以关闭网格上的“自动更新”,然后在组合框的“渲染”事件上设置“extraParams”,然后在组合框隐藏时取消设置它,但这将是一个非常丑陋的解决方案。

I can also duplicate the store to a "Users2" and use that one for the combobox, but that is a ugly solution as well.

我也可以将商店复制到“Users2”并将其用于组合框,但这也是一个丑陋的解决方案。

What is the correct way to do this? It must be a very common thing for most people.

这样做的正确方法是什么?对于大多数人来说,这应该是一件很平常的事情。

UPDATE 1:

更新 1:

I know I can use something like:

我知道我可以使用类似的东西:

store.load({
  params:{
    'foo1': bar1,
    'foo2': bar2
  } 
});

But how would I use that in ExtJS 4 MVC? There I just specify "store: Users" in the form-object. How would I send those extra parameters to the .load() function?

但是我将如何在 ExtJS 4 MVC 中使用它?在那里我只是在表单对象中指定“商店:用户”。我如何将这些额外的参数发送到 .load() 函数?

I have tried the following:

我尝试了以下方法:

{
xtype: 'combobox',
fieldLabel: 'Server',
name: 'server',

//store: 'ServersIP',
store: new Cloud.store.ServersIP(),

/*
listeners: {
    beforeload: function(store, options) {
        console.log(store);
    }
},
*/

valueField: 'id',
displayField: 'name_id',
emptyText: 'Select a Server...',
editable: false
},

However, it gives error "Uncaught TypeError: Cannot read property 'ServersIP' of undefined"

但是,它给出了错误“未捕获的类型错误:无法读取未定义的属性 'ServersIP'”

But the name is correct:

但名字是正确的:

Ext.define('Cloud.store.ServersIP', {

extend: 'Ext.data.Store',
model: 'Cloud.model.Server', 

Also, where exactly would I put the listener? I assume I did not get that one correct in my example either?

另外,我到底要把听众放在哪里?我想我在我的例子中也没有得到正确的答案?

UPDATE 2:

更新 2:

I've got a little bit further to a solution now:

我现在对解决方案有了进一步的了解:

store: Ext.create('Cloud.store.ServersIP', {
proxy: {
    extraParams: {
        param1: 'value1',
        param2: 'value2'
    }
},                          
}),

The above does NOT work. It works if I only have a "one-level" variable in the params to Ext.Create. For some reason, it does not like it when I pass in proxy => extraParams => param1.

以上不起作用。如果我在 Ext.Create 的参数中只有一个“一级”变量,它就可以工作。出于某种原因,当我传入代理 => extraParams => param1 时,它不喜欢它。

This one works:

这个有效:

store: Ext.create('Cloud.store.ServersIP', {
aaa: 'bbb'
}),

But then of course, my extraParams are not there. Anyone know how to fix this last part?

但是当然,我的 extraParams 不在那里。有谁知道如何修复这最后一部分?

采纳答案by Daniele Testa

This is the best code I could come up with. No listener is needed :)

这是我能想到的最好的代码。不需要听众:)

Create a new instance of the "users"-store and change the params in that one.

创建“用户”存储的新实例并更改该实例中的参数。

xtype: 'combobox',
fieldLabel: 'Users',
name: 'users',

store: (function() {
  var s = Ext.create('MyApp.store.Users');
  s.proxy.extraParams = {
    active: '1'
  }
  return s;
})(),

回答by Rio Adetya Rizky

I just solved this problem. combobox queryMode:remote with extra parameters.

我刚刚解决了这个问题。组合框查询模式:带有额外参数的远程。

just instantiate the store in "VIEW"inside initComponentand override the proxy

只需在initComponent内的“VIEW”中实例化商店并覆盖代理

//instantiate the store

var store = Ext.create('Cloud.store.ServersIP', {
    storeId: 'YourStoreId'
});
store.proxy.extraParams = { param1: 'value1', param2: 'value2' };

and in xtype:combobox

并在 xtype:combobox

//set combobox store

{
    xtype: 'combobox',
    queryMode:'remote',
    store : Ext.data.StoreManager.lookup('YourStoreId'),
    ...
    ...
}

I hope you understand my proposed solution.

我希望你理解我提出的解决方案。

回答by TheZver

You can catch the 'beforeload' store event and alter the current load-operation. This way the parameters won't persist to other requests.

您可以捕获“beforeload”存储事件并更改当前的加载操作。这样参数就不会持续到其他请求。

Ext.create('Ext.data.Store', {
    model: MyModel,
    autoLoad: true,
    listeners:{
        // Fires before a request is made. op is an Ext.data.Operation object
        beforeload:function(store,op){ 
            // Set request parameters (without overriding other parameters)
            op.params = Ext.apply(op.params||{},{
                someParam:true
            });
        }
    }
});

回答by dbrin

Instead of referencing the same store as the grid you can create a new instance of the same store. This way the two store instances are managed separately.

您可以创建同一商店的新实例,而不是引用与网格相同的商店。这样两个存储实例是分开管理的。

So in your form replace store: 'Users'with store: new MyApp.store.Users()and then you can use whichever method works better for you (extraparams, load with params, beforeload listener, etc)

因此,在您的表单中替换store: 'Users'store: new MyApp.store.Users(),然后您可以使用更适合您的方法(额外参数、加载参数、加载前侦听器等)

回答by rixo

You can set autoLoadto false on your store, and then call the loadmethod yourself, and use the paramsoption:

您可以autoLoad在您的商店上设置为 false,然后load自己调用该方法,并使用该params选项:

var store = Ext.create('My.Store', {
    autoLoad: false
    ...
});

store.load({
    params: {status: 2}
});

That will work if you need your combo to load only once and then query in local mode. If you need your combo to make multiple calls to the server (remote mode), set your params in the beforeloadevent of the store:

如果您需要组合只加载一次,然后在本地模式下查询,这将起作用。如果你需要你的组合来多次调用服务器(远程模式),beforeload在商店的事件中设置你的参数:

comboStore.on('beforeload', function(store, operation) {
    operation.params = operation.params || {};
    operation.params.status = 2;
});