ExtJS:如何防止 GridPanel 中的数字列总和超过给定的最大值?

发布于 2024-09-16 13:42:40 字数 680 浏览 3 评论 0原文

我有一个只有两列的 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 技术交流群。

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

发布评论

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

评论(2

赢得她心 2024-09-23 13:42:40

在将已更改元素保存到数据库之前,您需要提取已更改元素的脏值。事后验证干净值是没有意义的。

有关 ExtJS 中数据存储的信息,请参阅此处:

http://www.fusioncube.net/index.php/managing-dirty-rows-in-a-sencha-extjs-editorgridpanel

您可以使用诸如 field.originalValuefield.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 and field.getRawValue() to get at the data store and see the difference.

行至春深 2024-09-23 13:42:40

我并没有完全使用 js1568 的答案,因为我想在输入错误值时阻止它们;甚至在 AfterEdit 事件触发并且该行被标记为脏之前,并且在写入数据库之前很久。但是该链接确实通过使用网格的选择模型启发了我。所以,我的快速小解决方案:

因为 NumberField 不会将任何上下文传递给自定义验证器(除了当前值),所以使用 field.originalValue 会很棒,但是嘿,生活很艰难。

但由于编辑一行首先需要选择该行(并且通过该网格的 RowSelectionModel 设置为 SingleSelect 来强化),我知道正在编辑的行必须是唯一的元素在grid.selModel.selections.items[]中。从那里开始,只需

var newTotal = TotalCommissionPercentagesInGrid() – oldValue + newValue;

oldValue 设为 grid.selModel.selections.items[0].data.CommissionPercentage 并将 newValue 设为从 NumberField 传递到验证器函数的值。

return ( newTotal <= percentageAvailable ) ? true : "No dice.";

很不错。

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 in grid.selModel.selections.items[]. From there, it’s just a matter of

var newTotal = TotalCommissionPercentagesInGrid() – oldValue + newValue;

With oldValue being grid.selModel.selections.items[0].data.CommissionPercentage and newValue being the value passed to the validator function from the NumberField.

return ( newTotal <= percentageAvailable ) ? true : "No dice.";

Very nice.

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