jquery - 如何在导出的 html 表格数据中包含 <th> 行?

标签 jquery html

我使用了以下插件来导出 XLS 和 WORD 中的表格数据。但是当我导出表格数据时,它不包括表格标题行。那么有没有办法在导出的数据中包含字段。

<li>
<a href="#" onClick ="$('#datawtable').tableExport({type:'excel',escape:'false'});"> 
<img src='icons/xls.png' width='24px'> XLS
</a>
</li>
<li>
<a href="#" onClick ="$('#datawtable').tableExport({type:'doc',escape:'false'});"> 
<img src='icons/word.png' width='24px'> Word
</a>
</li>

<script type="text/javascript" src="js/tableExport.js"></script>
<script type="text/javascript" src="js/jquery.base64.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>

HTML 表格:

<table class="draw_table" id="datawtable" border="1" width="100%" cellspacing="0" cellpadding="0">
        <tr>
            <th>
                <a href="disdept.php?sort_element=dept_name&sort_type=<?php echo 
                    ($sort_element == "dept_name"  && $sort_type == "asc") ? "desc" : "asc"; ?>">Department
                    <?php if ($sort_element == "dept_name" ) {  if($sort_type == "desc" ) { ?>
                    <img class="sorting" src="images2/downarrow.png" alt="asc">
                    <?php } else { ?>
                    <img class="sorting" src="images2/uparrow.png" alt="desc">
                    <?php } } ?>
                </a>
            </th>
            <th >Description</th>
            <th >Options</th>
       </tr>

最佳答案

您的表格是正确的,但您缺少 <thead><tbody>标签。您的 Html 应该如下所示,您应该在 header 和 <tbody> 中添加标签其他行的标记。希望这能让你更清楚:

<table class="draw_table" id="datawtable" border="1" width="100%" cellspacing="0" cellpadding="0">
   <thead>  // Here we have added a <thead> tag (For headers)
        <tr>
            <th>
                <a href="disdept.php?sort_element=dept_name&sort_type=<?php echo 
                    ($sort_element == "dept_name"  && $sort_type == "asc") ? "desc" : "asc"; ?>">Department
                    <?php if ($sort_element == "dept_name" ) {  if($sort_type == "desc" ) { ?>
                    <img class="sorting" src="images2/downarrow.png" alt="asc">
                    <?php } else { ?>
                    <img class="sorting" src="images2/uparrow.png" alt="desc">
                    <?php } } ?>
                </a>
            </th>
            <th >Description</th>
            <th >Options</th>
       </tr>
</thead>
   <tbody> // Here we have added a <tbody> tag
     <tr>
      <td>
      </td>
      </tr>
   </tbody>
</table>

因为根据插件中的代码,它的选择器寻找<thead> , 然后 <tr>然后 <th>标题和 <tbody> , 然后 <tr>然后 <td>用于数据行。

编辑 1:

要忽略任何列,只需像这样在 ignoreColumn 参数中传递其索引:

$('#tableID').tableExport({
    type:'pdf',
    escape:'false',
    ignoreColumn: [2,3]  // here i have ignored 2nd and 3rd column
});

关于jquery - 如何在导出的 html 表格数据中包含 <th> 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777111/

相关文章:

javascript - 跨源请求被阻止 : Multiple API; need to aggregate data

javascript - 滚动jquery更改html标题标签背景图像

html - 圆形图像放置在 div 的底部中间

html - 启用键盘的 html 元素布局

javascript - 尽管没有错误,但未显示弹出窗口

javascript - 当我在 $(document).ready 中放置 javascript 时出现问题

javascript - Watir 不触发 Jquery 事件处理程序

javascript - 使用jquery从字符串中删除html标签

html - 如何减少 vscode 中编辑器文件的宽度,这样我就不必向右滚动到达段落末尾

html - 将文本与不同行数的 css 对齐