ExtJS:如何防止 GridPanel 中的数字列总和超过给定的最大值?
我有一个只有两列的 GridPanel:名称和佣金百分比。所有百分比的总和不应超过预定的最大值(假设 25% 的销售额可供争夺)。
Name Commission
Bob 10%
Sally 15%
(Maximum is 25%)
Everything is fine.
我已附加一个自定义验证器(请参阅“validator”)到 NumberField 编辑器,它非常适合单个值的复杂逻辑,并且我可以轻松地总结数据存储中数字列的所有值。但问题在于将两者结合起来;直到成功验证后,新值才会被推送到存储,并且自定义验证器仅给出有问题的当前值,而没有关于涉及哪一行的上下文(我可以用它减去旧值,并且在总数中使用这个新值)。
如何让该验证器以确保新输入的值不会使总和超过最大值的方式工作?
Name Commission
Bob 10%
Sally 16% (Invalid! Total commissions must not exceed 25%)
(Maximum is 25%)
I have a GridPanel with just two columns: names and commission percentages. The sum of all of the percentages should never exceed a predetermined maximum (say that 25% of the sale is up for grabs).
Name Commission
Bob 10%
Sally 15%
(Maximum is 25%)
Everything is fine.
I’ve attached a custom validator (see "validator") to the NumberField editor which works just great for complex logic on individual values, and I can sum up all of the values for the number column in the data store easily. But the problem is combining the two; the new value isn’t pushed to the store until after a successful validation, and the custom validator is only given the current value in question, with no context as to what row is involved (with which I could potentially subtract the old value, and use this new value in the total).
How can I get this validator to work in such a way that it makes sure that a newly entered value doesn’t push the sum over the maximum?
Name Commission
Bob 10%
Sally 16% (Invalid! Total commissions must not exceed 25%)
(Maximum is 25%)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将已更改元素保存到数据库之前,您需要提取已更改元素的脏值。事后验证干净值是没有意义的。
有关 ExtJS 中数据存储的信息,请参阅此处:
http://www.fusioncube.net/index.php/managing-dirty-rows-in-a-sencha-extjs-editorgridpanel
您可以使用诸如
field.originalValue
和field.getRawValue()
获取数据存储并查看差异。You need to pull in the dirty value of the changed element before it is saved to the database. It does not make sense to validate the clean value after the fact.
See here for info on data storage in ExtJS:
http://www.fusioncube.net/index.php/managing-dirty-rows-in-a-sencha-extjs-editorgridpanel
You can use properties such as
field.originalValue
andfield.getRawValue()
to get at the data store and see the difference.我并没有完全使用 js1568 的答案,因为我想在输入错误值时阻止它们;甚至在 AfterEdit 事件触发并且该行被标记为脏之前,并且在写入数据库之前很久。但是该链接确实通过使用网格的选择模型启发了我。所以,我的快速小解决方案:
因为 NumberField 不会将任何上下文传递给自定义验证器(除了当前值),所以使用
field.originalValue
会很棒,但是嘿,生活很艰难。但由于编辑一行首先需要选择该行(并且通过该网格的 RowSelectionModel 设置为
SingleSelect
来强化),我知道正在编辑的行必须是唯一的元素在grid.selModel.selections.items[]
中。从那里开始,只需将
oldValue
设为grid.selModel.selections.items[0].data.CommissionPercentage
并将newValue
设为从 NumberField 传递到验证器函数的值。很不错。
I didn’t quite use js1568’s answer, as I wanted to block bad values as they’re being entered; even before the AfterEdit event fires and the row gets marked dirty, and long before writing to the database. But that link did inspire me through its use of the grid’s selection model. So, my quick little solution:
Because the NumberField doesn’t pass any context to the custom validator (aside from the current value), using
field.originalValue
would have been awesome, but hey, life is hard.But due to the fact that editing a row first requires selection of that row (and reinforced by the fact that this grid’s RowSelectionModel is set to
SingleSelect
), I know that the row being edited must be the only element ingrid.selModel.selections.items[]
. From there, it’s just a matter ofWith
oldValue
beinggrid.selModel.selections.items[0].data.CommissionPercentage
andnewValue
being the value passed to the validator function from the NumberField.Very nice.