Display Specific Column Data in infoCallback

Display Specific Column Data in infoCallback

anthony89anthony89 Posts: 20Questions: 6Answers: 0
edited September 14 in Free community support

In custom formatting the infoCallback, how do I go about getting a specific column's data? I can output all the rows and their data such as given in the example. I have a table with a column called "totaltime" that I want to display.

        "infoCallback": function( settings, start, end, max, total, pre ) {
            var api = new $.fn.dataTable.Api( settings );
            console.log( api.rows( {page:'current'} ).data() );
            return start + "–" + end + " of " + total + " tracks";
        }

So, I want to return something like this:

  • 1–10 of 54 tracks (35:15:03)

where the item in parenthesis represents my totaltime column.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 10,721Questions: 25Answers: 2,449

    The FooterCallback example shows how you can get the total of a specific column. The example is summing numbers. You will need to do something different to sum times. Maybe use moment.js for this or you might find suggestions on Stack Overflow.

    Kevin

  • anthony89anthony89 Posts: 20Questions: 6Answers: 0

    What I need is to be able to access a single column from the data attribute instead of this line:

    console.log( api.rows( {page:'current'} ).data() );
    

    which displays all of the data.

    I want to only access the column called "totaltime" which is returned from the database.

  • kthorngrenkthorngren Posts: 10,721Questions: 25Answers: 2,449
    Answer ✓

    This code snippet from the example I linked to will access the data in a particular column on the current page:

                // Total over this page
                pageTotal = api
                    .column( 4, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
    

    If you need help creating this then please build a simple test case with an example of your data. We can then help you with a solution.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • anthony89anthony89 Posts: 20Questions: 6Answers: 0
    edited September 15

    Ok, so here's what I came up with and it does return a single value from a specific column, and I assume this is the best approach to take:

        var oCategoryTracksTable = $('#category_tracks_table').DataTable( {
            searching: false,
            processing: true,
            ajax: {
                "url": root + "resources/php/data_handler_category_tracks.php",
                "type": 'POST',
                "data": function(d){
                    d.category = GetCategory();
                }
            },
            // use createdrow to add record id field to the tr id
            "createdRow": function( row, data, dataIndex ) {
                $(row).attr('id', data.id);
            },
            columns: [
                { data: 'id', visible: false, searchable: false }, // Row 0
                { data: 'artist', title: 'Artist', orderable: true }, // Row 1
                { data: 'title', title: 'Title', orderable: true }, // Row 2
                { data: 'tracktime', title: 'Duration', orderable: false, searchable: false }, // Row 3
                { data: 'totaltime', visible: false, searchable: false } // Row 4
            ],
            "infoCallback": function( settings, start, end, max, total, pre ) {
                var api = new $.fn.dataTable.Api( settings );
                var totaltime = api.column( 4, { page: 'current'}).data()[0];
                return start + "–" + end + " of " + total + " tracks (" + totaltime + ")";
            }
        });
    
  • kthorngrenkthorngren Posts: 10,721Questions: 25Answers: 2,449

    Sorry, I thought you were wanting to total the totaltime column. If that approach works for what you want then its a good one :smile:

    Kevin

  • anthony89anthony89 Posts: 20Questions: 6Answers: 0

    No problem. You still helped me find the answer by the code snippet you gave as it put me on the course to find the answer I was looking for.

Sign In or Register to comment.