带有标题的数据表,并都将其下拉

时间:2019-07-02 14:13:49

标签: javascript jquery datatables

我有一个数据表,不幸的是,表标题被每一列的下拉选择所取代。

当我尝试打印时,由于连接了下拉菜单的所有值,因此每列输出标题均错误。

如何修改代码,以便打印出选中的整个行,并选择适当的标题名称

<!DOCTYPE html>
<html>

<body>

<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.3.1.js"></script>


<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/fixedheader/3.1.5/js/dataTables.fixedHeader.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">

<table id="example" class="stripe compact row-border hover" width="100%"></table>


<script>
      dataSet = [
    ["2019-02-05T08:25:08.816Z", "David", "Thorson", "AT&T", "david@gmail.com", "", "Message to be typed here", "Telephoned, Returned your call, Will call you, Quote Needed", "Paper", "Mike", "James, Mike", "", "\nMike - TESTED", "", "", ""],
    ["2019-02-06T15:00:56.923Z", "David", "Thorson", "AT&T", "david@gmail.com", "", "Message to be typed here", "Telephoned, Returned your call, Will call you, Quote Needed", "Paper", "Mike", "James, Mike", "", "\nMike - OK", "", "", ""],
    ["2019-02-06T19:39:19.476Z", "cd", "", "", "", "275-9288", "", "Quote Needed", "", "Mike", "Chris", "", "\nMike - OK", "", "", ""],
    ["2019-02-06T20:15:31.693Z", "Jo", "blanc", "", "", "275-6855", "call about stone", "Please call, Quote Needed", "", "Mike", "Chris", "", "\nMike - OK", "", "", ""],
    ["2019-02-06T23:47:47.663Z", "Dave", "bell", "", "", "285-8958", "", "Returned your call", "", "Mike", "Chris", "", "\nMike - OK", "", "", ""],
];
      const dataTable = $('#example').DataTable({
          data: dataSet,
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'print',
                text: 'Print all',
                exportOptions: {
                    modifier: {
                        selected: null
                    }
                }
            },
            {
                extend: 'print',
                text: 'Print selected'
            }
        ],
        select: true,
          ordering: false,
          "pageLength": 100,
          columns: [{
              title: "Date"
          }, {
              title: "Fname"
          }, {
              title: "Lname"
          }, {
              title: "Company"
          }, {
              title: "Email"
          }, {
              title: "Phone"
          }, {
              title: "Message"
          }, {
              title: "Priority"
          }, {
              title: "Source"
          }, {
              title: "Assigned"
          }, {
              title: "By"
          }, {
              title: "Info"
          }, {
              title: "SMS"
          }, {
              title: "Completion"
          }, {
              title: "CRM Number"
          }, {
              title: "Notes"
          }],

          initComplete: function() {
              const table = this.api();
              
              table.columns().every(function() {
              console.log($(this.header()).text())
                  const title = $(this.header()).text();
                  
                  $(this.header()).html(`<select><option value="">${title} (All)</option></select>`);
                  const options = this.data().unique().sort().toArray().reduce((options, item) => options += `<option value="${item}">${item}</option>`, '');
                  $(this.header()).find('select').append(options);
              });
              
          }
              });
      $('#example').on('change', 'thead select', event => dataTable.column($(event.target).closest('th')).search($(event.target).val()).draw());

</script>      
</body>

</html>

0 个答案:

没有答案