找回密码
 立即注册

QQ登录

只需一步,快速开始

Simon.hu 讲师达人认证 悬赏达人认证 活字格认证
超级版主   /  发表于:2020-11-5 16:24  /   查看:3805  /  回复:0
本帖最后由 Simon.hu 于 2020-11-5 18:36 编辑

最近听到了很多的表格内公式互算的问题,今天咱以一个小例子,让大家了解一下怎么玩
先看一下问题描述
页面上有一个表,表格中有3列,分别是【单价】,【数量】,【总价】;我们希望用户任意输入2个值就能自动把第三个值计算出来
这样描述言简意赅,但是细细品味以后,我们需要将这个问题分解,在一点一点考虑,其实问题分解完了以后就是这样的

    新增时:
  • 随便填写2个字段的值,第三个都能自动计算出来

修改时:
  • 修改单价,总价变
  • 修改总价,单价变
  • 修改数量,没效果(这个我专门和几个大佬确定过,确实是这个需求,因为一般数量很少修改,大部分为单价和总价的修改)

清楚了问题以后,我想先给大家看一下期望效果

如下图所示
效果.gif
那么如何来解决问题呢?
活字格自带的功能确实没有,不过这个需要我们的代码来做了,能写代码基本等于万能
废话不多说了,先来一个代码吓吓人
  1. // JavaScript source code
  2. Forguncy.Page.ready(function () {

  3.     var priceColIndex = 1;
  4.     var countColIndex = 2;
  5.     var totalColIndex = 3;
  6.     var isSettingValue = false;

  7.     var listview = Forguncy.Page.getListView("表格1");

  8.     var updateCount = function (row) {
  9.         var total = listview.getValue(row, totalColIndex);
  10.         if (total == null) {
  11.             return false
  12.         }
  13.         var price = listview.getValue(row, priceColIndex);
  14.         if (price == null) {
  15.             return false
  16.         }

  17.         var count = total / price;
  18.         isSettingValue = true;
  19.         listview.setValue(row, countColIndex, count);
  20.         isSettingValue = false;

  21.         return true;
  22.     }

  23.     var updatePrice = function (row) {
  24.         var total = listview.getValue(row, totalColIndex);
  25.         if (total == null) {
  26.             return false
  27.         }
  28.         var count = listview.getValue(row, countColIndex);
  29.         if (count == null) {
  30.             return false
  31.         }

  32.         var price = total / count;
  33.         isSettingValue = true;
  34.         listview.setValue(row, priceColIndex, price);
  35.         isSettingValue = false;

  36.         return true;
  37.     }

  38.     var updateTotal = function (row) {
  39.         var price = listview.getValue(row, priceColIndex);
  40.         if (price == null) {
  41.             return false
  42.         }
  43.         var count = listview.getValue(row, countColIndex);
  44.         if (count == null) {
  45.             return false
  46.         }

  47.         var total = price * count;
  48.         isSettingValue = true;
  49.         listview.setValue(row, totalColIndex, total);
  50.         isSettingValue = false;

  51.         return true;
  52.     }

  53.     listview.bind(Forguncy.ListViewEvents.ValueChanged, function (arg1, arg2) {
  54.         if (isSettingValue) {
  55.             return;
  56.         }

  57.         var row = arg2.CellRanges[0].Row;
  58.         var col = arg2.CellRanges[0].Column;
  59.         if (row >= 0 && col >= 0) {
  60.             if (col === priceColIndex) {
  61.                 if (!updateTotal(row)) {
  62.                     updateCount(row);
  63.                 }
  64.             }
  65.             else if (col === countColIndex) {
  66.                 if (!updateTotal(row)) {
  67.                     updatePrice(row);
  68.                 }
  69.             }
  70.             else if (col === totalColIndex) {
  71.                 if (!updatePrice(row)) {
  72.                     updateCount(row);
  73.                 }
  74.             }
  75.         }
  76.     });
  77. });
复制代码

把这个代码搞到页面的这个位置,就行了
image.png41723021.png
这里是我的工程文件,你可以拿去玩玩
Document.fgcc (55.06 KB, 下载次数: 131)

评分

参与人数 1满意度 +5 收起 理由
ridddx + 5 神贴,就是要这个!未税价与含税价就可以解.

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部