Calculated fields in rows and column totals

Calculated fields in rows and column totals

Peter.prc48Peter.prc48 Posts: 22Questions: 6Answers: 1

Hi,
I'm trying to generate three calculated fields in each row and then sum the totals of these three columns.
My first attempt for the row data was to do the calculations client-side - this I managed okay but when it came to total the columns using the footercallback, it didn't work - I don't think it sees the calculated data in each row when it runs. Client-side, I thought I'd have to do all the calculations for each row again and total them for the totals. As this seemed like a poor approach, I thought it best to do it server-side using raw sql. Now my issue is that I get all the row data with each rows calculated fields in an array but now I'm missing some of the 'surrounding' json structure usually provided by the Editor e.g. the DT_RowId's.
My question is: How can I run a raw sql query server side and still get the 'standard' json structure.
I've checked through many of the posts but most are PHP and its PortuGreek to me.
My .NET code for the query is:

var response = new Editor(db)
.Model<TaskModel>()
Process(dataist).Db().Sql("My raw sql query").FetchAll();

return Json(response);

Sorry I don't have a live site to link to and the DataTables live system looks like its more for front-end issues.
Thanks,

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    I don't think it sees the calculated data in each row when it runs

    It might - but it depends how the table is configured. You might want to take a look at this thread on this topic.

    On the server-side you could do the following:

    var response = new Editor(db)
    .Model<TaskModel>()
    Process(dataist);
    
    var extra = db.Sql("My raw sql query").FetchAll();
    
    response.meta.Add( "extra", extra );
    
    return Json(response);
    

    The meta object in the DTResponse class is just a Dictionary<string, object> so you can easily add anything you want to it. Then in the JSON you have meta.extra as the array returned.

    Allan

  • Peter.prc48Peter.prc48 Posts: 22Questions: 6Answers: 1

    Thanks Allan,
    It worked client-side, using the footercallback method you linked to.
    I had a small issue as 2 of my columns were rendered prefixed with a dollar sign but a slight change to the footercallback using

    var x = parseFloat(accounting.unformat(a)) || 0;
    var y = parseFloat(accounting.unformat(b)) || 0;
    

    solved the problem.
    Cheers.

  • Peter.prc48Peter.prc48 Posts: 22Questions: 6Answers: 1

    I must have skipped client-side data access 101 in my self-learning of Datatables :-(
    I have the meta data added and being returned in the JSON but I can't seem to access it - I don't know the correct syntax client-side and also at which point can it be accessed?
    I've tried in my footerCallback but should it be accessed outside the table scope?

    myTable = $("#myTable").DataTable({blah});
    var metaData = myTable.Data().meta;
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Use ajax.json() to access the JSON returned from the server.

    Remember also that this is only useful once the Ajax request has returned (so you might need to use initComplete depending on how your code is called).

    Allan

This discussion has been closed.