jQuery DataTables TableTools Export - get values of checkboxes or radio items

jQuery DataTables TableTools Export - get values of checkboxes or radio items

dreadedmanrazdreadedmanraz Posts: 24Questions: 5Answers: 0
edited October 2014 in TableTools

I am using TableTools v2.2.2 and DataTables 1.10.0 and have it successfully exporting some table contents. However, it is not picking up the values from the checkboxes in the table cells. How can I extract the value as true or false (or 1,0; any indicator really) from the cells and make them export?

Here is my JSFiddle for the problem.
http://jsfiddle.net/razzledazzle/e8brzc6c/4/

Other people have asked the same question, but with no response:
http://w.datatables.net/forums/discussion/23388/using-fncellrender-before-exporting-to-pdf
http://datatables.net/forums/discussion/14328/table-tools-doesn-t-export-pdf-using-fncellrender-and-tabs
http://www.datatables.net/forums/discussion/23550/jquery-datatables-tabletools-export-get-values-of-checkboxes-or-radio-items#latest - mine in the free forum.

You can see I've been trying to use fnCellRender and its anoynmous method, but even though I expect it to return " TableTools" for each cell, it returns nothing.

Thanks, Rhys

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,703Questions: 1Answers: 8,556 Site admin

    Using fnCellRender is the correct thing to do - however you need to extend a button type (sExtends) and use the function there, like in the code example for fnCellRender. Its a little bit of a pain at the moment, but you need to define that function for each button (although obviously you could define it once and then attach it to each button).

    Allan

  • dreadedmanrazdreadedmanraz Posts: 24Questions: 5Answers: 0

    Isn't this what I've already done:

    var sortColumn = 0;
    $(document).ready(function () {            
        table = $('#sort1').DataTable({
            stateSave: true,
            filter: false,
            "order": [[sortColumn, "asc"]],
            "pageLength": 25,
            "columnDefs": [
                {
                    "type" : "html",
                    "targets": ['no-sort'],
                    "orderable": false,
                },
            ],          
            "autoWidth": false,
            "tableTools": {
                "aButtons": [                
                    {
                        "sExtends": "collection",
                        "sButtonText": "Export",
                        "aButtons": ["csv", "xls", "pdf", "print"],
                        // TODO: Get this working.
                        "fnCellRender": function ( sValue, iColumn, nTr, iDataIndex ) {                     
                            return sValue + " TableTools";
                        }
                    }
                ],
                "sSwfPath": "http://datatables.net/release-datatables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (oSettings, json) {
                $(this).closest('#sort1_wrapper').find('.DTTT.btn-group').addClass('table_tools_group').children('a.btn').each(function () {
                    $(this).addClass('btn-sm btn-default btn-primary');
                });
                if ($('td.dataTables_empty')) {
                    $('td.dataTables_empty').parent().hide();
                }
                $(this).closest('#sort1_wrapper').addClass('dataTables-scrollX');
            },
    
            "dom": "<'dt-toolbar'<'col-xs-12 col-sm-6'fi><'col-sm-6 col-xs-6 hidden-xs'plT>r>" +                        
                     't<"dt-toolbar-footer"<"col-sm-6 col-xs-12 hidden-xs"i><"col-xs-12 col-sm-6"p>><"clear">"'     
        });
    
    
  • allanallan Posts: 54,703Questions: 1Answers: 8,556 Site admin

    Ah I see. Sort of. You've got the cell renderer at the rendered for the collection button. but the other buttons in the collection do not inherit that property. You need to specify it for them as well (actually it is completely redundant on fnCellRender since it is never used there).

    The planned rewrite of TableTools will include inheritance.

    Allan

  • dreadedmanrazdreadedmanraz Posts: 24Questions: 5Answers: 0
    edited October 2014

    I have successfully done so by NOT using a collection. However, how can I code it to work with the Exports collection?

    var table;
    $(document).ready(function () {
        //TODO: Get table tools working server-side e.g. https://github.com/ALMMa/datatables.mvc        
        table = $('#sort1').DataTable({
            stateSave: true,
            filter: false,
            "order": [[sortColumn, "asc"]],
            "pageLength": 25,
            "columnDefs": [
                {
                    "type" : "html",
                    "targets": ['no-sort'],
                    "orderable": false,
                },
            ],          
            "autoWidth": false,
            "tableTools": {
                "aButtons": [                
                    {
                        sExtends: "csv",                    
                        "fnCellRender": renderColumn                   
                    },
                    {
                        sExtends: "xls",
                        "fnCellRender": renderColumn
                    },
                    {
                        sExtends: "pdf",
                        "fnCellRender": renderColumn
                    },
                    {
                        sExtends: "print",
                    }
                ],
                "sSwfPath": "/Scripts/plugin/datatables/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (oSettings, json) {
                //$(this).closest('#sort1_wrapper').find('.DTTT.btn-group').addClass('table_tools_group').children('a.btn').each(function () {
                //    $(this).addClass('btn-sm btn-default btn-primary');
                //});
                if ($('td.dataTables_empty')) {
                    $('td.dataTables_empty').parent().hide();
                }
                $(this).closest('#sort1_wrapper').addClass('dataTables-scrollX');
            },
            // This sets the resulting html layout i.e. https://datatables.net/reference/option/dom     
            "dom": "<'dt-toolbar'<'col-xs-12 col-sm-6'fi><'col-sm-6 col-xs-6 hidden-xs'plT>r>" +                        
                     't<"dt-toolbar-footer"<"col-sm-6 col-xs-12 hidden-xs"i><"col-xs-12 col-sm-6"p>><"clear">"'     
        });
    
        
        // Check all boxes.    
        $('#include').click(function () {
            if (this.checked) {
                $('.checked').prop('checked', true);
            }
            else {
                $('.checked').removeAttr('checked');
            }
        });
        $('.checked').click(function () {
            checkInclude();
        });
        checkInclude();
    });
    
    // This extracts the value from the checkboxes
    function renderColumn(value, column, row, iDataIndex) {
        if (column != sortColumn && $(value).find('input.onoffswitch-checkbox').length > 0) {
            return $(value).find('input.onoffswitch-checkbox').prop('checked');
        }
        return value;
    }
    
  • allanallan Posts: 54,703Questions: 1Answers: 8,556 Site admin
    Answer ✓

    Exactly the same, just extend the buttons in the collection.

                        "sExtends": "collection",
                        "sButtonText": "Export",
                        "aButtons": [ {
                          "sExtends": "csv",
                           "fnCellRender": function ( sValue, iColumn, nTr, iDataIndex ) {                    
                              return sValue + " TableTools";
                           }"
                         },
                         etc...
                         ]
    

    Allan

  • dreadedmanrazdreadedmanraz Posts: 24Questions: 5Answers: 0

    Perfect. Thanks very much.

This discussion has been closed.