How can I get the sum of the first 5 rows?
How can I get the sum of the first 5 rows?

I have an example of how to sum a column of data on a page as well as the grand total but I need to get the total of the top 5 records, then the first 10 records and then the first 20. Can you point me in the right direction?
This question has an accepted answers - jump to answer
Answers
Use
column().data()
to get the array of data in the column. UsetoArray()
to convert the returned API to a Javascript array. See the updated test case:https://live.datatables.net/cisixiyi/2/edit
The test case shows using Javascript slice() to get the first 5 elements of the array. You can then sum this array.
Another option si to iterate the array and in the loop have accumulators for 5, 10 and 20 records. Use the index to determine which accumulators to sum, for example:
Or choose any method that fits your specific situation.
Kevin
Perfect! Thanks for the help!