ExtJS 4:网格:如何选择新创建的记录?

发布于 2024-11-27 17:36:15 字数 715 浏览 2 评论 0原文

我的问题很简单,如标题所示!

这是我的代码:

userCreate: function(button) {
    var grid = button.up('panel'),
        store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];
    store.sync();
    /* select the newly created record */
    grid.getSelectionModel().select(inst.id);

    Ext.create('GS.view.user.Edit', { create:true }); 
},  

现在我只想让后面的行起作用

    /* select the newly created record */

我尝试了很多方法,但都不起作用:

    grid.getSelectionModel().select(inst.id);
    grid.getSelectionModel().select(inst);
    grid.getSelectionModel().select(store.last);

有什么想法吗?

非常感谢

My question is so simple it's in the title!

Here's my code:

userCreate: function(button) {
    var grid = button.up('panel'),
        store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];
    store.sync();
    /* select the newly created record */
    grid.getSelectionModel().select(inst.id);

    Ext.create('GS.view.user.Edit', { create:true }); 
},  

Now I just want the line after the

    /* select the newly created record */

to work!

I've tried many things but none of them work:

    grid.getSelectionModel().select(inst.id);
    grid.getSelectionModel().select(inst);
    grid.getSelectionModel().select(store.last);

Any idea?

Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

手长情犹 2024-12-04 17:36:15

正如@Tom 所说...
使用网格视图来选择您的新记录,尽管
您仍然可以通过选择模型选择新记录...

从文档中...选择方法需要传递 3 个参数..
所以在你的情况下:

userCreate: function(button) {
    var grid = button.up('panel');
    store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];
    store.sync();

    /* select the newly created record via model*/
    grid.getSelectionModel().select(inst, true, true);

    /* select the newly created record via view*/
    grid.getView().select(inst, true, true);

    Ext.create('GS.view.user.Edit', { create:true }); 
},

like what @Tom said...
use grid view to select your new record, althougt
you can still select your new record via selection model...

from docs.. select method need 3 parameter to pass..
so in your case :

userCreate: function(button) {
    var grid = button.up('panel');
    store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];
    store.sync();

    /* select the newly created record via model*/
    grid.getSelectionModel().select(inst, true, true);

    /* select the newly created record via view*/
    grid.getView().select(inst, true, true);

    Ext.create('GS.view.user.Edit', { create:true }); 
},
谢绝鈎搭 2024-12-04 17:36:15

这是我发现的:当没有“id”字段时,ExtJS 似乎会丢失。
因此,在调用 sync() 后,如果您没有精确输入“id”字段,它会重新组织网格,并且找不到“旧”记录(我记得的就是在通话之前)。
这只是我的猜测,我可能是错的。

无论如何,我已经做出了转变:为了保持“同质”,我将创建 xxxCreate()xxxCreateValidate(),对于 xxxUpdate 也是如此()xxxUpdateValidate()。我已经为示例完成了它们: user => userCreate()userCreateValidate()userUpdate()userUpdateValidate()

init: function () {
    this.control({
        /* (!) Actions in 'userlist' */
        'userlist': {
            itemdblclick: this.userEdit
        },  
        'userlist button[action=create]': {
            click: this.userCreate
        },  
        'userlist button[action=delete]': {
            click: this.userDelete
        },  
        /* (!) Actions in 'useredit' */
        'useredit button[action=create]': {
            click: this.userCreateValidate
        },  
        'useredit button[action=save]': {
            click: this.userEditValidate
        }   
    }); 
},  

userCreate: function(button) {
    /* Using Ext.create() to pass variable create:true
     * instead of the shortcut:
     * var view = Ext.widget('useredit');
     */
    var view = Ext.create('GS.view.user.Edit', {
        create:true
    }); 
},  

userCreateValidate: function(button) {
    var win    = button.up('window'),
        form   = win.down('form'),
        values = form.getValues();

    this.getUsersStore().add(values);
    this.getUsersStore().sync();
    win.close();
},  

userEdit: function(grid, record) {
    var view = Ext.widget('useredit');
    view.down('form').loadRecord(record);
},  

userEditValidate: function (button) {
    var win    = button.up('window'),
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();

    record.set(values);
    win.close();
    this.getUsersStore().sync();
},

我希望我的代码能有所帮助有人...现在我正在寻找一种处理网格中的键的方法。就像 ExtJS 的一切一样:你不用花时间编码,因为一切都已经完成了,你花时间搜索...实际上更令人沮丧 =)

无论如何,最需要注意的是新添加的记录被选中。这只是继续开发的一种解决方法,但这不是我想要的(这并不完全是我在问题中要求的)。

Here's what I found: ExtJS seems to be lost when there's no "id" field.
So after a call to sync() if you didn't precise an "id" field, it kindof re-organize the grid and can't find the "old" record (the one I remembered just before the call).
This is just what I guess, I may be wrong.

Anyway, I've made a turnaround: to stay "homogeneonous", I'm going to make xxxCreate(), and xxxCreateValidate(), same for xxxUpdate() and xxxUpdateValidate(). I've done them for the example: user => userCreate(), userCreateValidate(), userUpdate() and userUpdateValidate():

init: function () {
    this.control({
        /* (!) Actions in 'userlist' */
        'userlist': {
            itemdblclick: this.userEdit
        },  
        'userlist button[action=create]': {
            click: this.userCreate
        },  
        'userlist button[action=delete]': {
            click: this.userDelete
        },  
        /* (!) Actions in 'useredit' */
        'useredit button[action=create]': {
            click: this.userCreateValidate
        },  
        'useredit button[action=save]': {
            click: this.userEditValidate
        }   
    }); 
},  

userCreate: function(button) {
    /* Using Ext.create() to pass variable create:true
     * instead of the shortcut:
     * var view = Ext.widget('useredit');
     */
    var view = Ext.create('GS.view.user.Edit', {
        create:true
    }); 
},  

userCreateValidate: function(button) {
    var win    = button.up('window'),
        form   = win.down('form'),
        values = form.getValues();

    this.getUsersStore().add(values);
    this.getUsersStore().sync();
    win.close();
},  

userEdit: function(grid, record) {
    var view = Ext.widget('useredit');
    view.down('form').loadRecord(record);
},  

userEditValidate: function (button) {
    var win    = button.up('window'),
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();

    record.set(values);
    win.close();
    this.getUsersStore().sync();
},

I hope my code will help someone... now I'm looking for a way to handle keys in the Grid. And like everything with ExtJS: you don't spend your time coding, because everything is already done, you spend your time searching... it's more frustrating actually =)

Anyway, the most important thing to note is that the newly added record is not selected. It's just a workaround to continue to develop, but it's not what I wanted (it's not exactly what I'm asking for in the question).

最好是你 2024-12-04 17:36:15

我认为这里的问题是您尝试在调用 store.sync(); 后立即选择记录,但这实际上是异步请求,在从服务器获取数据后更新存储。

从服务器更新您的商店后,尝试调用grid.getSelectionModel().select。这种方法的问题是 store.sync 不支持任何回调方法,但您应该能够在商店上使用 datachanged 事件,该事件应该在商店之后触发同步全部完成。

尝试这样的事情(基于您提供的有限代码示例):

userCreate: function(button) {
    var grid = button.up('panel'),
        store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];

    store.on('datachanged', function() {
        /* select the newly created record, keep existing selection and
           don't suppress events because grid view is updating upon this event
        */
        grid.getSelectionModel().select(inst, true, false);
        Ext.create('GS.view.user.Edit', { create:true }); 
    });

    store.sync();
},

I think that problem here is that you try to select record right after you call store.sync(); but this is actually asynchronous request that updates store after it gets data from server.

Try calling grid.getSelectionModel().select after your store is updated from server. Problem with this approach is that store.sync doesn't support any callback method, but you should be able to use datachanged event on your store, that should be fired after store synchronization is all done.

Try something like this (based on limited code sample you provided):

userCreate: function(button) {
    var grid = button.up('panel'),
        store = grid.getStore();

    /* Create new empty record */
    var inst = store.add({})[0];

    store.on('datachanged', function() {
        /* select the newly created record, keep existing selection and
           don't suppress events because grid view is updating upon this event
        */
        grid.getSelectionModel().select(inst, true, false);
        Ext.create('GS.view.user.Edit', { create:true }); 
    });

    store.sync();
},
鱼忆七猫命九 2024-12-04 17:36:15

网格行选择通常在 View 对象中处理(在 ExtJS 3 中是 GridView)。所以我想如果你将上面的代码更新为:

grid.getView().select(0);

这将选择索引 0 处的行,该行看起来像您要添加新记录的位置。然后,View 对象将通知选择模型并更新浏览器视图。您可以通过 API 文档找到其他选择选项,网址为

Grid row selection is usually handled in the View object (in ExtJS 3 it was GridView). So I think if you update your code above to look like:

grid.getView().select(0);

This will select the row at index 0 which looks like where you're adding the new record. The View object will then notify the selection model and update the browser view. There are other selection options which you can find via the API docs at http://docs.sencha.com/ext-js/4-0/#/api/Ext.grid.View-method-select

风吹短裙飘 2024-12-04 17:36:15

grid.getSelectionModel().select 时,您的示例工作正常 (inst); 正在使用。因此,唯一可能导致这种行为的就是 store.sync(); 抛出异常。检查控制台输出是否有错误。

Your example works fine when grid.getSelectionModel().select(inst); is being used. So the only thing that can cause such behavior is store.sync(); throwing exception. Check out the console output for errors.

半葬歌 2024-12-04 17:36:15

我不知道是否有可能直接选择此记录,因为在同步之前,该记录是存储中的虚拟记录,但同步后就不是了。另外,您可能会在服务器端为其分配一个 id(?),这会更改记录,并且在sync()之后它不会相同...

如果记录中的某些字段是唯一的(例如名称或描述或任何内容),您可以在同步后使用它来查找此记录并选择它:

var value = store.getAt(0).get('name');           // getting the value of a unique field e.g. "name" from the newly added record

store.sync();

var rec = store.findRecord("name", value);   // find the record with the unique value "name" after sync
grid.getSelectionModel().select(rec);       // select that record

I dont know of any possibility to directly select this record because before sync, that records is a phantom record in the store but not afterwards. Also, you might be assigning an id to it on the server side(?) which would change the record and it would'nt be the same after sync()...

If some field in the record is to be unique (e.g. name or description or anything), you might use that to find this record after sync and select it:

var value = store.getAt(0).get('name');           // getting the value of a unique field e.g. "name" from the newly added record

store.sync();

var rec = store.findRecord("name", value);   // find the record with the unique value "name" after sync
grid.getSelectionModel().select(rec);       // select that record
缘字诀 2024-12-04 17:36:15

你可以简单地使用这样的:

grid.getSelectionModel().select(store.data.length-1)

you can simply use like this:

grid.getSelectionModel().select(store.data.length-1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文