简体   繁体   中英

export value of checkboxes to excel in jQuery datatables

I'm using jQuery Datatables pluging to view some data and I have in my table two columns containing checkboxes, I've tried to export the data from the table to an XSLX file using excel and excelHtml5 and also excelFlash buttons but I get two empty columns in the file, I've also included JSZip plugin in my project but in vain. How can I get the values of these checkboxes as booleans in my file.

SOLUTION

You're using DataTables 1.10.8. Before this version (1.10.7 and earlier) there was TableTools with fnCellRender option that would help to do what you want. Since 1.10.8 TableTools extension has been replaced with Buttons extension.

With Buttons extension you may use exportOptions and tell DataTables that you want the the data used for sorting ( orthogonal: 'sort' ). Then you need to define render function and return appropriate data when sorting is performed ( type === 'sort' ).

As a side effect this will make your checkbox columns sortable as well.

var table = $('#example1').DataTable({
    dom : 'Bfrtlip',    
    buttons: [
        {
            extend: 'excel',
            exportOptions: {
                orthogonal: 'sort'
            }
        }        
    ],
    columnDefs: [{
       targets:[0,5],
       render: function(data, type, row, meta){
          if(type === 'sort'){
             var $input = $(data).find('input[type="checkbox"]').addBack();
             data = ($input.prop('checked')) ? "1" : "0";
          }

          return data;    
       }
    }]
});

DEMO

See this jsFiddle for code and demonstration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM