Calculating the average of column

Calculating the average of column

jhjhjhjh Posts: 10Questions: 5Answers: 0
edited November 2021 in Free community support

Hello, I want to implement the average value in javascript using the values imported into DB(Row1, Row2). Among the values shown in the figure, I want to obtain rows, minimum, maximum, and average values of col1 to co2, which page should I refer to?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This example shows how to sum the column, it would be the same principle with a division in there too,

    Colin

  • jackalbrightjackalbright Posts: 14Questions: 4Answers: 0

    Colin, in the example that you point to, how do you know how many rows there are? In the original post, there are two rows. Is there a built-in function that gives us the number or rows or do you have to count them as you iterate over them?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited January 2023

    @jackalbright There are a couple options.

    1. The footerCallback docs state the the data parameter is the full array of table data. You can use data.length to get the full number of rows.
    2. Using the count() API you can get the number of rows in the data set if using selector-modifier to calculate over the page or filtered data. Something like this:
    var rowCount = api
                    .column(4, { page: 'current' })
                    .data()
                    .count();
    

    Kevin

  • SamkSamk Posts: 6Questions: 2Answers: 0

    I have a similar question but my cells are html input elements. Can the footer callback option work with. Columns made up of input text ?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    @Samk Yes. Taking the footerCallback example to start with and changing the Age column to text inputs.
    https://live.datatables.net/tiqezose/1/edit

    I added this function to get the value of the input:

        let inputVal = function (i) {
          return intVal( $(i).val() );
        }
    

    I changed the reduce function to call the inputVal function to get the input:

    .reduce((a, b) => intVal(a) + inputVal(b), 0);
    

    You solution may need to be different depending on what you are doing and how you have Datatables configured. If you still need help then please provide a test case (update mine if you wish) to show exactly what you have so we can offer more specific suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • SamkSamk Posts: 6Questions: 2Answers: 0

    thanks for the pointer kevin. ive posted the datatable here https://live.datatables.net/zalitado/1/edit?html, js, output

    where im trying to calc the min, avg and total of the columns. got the total but it doesn't fire dynamically .

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    @Samk see my answer in your other thread.

    Kevin

Sign In or Register to comment.