How to make a sum of the data filtered

How to make a sum of the data filtered

fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

Hi,
I have for exemple 3000 rows in my table, i, I filter them, using the filter zone, I get for example;
Page x on 6 pages / 150 records (10 records per page)
I used the sum plug in to compute my sum, but it computes the 3000 rows in any case.
I just want the total of the 150 records filtered (don't care if 10 or 25 or 100 records per page)
I want the whole sum filtered
Is it possible ?

Regards,
François

Replies

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0
    edited May 2022

    cf : http://live.datatables.net/yijufoqa/1
    If I enter **New **in the filter zone, to get the 11 records for New-york, the sum is 248650 !
    I want the total for new york only ( enven if the display is on 2 pages because of 10 records per page)

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Use the selector-modifier of search:'applied'. Similar to this example.

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0
    edited May 2022

    Hi, thanks, look s great, and close of my request, in the example you sent me, if we filter London => 12 results
    I wish to get the sum of the 12 results either we are on 10 records per page (then we get 2 pages) or 25 records per page (then one page is enough)

    Hope my explanations are clear :(

    Regards,
    François

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    The example uses { page: 'current' } to sum the data on the page. Instead of that use {{ search:'applied' } to sum the data based on the search results. If you still need help please provide a test case showing the problem.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    hi Kevin ,
    I have problms with tht JS code, could you help:
    http://live.datatables.net/dehinumu/3/edit

    Regards,

    François

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

    You have two things going on:

    1. you were initialising the table twice
    2. bad data for Garrett Winters that was breaking the all page count

    Please find updated test case here,

    Colin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0
    edited May 2022

    hi,
    Ok, got it, thanks a lot

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    dont manage to do in this example, I want to set the code
    "lengthChange": true,
    "paging": true,
    "searching": true,
    "info": true,
    "pageLength": 10,
    "order": [[ 4, "desc" ]],
    "language": {
    "lengthMenu": "Affiche MENU par page",
    "zeroRecords": "Rien à afficher",
    "info": "Page PAGE sur PAGES, avec TOTAL résultats au total",
    "infoEmpty": "Rien à afficher",
    "infoFiltered": "(filtrage sur MAX données au total)",
    "search": "Filtrer:",
    "paginate": {
    "first": "Premier",
    "last": "Dernier",
    "next": "Suivant",
    "previous": "Précédent"
    },
    }
    in the nex declaation of the tabl, but don't get the syntax :(

    http://live.datatables.net/dehinumu/5/edit

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited May 2022

    As Colin explained you are getting this error because you are trying to initialize the Datatable twice:

    DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

    Please read the technote in the link to learn how to troubleshoot and fix the error. Basically you need to combine all the config options into one initialization, like this:
    http://live.datatables.net/jifowusa/1/edit

    Also, as Colin said, you have an error with the salary for Garrett Winters. You have this:

              <tr>
                <td>Garrett Winters</td>
                <td>Director</td>
                <td>Edinburgh</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>3.15 €0</td>
              </tr>
    

    Remove the trailing0so you have3.15 €`.

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    ok, but I want to add the following code in the JS part, and don't get thet syntax, can you halp ?
    "lengthChange": true,
    "paging": true,
    "searching": true,
    "info": true,
    "pageLength": 10,
    "order": [[ 4, "desc" ]],
    "language": {
    "lengthMenu": "Affiche MENU par page",
    "zeroRecords": "Rien à afficher",
    "info": "Page PAGE sur PAGES, avec TOTAL résultats au total",
    "infoEmpty": "Rien à afficher",
    "infoFiltered": "(filtrage sur MAX données au total)",
    "search": "Filtrer:",
    "paginate": {
    "first": "Premier",
    "last": "Dernier",
    "next": "Suivant",
    "previous": "Précédent"

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Please update the test case to show the issue. Its hard to say what is wrong without seeing the problem.

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    here is the test case updated ;
    http://live.datatables.net/dehinumu/5/edit

    In the JS, I want to include what is commented in the new table initialiastion above.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Did you see my example above which combines both?
    http://live.datatables.net/jifowusa/1/edit

    If you still need help please update the test case to show the issue. Meaning combine the code as your are trying so we can help debug.

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    Hi, this works well but in fact my numbers are formatted whh ' ' to separate the thousand, then the sum does not work .
    see : http://live.datatables.net/jifowusa/3/edit
    I'm not comfortable at all in JS, how should I change the code so as it works and display the result formatted as well .
    Thanks a million,
    François

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

    You've got

                <td>1 12.50 €</td>
    

    for Tiger Nixon, another bad value, so you need to fix that,

    Colin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    I know I have 1 12.50 € and this is what I want to be displayed, I want a ' ' to separate the thousandss, so how could we delete the ' ' in order ton make the total ?

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

    That's fine, but then it's not a valid number so you can't do sums on it! You need to give the data as a number to do those calculations.

    Colin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    then how could I create a hidden column tha will containt tht numbers not formatted ?

    if I add a ccolum, it will be pislayed and I want it hidden .

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    or in the function that calculates, I already remove the €, so can I remove the ' ' by the way ?
    if yes, how ?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Look for this in your code:

    return typeof i === 'string' ? i.replace(/[\€,]/g, '')
    

    The replace statement is used to remove non-numeric characters. Add a space and whatever other characters you need to remove to create a numeric value.

    Kevin

  • fmargueriefmarguerie Posts: 36Questions: 3Answers: 0

    great

Sign In or Register to comment.