猿问

对于其他 2 列中的每个唯一组合值,一列中的 Javascript 页脚回调总和值

我有一份不同日期的公司股息支付报告,其中重复了每次支付的持股价值。


每个独特的 Portfolio.Symbol 可能有多次付款,但每次付款只会重复一个值。


我需要对每个独特的 Portfolio.Symbol 组合的值列中的值求和。


Date        Portfolio  Symbol   Value  Payment.   Sum Value

2020-11-27  DEA        GSK      26000  30.        26000

2020-11-30  ISA        GSK      44000  30.        44000

2021-06-30  ISA        GSK      44000  30.        0, repeat of ISA.GSK so ignore

2021-06-30  DEA        GSK      26000  30.        0, repeat of DEA.GSK so ignore


Unique Sum                      70000   

So in the above example Sum of Value should be Sum of last column 

26000+44000=70000


我已经设置了一个jsfiddle


https://jsfiddle.net/cpshart/s478gvcj/53/


作为使用 datatables.net 页脚回调的起点


如果有人可以帮助我获得解决方案,将不胜感激。


非常感谢


ITMISS
浏览 112回答 1
1回答

当年话下

在您在 jsfiddle 中提供的代码中,您似乎没有添加仅添加唯一数据的条件。然后作为解决方案,您可以尝试使用以下代码:$(document).ready(function() {&nbsp; $('#example').DataTable({&nbsp; &nbsp; "footerCallback": function(row, data, start) {&nbsp; &nbsp; &nbsp; var api = this.api();&nbsp; &nbsp; &nbsp; var intVal = function(i) {&nbsp; &nbsp; &nbsp; &nbsp; return typeof i === 'string' ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i.replace(/[\$,]/g, '') * 1 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; typeof i === 'number' ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i : 0;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; var indexOfPortofolio = 1;&nbsp; &nbsp; &nbsp; var indexOfSymbol = 2;&nbsp; &nbsp; &nbsp; var indexOfValue = 3;&nbsp; &nbsp; &nbsp; // Total over all pages&nbsp; &nbsp; &nbsp; var total = api&nbsp; &nbsp; &nbsp; &nbsp; .rows()&nbsp; &nbsp; &nbsp; &nbsp; .data()&nbsp; &nbsp; &nbsp; &nbsp; .toArray()&nbsp; &nbsp; &nbsp; &nbsp; .reduce(function(a, v, i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var uniquePortofolioSymbol = v[indexOfPortofolio] + v[indexOfSymbol];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a.uniqueList.indexOf(uniquePortofolioSymbol) === -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.sum += intVal(v[indexOfValue]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.uniqueList.push(uniquePortofolioSymbol);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.uniqueIndex.push(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return a;&nbsp; &nbsp; &nbsp; &nbsp; }, { sum: 0, uniqueList: [], uniqueIndex: [] });&nbsp; &nbsp; &nbsp; // Total over this page&nbsp; &nbsp; &nbsp; var pageTotal = api&nbsp; &nbsp; &nbsp; &nbsp; .rows({ page: 'current' })&nbsp; &nbsp; &nbsp; &nbsp; .data()&nbsp; &nbsp; &nbsp; &nbsp; .toArray()&nbsp; &nbsp; &nbsp; &nbsp; .reduce(function(a, v, i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var uniquePortofolioSymbol = v[indexOfPortofolio] + v[indexOfSymbol];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.uniqueList.indexOf(uniquePortofolioSymbol) === -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && total.uniqueIndex.indexOf(start + i) !== -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.sum += intVal(v[indexOfValue]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.uniqueList.push(uniquePortofolioSymbol);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return a;&nbsp; &nbsp; &nbsp; &nbsp; }, { sum: 0, uniqueList: [] });&nbsp; &nbsp; &nbsp; // Update footer&nbsp; &nbsp; &nbsp; $(api.column(3).footer()).html(&nbsp; &nbsp; &nbsp; &nbsp; pageTotal.sum + ' ( ' + total.sum + ' )'&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; });});<link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css"><script src="https://code.jquery.com/jquery-3.5.1.js"></script><script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script><table id="example" class="display" style="width:100%">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; <th>Portfolio</th>&nbsp; &nbsp; &nbsp; <th>Symbol</th>&nbsp; &nbsp; &nbsp; <th>Value</th>&nbsp; &nbsp; &nbsp; <th>Payment</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2020-11-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>30000</td>&nbsp; &nbsp; &nbsp; <td>30</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-06-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>GSK</td>&nbsp; &nbsp; &nbsp; <td>44000</td>&nbsp; &nbsp; &nbsp; <td>30</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2020-11-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>GSK</td>&nbsp; &nbsp; &nbsp; <td>44000</td>&nbsp; &nbsp; &nbsp; <td>30</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2020-11-27</td>&nbsp; &nbsp; &nbsp; <td>DEA</td>&nbsp; &nbsp; &nbsp; <td>GSK</td>&nbsp; &nbsp; &nbsp; <td>26000</td>&nbsp; &nbsp; &nbsp; <td>30</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-02-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>30000</td>&nbsp; &nbsp; &nbsp; <td>50</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>30000</td>&nbsp; &nbsp; &nbsp; <td>54</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>30000</td>&nbsp; &nbsp; &nbsp; <td>57</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>DEA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>20000</td>&nbsp; &nbsp; &nbsp; <td>27</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>DEA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>20000</td>&nbsp; &nbsp; &nbsp; <td>27</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>ISA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>30000</td>&nbsp; &nbsp; &nbsp; <td>31</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>2021-05-30</td>&nbsp; &nbsp; &nbsp; <td>DEA</td>&nbsp; &nbsp; &nbsp; <td>RLSEB</td>&nbsp; &nbsp; &nbsp; <td>20000</td>&nbsp; &nbsp; &nbsp; <td>22</td>&nbsp; &nbsp; </tr>&nbsp; </tbody>&nbsp; <tfoot>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th colspan="3" style="text-align:right">Total:</th>&nbsp; &nbsp; &nbsp; <th></th>&nbsp; &nbsp; </tr>&nbsp; </tfoot></table>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答