Footercallback - skip dash signs without converting to 0

Footercallback - skip dash signs without converting to 0

JuliaRose25JuliaRose25 Posts: 3Questions: 1Answers: 0

Hello,

Thank you for taking the time to read this.

I know similar questions have been posted and I have spent some hours browsing all options before asking this question but I could not implement any solution.

I have a table and I am trying to calculate the average in the footer. I have managed to implement the average however, I have some td that do not have any data and therefore need to be left empty. I have entered the sign - in those cells but the code I have now, converts the dash sign to 0 which ruins the calculation of the values from the other cells. (I need accurate calculation of the values from the other cells).

Therefore, my question is: What do I need in order to completely skip/ignore those cells from the calculation and avoid converting them to 0?

Please see this link for the test case:

https://live.datatables.net/fuporifi/3/edit

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

This question has an accepted answers - jump to answer

Answers

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

    Easiest option would probably just be to check the value in the reduce function:

    if (b === '-') {
      b = 0;
    }
    

    Allan

  • JuliaRose25JuliaRose25 Posts: 3Questions: 1Answers: 0

    Hi,

    Thanks for the answer. I tried adding it just to test but it is not working.

    However, I am not trying to convert the - to 0. If the value is 0, the calculation from the column will not be correct. I need the code to completely ignore the empty cell.

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954
    edited June 2023 Answer ✓

    Since you are calculating the average then keep track of the number of skipped/empty rows in the if statement.

    Since you are calculating the average from the rows on the page, for example:

            pageTotalPower = api
                .column( 2, { page: 'current'} )
    

    You should get the number of rows using the same selector modifier. Change:

    let numRows = api.rows({search: 'applied'}).count()
    

    To:

    let numRows = api.rows({page: 'current'}).count()
    

    Here is the updated example with Allan's suggestion plus the above two changes:
    https://live.datatables.net/fuporifi/7/edit

    Kevin

  • JuliaRose25JuliaRose25 Posts: 3Questions: 1Answers: 0

    Thank you so much for you help. This works :) :)

Sign In or Register to comment.