如何使用jQuery仅将可见的tr表导出到Excel [英] How to export only visible tr table to excel with jQuery

查看:167
本文介绍了如何使用jQuery仅将可见的tr表导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些与此插件相结合的表格 https://github.com/btechco/btechco_excelexport 将数据导出到excel.然后,我用复选框控制表格以显示或隐藏表格的特定列.

I have some table combine with this plugin https://github.com/btechco/btechco_excelexport to export data to excel. And then i control the table with checkbox to show or hide specific column of table like this.

但是,如何将仅可见的tr表导出到excel?

But, how to i can export to excel only visible tr table ?

表.

<div>
<input type="checkbox" value="blabla" checked="checked">Column 1
<input type="checkbox" value="blabla" checked="checked">Column 2
<input type="checkbox" value="blabla" checked="checked">Column 3
</div>

<button id="btnExport">Export</button>
<table id="tableexport">
  <thead>
    <tr>
      <th>No.</th>
      <th>Name</th>
      <th>Phone</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>1</td>
      <td>John</td>
      <td>123456</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Anne</td>
      <td>987654</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Nikki</td>
      <td>786578</td>
    </tr>
  </tbody>
<table>

隐藏并显示tr表

$(document).on('change', 'div input', function() {
        var checked = $(this).is(":checked");

        var index = $(this).parent().index();
            $('table thead tr ').each(function() {
                if(checked) {
                    $(this).find("th").eq(index).show();
                } else {
                    $(this).find("th").eq(index).hide(); 
                }
            });

            $('table tbody tr ').each(function() {
                if(checked) {
                    $(this).find("td").eq(index).show();
                } else {
                    $(this).find("td").eq(index).hide(); 
                }
            });
    });

然后将此代码导出到excel表. (插件)

And this code to export table to excel. (Plugin)

$(document).ready(function() {
        $("#btnExport").click(function () {
                $("#tableexport").btechco_excelexport({
                    containerid: "tableexport"
                   , datatype: $datatype.Table
                });
            });
    });

推荐答案

尝试一下:

$(document).ready(function() {
    $("#btnExport").click(function () {
            $("#tableexport").clone(true, true)
                             .find(':not(:visible)').remove()
                             .end().prop('id', 'customId').btechco_excelexport({
                                 containerid: "customId"
                                , datatype: $datatype.Table
                             });
    });
});

它将克隆表并删除所有不可见的内容,然后导出该表.

It clone the table and remove everything that is not visible then export that table.

这篇关于如何使用jQuery仅将可见的tr表导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆