I am unable to do the Sum for column Amount which is having the data like - values.

I am unable to do the Sum for column Amount which is having the data like - values.

Saritha Saritha Posts: 23Questions: 7Answers: 1

Please suggest me how to the Sum for Column at footer level.Its kind of urgency and please suggest me how to achieve the same.

j$ = jQuery.noConflict(); j$(document).ready( function () { //here i am applying the Datatable for my list in salesforce var ordermtdTable = j$('[id$="ordertable"]').DataTable({ }); });

This question has accepted answers - jump to:

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    You are going to want to do something very similar to https://datatables.net/examples/api/multi_filter_select.html except instead of adding select input, you are summing up the column values.

    If not all the columns are to be summed up, you can qualify within the https://datatables.net/reference/api/columns()

  • Saritha Saritha Posts: 23Questions: 7Answers: 1

    Hello i want something like this in below link

    https://datatables.net/examples/advanced_init/footer_callback.html
    Total needs to be calculate at footer level.
    and this code is not working to me.Suggest me any solution with example code samples

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    If you have tried the code in the linked example and it doesn't work for you, then we would need to see a working example with your code in order to help debug.

    Please see DataTables Live

  • Saritha Saritha Posts: 23Questions: 7Answers: 1

    The below is the code i am using for this.

    j$ = jQuery.noConflict(); j$(document).ready( function () { var ordermtdTable = j$('[id$="ordertable"]').DataTable({ "footerCallback": function ( row, data, start, end, display ) { var api = this.api(), data; // Remove the formatting to get integer data for summation var intVal = function ( i ) { console.log('enter1'); return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ? i : 0; }; // Total over all pages total = api .column( 6 ) .data() .reduce( function (a, b) { return intVal(a) + intVal(b); }, 0 ); // Total over this page pageTotal = api .column( 6, { page: 'current'} ) .data() .reduce( function (a, b) { return intVal(a) + intVal(b); }, 0 ); // Update footer $( api.column( 6 ).footer() ).html( '$'+pageTotal +' ( $'+ total +' total)' ); } } ); } ); }); });

    Declared th footer:
    <tfoot>
    <tr>
    <th colspan="6" style="text-align:right">Total:</th>
    <th></th>
    </tr>
    </tfoot>

    and included the javasript tags in header

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    Answer ✓

    Here is a working example

    Make sure your column # is correct. Column index starts with 0.

  • Saritha Saritha Posts: 23Questions: 7Answers: 1

    Thank you all.
    It is working as expected.Please let me know is there any chance like wanted to display only the grant total and not required the Total of the page.

  • allanallan Posts: 63,809Questions: 1Answers: 10,516 Site admin
    Answer ✓

    Yes - the example you linked to above shows how you can sum over all columns, not just the current page.

    Allan

  • Saritha Saritha Posts: 23Questions: 7Answers: 1

    Every thing looks fine,i have modified the footer logic only to display the total value of all pages.
    I have the field with negative values and sorting is not working for that particular field.
    Please suggest what are can be scenarios to solve this

  • Saritha Saritha Posts: 23Questions: 7Answers: 1

    Output is like below:

    -31643.8
    -3500
    -854.38
    -944.62
    10000
    13.05
    157.44
    157

  • allanallan Posts: 63,809Questions: 1Answers: 10,516 Site admin

    We'd need a link to the page showing the issue so it can be debugged please. If you can't link to the page, you can use JSFiddle, CodePen or similar to create a test case showing the issue.

    Allan

This discussion has been closed.