Sum data=null

Sum data=null

Coder102Coder102 Posts: 78Questions: 2Answers: 0
edited October 2020 in Free community support
var table = $("#table").DataTable({
        searching: false,
        info: false,
        paging: false,
        ordering: false,
        data: dataf,
        columns: [
          {
            title: "a",
            data: "a",
          },
          {
            title: "b",
            data: "b",
          },
          {
            title: "c",
            data: "c",
          },
          {
            title: "d",
            data: null,
            render: function (data, type, row) {
              return numero.d;
            },
          },
        ],
      });
var sumb = table.column(1).data().sum();
var sumc= table.column(2).data().sum();
var nsp=table.column(3).data() -sumb+sumc

http://live.datatables.net/wobijesu/1/edit

Hello, I want to subtract the sum of column 1 plus column 2 from column 3 but I don't know how to do it because I can't use column (3) .data since the data is null

in short, from column 3 subtract column 1 plus column 2 and store it in the variable nsp

Replies

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

    you can use cells().render() to get the rendered values. Maybe something like this would work:

    table.cells(null, 3).render('display')`
    

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020
     var sumCorrectas = table.column(1).data().sum();
          var sumIncorrectas = table.column(2).data().sum();
          var noRespondio=table.cells(null, 3).render('display') -(sumCorrectas+sumIncorrectas)
    
    console.log(noRespondio)
    

    dont work, give back a NaN

    var table = $("#table").DataTable({
            searching: false,
            info: false,
            paging: false,
            ordering: false,
            data: dataf,
            columns: [
              {
                title: "a",
                data: "a",
              },
              {
                title: "b",
                data: "b",
              },
              {
                title: "c",
                data: "c",
              },
              {
                title: "d",
                data: null,
                render: function (data, type, row) {
                  return numero.d; // HERE
                },
              },
            ],
          });
    

    another thing. In column 3 I want to show that result, the result of number.d - (sumCorrectas + sumIncorrectas)

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited October 2020

    Sorry, Im not clear on what you are trying to achieve. Are you trying to do this on a row by row basis? Something like this example?

    What is the result of this?

          var noRespondio=table.cells(null, 3).render('display') 
     
    console.log(noRespondio)
    

    Can you build a running test case to show what you have so we can help with your solution?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020

    No, I want to do a calculation.
    I want to subtract from column 3 column 1 plus column 2 and save it in a variable.

    calculator= column 3 - (column 1.SUM + column 2.SUM)

    and also show in column 3 that result

    columns: [
              {
                title: "a",
                data: "a",
              },
              {
                title: "b",
                data: "b",
              },
              {
                title: "c",
                data: "c",
              },
              {
                title: "d",
                data: null,
                render: function (data, type, row) {
                  return calculator ( column 3 - (column 1 + column 2)) ; // HERE SHOW THE RESULT
                },
              },
    

    PSEUDOCODIGO
    http://live.datatables.net/siqiweya/2/edit

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
     {
               title: "d",
                data: null,
                render: function (data, type, row) {
                  return numero.d-('('+ column[2]+')'+'('+ column[3]+')');
                },
              }
    

    I try this but not found

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

    Pseudo code doesn't help. Can you build a running test case with an example of your data?

    If you are trying to calculate with sums of columns then you either drawCallback if the data changes or initComplete to run only once if the data is static.

    calculator= column 3 - (column 1.SUM + column 2.SUM)
    and also show in column 3 that result

    Not sure how you can use a particular cell as part of a calculation and store that back into the same cell. I think Excel calls this reciprocal data.

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0

    Can show you in a private message? because i use an api

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

    Just get an example of your data and use a Javascript variable. See this example. All we need is an example of the data. We don't need to see the actual API call.

    For example you have this:

    var q = json.data.dog.que;
          var dataf = q.map(function (v) {
            return {
              a: v.a,
              b: v.b,
              c: v.c,
            };
          });
          var q = json.data.cat.que;
          var dataf2 = q.map(function (v) {
            return {
              a: v.a,
              b: v.b,
              c: v.c,
            };
          });
    
     var numero = {
            d: json.data.dnum,
            e: json.data.enum,
          };
    
    

    Sounds like you want to use the numero variable as part fo the calculation. Put some values in it as part of the test case. Put some real or made up data in your other columns.

    Kevin

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

    Sorry the example you PM's me has a lot of extra code for the problem we are trying to solve.

    Is this what you are looking for?
    http://live.datatables.net/vohasiti/1/edit

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020

    I try this, thanks

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

    You need to use the data parameter to access the row data. I'm not sure what filas is. For example:

                render: function (data, type, row) {
                  return numero.participantes - (data.correctas+data.incorrectas)
                },
    

    Kevin

This discussion has been closed.