Stop printing hidden hidden rows

Stop printing hidden hidden rows

nashednabilnashednabil Posts: 2Questions: 1Answers: 0
edited April 26 in Free community support

im working on this script for selecting some rows to show only , but when try to print the table , it print all rows the visible and hidden , how to stop printing hidden rows , i tryed this but not working .

  $(document).ready(function () {
      var table = $('#register').DataTable({
          sort: [[4, 'asc']],
          dom: 'Bfrtip',  
          bInfo: false,
          buttons: [
              {
                  extend: 'excel',
                  exportOptions: {
                      // Exclude the last column
                      columns: ':not(:last)'
                      
                  }
              },
              {
                  extend: 'print',
                  exportOptions: {
                      // Exclude the last column
                      columns: ':not(:last)',


                  },
                  customize: function (win) {
                      $(win.document.body).find('tr:hidden').remove();

                      $(win.document.body)
                          .css('font-size', '10pt')
                          .prepend(
                              '<div id="header" style="text-align: center;">' +
                              '<img src="/Images/charmillion_logo.jpeg" style="max-width: 100%; height: auto; width: 200px;" />' +
                              '</div>'
                          );

                      $(win.document.body).find('table')
                          .addClass('compact')
                          .css('font-size', 'inherit');
                  },
              },
              'colvis',  
              'pageLength'
          ],

          columnDefs: [
              {
                  targets: 5,
                  data: 'LostStage.StageName',
                  render: function (data, type, row) {
                      if (type === 'display') {
                          var badgeColor = '';
                          switch (data) {
                              case 'Waiting For Receiving Committee':
                                  badgeColor = 'red';
                                  break;
                              case 'In Front Office For 24 Hours':
                                  badgeColor = 'orange';
                                  break;
                              case 'Moving To Security Storage':
                                  badgeColor = 'blue';
                                  break;
                              case 'Moving To Housekeeping Storage':
                                  badgeColor = 'grey';
                                  break;
                              case 'In Security Storage':
                                  badgeColor = 'green';
                                  break;
                              case 'In HouseKeeping Storage':
                                  badgeColor = 'Fuchsia';
                                  break;
                              case 'Delivery Request':
                                  badgeColor = 'Sienna';
                                  break;
                              case 'Moving To Front Office':
                                  badgeColor = 'Turquoise';
                                  break;
                              case 'Item Has Been delivered to Its Owner':
                                  badgeColor = 'black';
                                  break;
                          }
                          return '<span class="badge" style="background-color: ' + badgeColor + ';">' + data + '</span>';
                      }
                      return data;
                  }
              },
          ],
          createdRow: function (row, data, dataIndex) {
              var stageName = $('td:eq(5)', row).text();
              var checkboxChecked = $('td:eq(8) input[type="checkbox"]', row).prop('checked');

              if ((stageName === 'Moving To Security Storage' || stageName === 'In Security Storage' || stageName === 'Delivery Request') && checkboxChecked) {
                  $(row).show();
              } else {
                  $(row).hide();
              }
          }
      });
  });

i also tried this option

extend: 'print',
    exportOptions: {
        columns: ':visible:not(.no-print)'

also not effective , any idea how to do that ???

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Don't use display: none to hide a row in DataTables (which is what $().hide() basically does). Nothing will take account of it - including export, paging, scrolling and more.

    If you need to hide a row in a DataTable use the search abilities.

    Allan

  • nashednabilnashednabil Posts: 2Questions: 1Answers: 0

    i can't find what i want in your answer !!!
    i also searched in the link you provided but got nothing , would you be more specific ??!!

  • colincolin Posts: 15,177Questions: 1Answers: 2,590
    Answer ✓

    Allan is saying if you hide a row using a jQuery command, as you are, DataTables won't know anything about it as you're not going through it's API. Because of that, it will be included in the export.

    If you don't want those columns being displayed, either remove them with rows().remove(), or filter them out with the link that Allan provided.

    Colin

Sign In or Register to comment.