ExtJS 4:网格:如何选择新创建的记录?
我的问题很简单,如标题所示!
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如@Tom 所说...
使用网格视图来选择您的新记录,尽管
您仍然可以通过选择模型选择新记录...
从文档中...选择方法需要传递 3 个参数..
所以在你的情况下:
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 :
这是我发现的:当没有“id”字段时,ExtJS 似乎会丢失。
因此,在调用
sync()
后,如果您没有精确输入“id”字段,它会重新组织网格,并且找不到“旧”记录(我记得的就是在通话之前)。这只是我的猜测,我可能是错的。
无论如何,我已经做出了转变:为了保持“同质”,我将创建
xxxCreate()
和xxxCreateValidate()
,对于xxxUpdate 也是如此()
和xxxUpdateValidate()
。我已经为示例完成了它们: user =>userCreate()
、userCreateValidate()
、userUpdate()
和userUpdateValidate()
:我希望我的代码能有所帮助有人...现在我正在寻找一种处理网格中的键的方法。就像 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()
, andxxxCreateValidate()
, same forxxxUpdate()
andxxxUpdateValidate()
. I've done them for the example: user =>userCreate()
,userCreateValidate()
,userUpdate()
anduserUpdateValidate()
: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).
我认为这里的问题是您尝试在调用 store.sync(); 后立即选择记录,但这实际上是异步请求,在从服务器获取数据后更新存储。
从服务器更新您的商店后,尝试调用
grid.getSelectionModel().select
。这种方法的问题是store.sync
不支持任何回调方法,但您应该能够在商店上使用datachanged
事件,该事件应该在商店之后触发同步全部完成。尝试这样的事情(基于您提供的有限代码示例):
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 thatstore.sync
doesn't support any callback method, but you should be able to usedatachanged
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):
网格行选择通常在 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
当
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 isstore.sync();
throwing exception. Check out the console output for errors.我不知道是否有可能直接选择此记录,因为在同步之前,该记录是存储中的虚拟记录,但同步后就不是了。另外,您可能会在服务器端为其分配一个 id(?),这会更改记录,并且在sync()之后它不会相同...
如果记录中的某些字段是唯一的(例如名称或描述或任何内容),您可以在同步后使用它来查找此记录并选择它:
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:
你可以简单地使用这样的:
you can simply use like this: