Why this Jquery works on DataTables print priview customization

Why this Jquery works on DataTables print priview customization

LaskiLaski Posts: 2Questions: 1Answers: 0

Hello,

I had a problem to exclude first column from print view if its set as visible, datatables has Colvis button all so in use.
And if the first column aint presented take all visible columns.

My plan was to make ID value of NoExport to first column and with customization loop all columns and if NoExport ID column is visible remove it from print view.
I dont know why the following code is working. It actually only hides the first column if the column with NoExport id is presented.
My understand of coding, aint so profissional at it yeat, says that this should allways hide the first column, because allways when function is runned ill set in the jquery the value that i compare in jquery.

Other buttons are hiding column other way, everything is working just curious why its working.

Table code :

<asp:Repeater ID="Computers" runat="server" OnItemCommand="Computers_ItemCommand">
                <HeaderTemplate>
                    <table class="Table table-striped Computer" style="width: 100%; display:none" id="Computer">
                        <thead>
                            <tr style="height: 25px">
                                <th id="NoExport">Info</th>
                                <th>Computer name</th>
                                <th>Primary user</th>
                                <th>Operating System</th>
                                <th>Operating System version</th>
                                <th>Provider</th>
                                <th>Provider version</th>
                                <th>Domain</th>
                                <th>Manufacturer</th>
                                <th>Model</th>
                                <th>Serial</th>
                                <th>Last update</th>
                            </tr>
                        </thead>
                        <tbody>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <a href="../pages/Pages.aspx">
                                <div class="glyphicon glyphicon-info-sign"></span></div>
                            </a>
                        </td>
                        <td><%# DataBinder.Eval(Container.DataItem, "FQDN")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "PrimaryUser")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "OS")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "OS_VERSION")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "PROVIDER")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "PROVIDER_VERSION")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "DOMAIN")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "MANUFACTURER")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "MODEL")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "SERIAL")%></td>
                        <td><%# DataBinder.Eval(Container.DataItem, "LASTSEEN")%></td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </tbody>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

Print button script lines:

 buttons: [
                        {
                            extend: 'print',
                            text: 'Print view',
                            autoPrint: false,
                            exportOptions: {
                                columns: ':visible',
                                modifier: {
                                    page: 'current'
                                }
                            },
                            customize: function (win) {
                                var colid = NoExport;
                                if (colid == NoExport) {
                                    $(win.document.body).find('table').find('td:first-child, th:first-child').remove();
                                }
                                else {
                                }
                            }
                        }, 

Answers

  • LaskiLaski Posts: 2Questions: 1Answers: 0

    Rest of the question that didnt fit in the original post:
    Full Script code:

            $(document).ready(function () {
                var hideFromExport = [0];
                var table = $('#Computer').DataTable({
                    "columnDefs": [
                {
                    "targets": [4, 6, 7, 8, 9, 10],
                    "visible": false
                }],
                    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    "iDisplayLength": 25,
                    pageResize: true,
                    "lengthChange": true,
                    paging: true,
                    responsive: true,
                    "pagingType": "full_numbers",
                    "processing": true,
                    "order": [],
                    "ServerSide": true,
                    dom: "<'row'<'col-sm-3'l><'col-sm-2'C><'col-sm-4'B><'col-sm-3'f>>" +
                    "<'row'<'col-sm-12'tr>>" +
    "<'row'<'col-sm-5'i><'col-sm-7'p>>",
                    buttons: [
                            {
                                extend: 'print',
                                text: 'Print view',
                                autoPrint: false,
                                exportOptions: {
                                    columns: ':visible',
                                    modifier: {
                                        page: 'current'
                                    }
                                },
                                customize: function (win) {
                                    var colid = NoExport;
                                    if (colid == NoExport) {
                                        $(win.document.body).find('table').find('td:first-child, th:first-child').remove();
                                    }
                                    else {
                                    }
                                }
                            }, {
                                extend: 'copyHtml5',
                                text: 'Copy',
                                exportOptions: {
                                    columns: function (idx, data, node) {
                                        var isVisible = table.column(idx).visible();
                                        var isNotForExport = $.inArray(idx, hideFromExport) !== -1;
                                        return isVisible && !isNotForExport ? true : false;
                                    },
                                    modifier: {
                                        page: 'current'
                                    }
                                },
                            }, {
                                extend: 'colvis',
                                text: 'Columns',
                                 },
          {
              extend: 'collection',
              text: 'Export',
              buttons: [
                  {
                      extend: 'excel',
                      text: 'excel',
                      exportOptions: {
                          columns: function (idx, data, node) {
                              var isVisible = table.column(idx).visible();
                              var isNotForExport = $.inArray(idx, hideFromExport) !== -1;
                              return isVisible && !isNotForExport ? true : false;
                          },
                          modifier: {
                              page: 'current'
                          }
                      }
                  },
                                                  {
                                                      extend: 'csvHtml5',
                                                      text: 'csv',
                                                      exportOptions: {
                                                          columns: function (idx, data, node) {
                                                              var isVisible = table.column(idx).visible();
                                                              var isNotForExport = $.inArray(idx, hideFromExport) !== -1;
                                                              return isVisible && !isNotForExport ? true : false;
                                                          },
                                                          modifier: {
                                                              page: 'current'
                                                          }
                                                      }
                                                  },
                                                  {
                                                      extend: 'pdfHtml5',
                                                      text: 'PDF',
                                                      orientation: 'landscape',
                                                      customize: function (doc) {
                                                          doc.content.splice(1, 0, {
                                                              margin: [0, 0, 0, 12],
                                                              alignment: 'center',
                                                          })
                                                      },
                                                      exportOptions: {
                                                          columns: function (idx, data, node) {
                                                              var isVisible = table.column(idx).visible();
                                                              var isNotForExport = $.inArray(idx, hideFromExport) !== -1;
                                                              return isVisible && !isNotForExport ? true : false;
                                                          },
                                                          modifier: {
                                                              page: 'current'
                                                          }
                                                      }
                                                  }
              ]
          }
                    ]
                }
                )
                $('#Computer').show();
            })
    

    And in table i have column with ID NoExport. My plan was loop each colum and compare if the ID is NoExport. But some how that code hides the column with ID NoExport. If i hide the column with Colvis wich has the NoExport ID, no columns get hided. If Set to visible through ColVis button.

This discussion has been closed.