FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 5317|回复: 4

【非BUG,这个问题有意思】Grid里的NumberBox数值有问题!

[复制链接]
发表于 2017-7-4 10:21:15 | 显示全部楼层 |阅读模式
当输入59时,按回车,很正常

当再次输入60时,这时还没按回车


当按回车时,60却变成60.0001了

  1. @(F.Window()
  2.         .Width(350)
  3.         .WindowPosition(WindowPosition.GoldenSection)
  4.         .EnableClose(false)
  5.         .IsModal(false)
  6.         .IsViewPort(true)
  7.         .Title("测试")
  8.         .ID("Window1")
  9.         .Items(
  10.             F.Grid()
  11.                 .Title("BugTest")
  12.                 .ID("BugTest")
  13.                 .Height(500)
  14.                 .Width(500)
  15.                 .Listener("dataload", "onCreatRow")
  16.                 .AllowCellEditing(true)
  17.                 .ClicksToEdit(1)
  18.                 .Columns(
  19.                     F.RenderField()
  20.                         .ColumnID("Price")
  21.                         .HeaderText("单价")
  22.                         .HeaderTextAlign(TextAlign.Center)
  23.                         .Width(90)
  24.                         .Editor(
  25.                             F.NumberBox()
  26.                                 .NoDecimal(false)
  27.                                 .NoNegative(true)
  28.                                 //.EnableRound(true)
  29.                                 .DecimalPrecision(4)
  30.                                 .ShowTrigger(false)
  31.                                 .Required(true)
  32.                         ),
  33.                     F.RenderField()
  34.                         .ColumnID("TaxPrice")
  35.                         .HeaderText("含税单价")
  36.                         .HeaderTextAlign(TextAlign.Center)
  37.                         .Width(90)
  38.                         .Editor(
  39.                             F.NumberBox()
  40.                                 .NoDecimal(false)
  41.                                 .NoNegative(true)
  42.                                 //.EnableRound(true)
  43.                                 .DecimalPrecision(4)
  44.                                 .ShowTrigger(false)
  45.                                 .Required(true)
  46.                         )
  47.                         
  48.                 )
  49.                 .Listener("afteredit", "onCalculate")
  50.         )
  51.     )
复制代码
  1. <script>
  2.         function onCreatRow() {
  3.             var records = [];
  4.             for (var i = 0; i < 10; i++) {
  5.                 records.push({
  6.                     //'': ''
  7.                 });
  8.             }
  9.             // 此过程禁止触发事件,防止 dataload 事件死循环
  10.             F.noEvent(function () {
  11.                 F.ui.BugTest.addNewRecords(records, true);
  12.             });
  13.         }

  14.         function onCalculate(event, value, params) {
  15.             var me = this, columnId = params.columnId, rowId = params.rowId;
  16.             var Price = me.getCellValue(rowId, 'Price');
  17.             var TaxPrice = me.getCellValue(rowId, 'TaxPrice');
  18.             if (columnId === 'Price') {
  19.                 TaxPrice = Price * (1 + 10 / 100);
  20.                 me.updateCellValue(rowId, 'TaxPrice', TaxPrice.toFixed(4));
  21.             }
  22.             if (columnId === 'TaxPrice') {
  23.                 Price = TaxPrice / (1 + 10 / 100);
  24.                 me.updateCellValue(rowId, 'Price', Price.toFixed(4));
  25.             }
  26.         }

  27.     </script>
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
 楼主| 发表于 2017-7-4 10:30:42 | 显示全部楼层
后来我在Js中加了标志
  1. function onCalculate(event, value, params) {
  2.             var me = this, columnId = params.columnId, rowId = params.rowId;
  3.             var Price = me.getCellValue(rowId, 'Price');
  4.             var TaxPrice = me.getCellValue(rowId, 'TaxPrice');
  5.             if (columnId === 'Price') {
  6.                 TaxPrice = Price * (1 + 10 / 100);
  7.                 me.updateCellValue(rowId, 'TaxPrice', TaxPrice.toFixed(4));
  8.                 console.log("This is Price");
  9.             }
  10.             if (columnId === 'TaxPrice') {
  11.                 Price = TaxPrice / (1 + 10 / 100);
  12.                 me.updateCellValue(rowId, 'Price', Price.toFixed(4));
  13.                 console.log("This is TaxPrice");
  14.             }
  15.         }
复制代码

却发现afteredit,这个事件,会触发三次

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
发表于 2017-7-4 10:32:02 | 显示全部楼层
请上传完整空项目工程,或者发到:2877408506@qq.com
发表于 2017-7-4 11:11:41 | 显示全部楼层

首先确认一点,这个不是 FineUIMvc 的BUG,而是自己注册的 .Listener("afteredit", "onCalculate") 中处理的问题。

这个地方有点意思,我们来分析一下:
1. 在【含税单价】中输入数字 60,然后失去焦点,会触发  afteredit 事件:


2. 在 afteredit 中,判断 columnId == TaxPrice,会调用 updateCellValue 更新【单价】单元格的值:


3. 这是问题来了,调用updateCellValue 时同样会触发 afteredit 事件,此时 columnId == Price,此时计算的结果是 6.0001:


4. 由于【含税单价】值从 60 改为了 60.0001,所以会再次触发 afteredit 事件,只不过此时计算的【单价】和之前相同,所以不会再次触发 afteredit 事件了。

在整个过程中,修改一次 【含税单价】,其实 afteredit 事件触发了 3 次!!这应该是之前没想到的吧。如果每次改变的值都不同,这将会是一个死循环。





解决办法也很简单,用 F.noEvent 将对 updateCellValue 的调用包含起来:
  1. function onCalculate(event, value, params) {
  2.             var me = this, columnId = params.columnId, rowId = params.rowId;
  3.             var Price = me.getCellValue(rowId, 'Price');
  4.             var TaxPrice = me.getCellValue(rowId, 'TaxPrice');
  5.             if (columnId === 'Price') {
  6.                 TaxPrice = Price * (1 + 10 / 100);
  7.                 F.noEvent(function () {
  8.                     me.updateCellValue(rowId, 'TaxPrice', TaxPrice.toFixed(4));
  9.                     console.log("This is Price");
  10.                 });
  11.             }
  12.             if (columnId === 'TaxPrice') {
  13.                 Price = TaxPrice / (1 + 10 / 100);
  14.                 F.noEvent(function () {
  15.                     me.updateCellValue(rowId, 'Price', Price.toFixed(4));
  16.                     console.log("This is TaxPrice");
  17.                 });
  18.             }
  19.         }
复制代码


其实,官网示例已经有现成了例子:http://fineui.com/demo_mvc#/demo ... /UpdateCellValueTax

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
 楼主| 发表于 2017-7-4 11:16:26 | 显示全部楼层
原来如此,感谢30大大的帮助~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-4-18 22:57 , Processed in 0.051819 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表