【问题标题】:Calculating the sum of diffrent columns in TableView计算 TableView 中不同列的总和
【发布时间】:2016-06-03 04:31:57
【问题描述】:

我有一个桌子女巫,如下表所示

TableVeiw<Transaction>

 ---------------------------------------------------------------------
| id | Transaction date |  Name | type  | Debit Amount | Credit Amount|
|---------------------------------------------------------------------|
| 1  |   21/02/2016     |Invoice|Credit |              |      12000   |
|---------------------------------------------------------------------|
| 2  |    21/02/2016    |Payment|Debit  |  20000       |              |
|---------------------------------------------------------------------|
                                        |  Total Debit | Total Credit | 
                                         -----------------------------

借方金额和贷方金额中的数据来自交易对象的一个​​属性,如何填充这些列的代码片段如下:

 tcCreditAmmout.setCellValueFactory(cellData -> {
            Transaction transaction = cellData.getValue() ;
            BigDecimal value = null;
            if(transaction.getKindOfTransaction() == KindOfTransaction.CREDIT){
                value = transaction.getAmountOfTransaction();
            }

            return new ReadOnlyObjectWrapper<BigDecimal>(value);
        });

        tcDebitAmmout.setCellValueFactory(cellData -> {
            Transaction transaction = cellData.getValue() ;
            BigDecimal value = null;
            if(transaction.getKindOfTransaction() == KindOfTransaction.DEBIT){
                value = transaction.getAmountOfTransaction();
            }

            return new ReadOnlyObjectWrapper<BigDecimal>(value);
        });

每次通过 Javafx 绑定更改 TableView 项目时,我都需要计算:总借方(见上表)和总贷方(见上表)的总和,但我不知道如何实现这一点。

注意:总借方和总贷方是标签,

【问题讨论】:

  • 这与stackoverflow.com/questions/35187145/… 的第二部分有何不同?你似乎在反复问同一个问题。
  • 不,它是不同的,transaction.getAmountOfTransaction() 是根据交易类型分成列的。我需要计算每列 tcCreditAmmout 和 tcDebitAmmout 的总和
  • 但是对于所有分别具有transaction.getKindOfTransaction() == KindOfTransaction.CREDITtransaction.getKindOfTransaction() == KindOfTransaction.DEBIT 的事务来说,这些只是transaction.getAmountOfTransaction() 的总和,不是吗?
  • 是的,但是我需要将它们的交易总和与借记类型分开。
  • 不太确定我明白为什么会有如此不同,但请看答案。

标签: tableview javafx-8 observable tablecolumn


【解决方案1】:

假设你有

TableView<Transaction> table = ... ;
Label totalDebit = ... ;
Label totalCredit = ... ;

那么你只需要:

totalDebit.textProperty().bind(Bindings.createObjectBinding(() -> 
    table.getItems().stream()
         .filter(transaction -> transaction.getKindOfTransaction() == KindOfTransaction.DEBIT)
         .map(Transaction::getAmountOfTransaction)
         .reduce(BigDecimal.ZERO, BigDecimal::add),
         table.getItems())
    .asString("%.3f"));

当然

totalCredit.textProperty().bind(Bindings.createObjectBinding(() -> 
    table.getItems().stream()
         .filter(transaction -> transaction.getKindOfTransaction() == KindOfTransaction.CREDIT)
         .map(Transaction::getAmountOfTransaction)
         .reduce(BigDecimal.ZERO, BigDecimal::add),
         table.getItems())
    .asString("%.3f"));

如果getAmountOfTransaction 可能在事务是表的一部分时发生变化,那么您表的项目列表必须使用extractor 构造。

【讨论】:

  • 我认为,您的代码中有一个错字:您必须使用 Bindings.createObjectBinding() 而不是 Bindings.computeObjectBinding()
  • 是的,抱歉,不知道那个错字是怎么发生的。编辑修复。
  • 对于:table.getItems()) .asString("%.3f")); ,如何自定义模式?
  • 只需阅读文档:asString() 其中引用了java.util.Formatter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多