简体   繁体   English

我想在 UI 中只向用户显示 3 列,但想将所有表数据导出到 excel,那么如何根据 header 文本隐藏列?

[英]I want to show the user only 3 columns in UI, But want to Export all table data to excel, So how can I hide the column based on the header text?

This is My Table, I want to show only First 3 columns to User but i want to export All data to Excel.这是我的表,我只想向用户显示前 3 列,但我想将所有数据导出到 Excel。 First how Can I hide the Columns based on the Header text?首先,如何根据 Header 文本隐藏列?

<table id="ibms" class="table table-bordered">

                              <thead>    
                                <tr>                      
                                  <th>IBMS Code</th>
                                  <th>Location Description</th>
                                  <th>FMS Location Code</th>
                                  <th>FMS Location Description</th>
                                  <th>Site</th>
                                  <th>Level</th>
                                  <th>Area</th>
                                  <th>Zone</th>
                                  <th>Unit</th>
                                  <th>Location</th>
                              </tr>
                          </thead> 
           
                             <tbody>
    
                            <tr><td>IB-0078</td>
                                  <td>Hello</td>
                                  <td>542</td>
                                  <td>Description here</td>
                                  <td>Industry</td>
                                  <td>1</td>
                                  <td>Arizona</td>
                                  <td>five</td>
                                  <td>2</td>
                                  <td>USA</td>
                               </tr>
                               <tr>
                               <td>IB-552</td>
                                  <td>World</td>
                                  <td>576</td>
                                  <td>Description here</td>
                                  <td>Textile</td>
                                  <td>2</td>
                                  <td>Texas</td>
                                  <td>one</td>
                                  <td>10</td>
                                  <td>USA</td>
                             </tr>
                             </tbody>
                            </table>
    
    
             

JS code: JS代码:
var hidecolumns = $("#ibms").DataTable(); var hidecolumns = $("#ibms").DataTable();

        function locationhie(hidecolumns){       
                    
                            var u = $("th:contains(FMS Location Description)").index();
                            hidecolumns.column(u).visible( false );
                        }
        function locationhieSite(hidecolumns){       
                    
                            var a = $("th:contains(Site)").index();
                            hidecolumns.column(a).visible( false );
                        }
        function locationhielevel(hidecolumns){       
                    
                            var b = $("th:contains(Level)").index();
                            hidecolumns.column(b).visible( false );
                        }
        function locationhieArea(hidecolumns){       
                    
                            var c = $("th:contains(Area)").index();
                            hidecolumns.column(c).visible( false );
                        }
        function locationhieZone(hidecolumns){       
                    
                            var d = $("th:contains(Zone)").index();
                            hidecolumns.column(d).visible( false );
                        }
        function locationhieUnit(hidecolumns){       
                    
                            var e = $("th:contains(Unit)").index();
                            hidecolumns.column(e).visible( false );
                        }
        function locationhieLocation(hidecolumns){       
                    
                            var f = $("th:contains(Location)").index();
                            hidecolumns.column(f).visible( false );
                        }

Can Anyone help me how to Achieve this?任何人都可以帮助我如何实现这一目标? Is there any alternative solutions available to do this in a single function?是否有任何替代解决方案可以在单个 function 中执行此操作?

You can use CSS :nth-child pseudo-class selector to hide some columns, no JS needed:您可以使用 CSS :nth-child伪类选择器来隐藏一些列,不需要 JS:

 .locations td:nth-child(n+4), .locations th:nth-child(n+4) { display: none; }
 <table id="ibms" class="table table-bordered locations"> <thead> <tr> <th>IBMS Code</th> <th>Location Description</th> <th>FMS Location Code</th> <th>FMS Location Description</th> <th>Site</th> <th>Level</th> <th>Area</th> <th>Zone</th> <th>Unit</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>IB-0078</td> <td>Hello</td> <td>542</td> <td>Description here</td> <td>Industry</td> <td>1</td> <td>Arizona</td> <td>five</td> <td>2</td> <td>USA</td> </tr> <tr> <td>IB-552</td> <td>World</td> <td>576</td> <td>Description here</td> <td>Textile</td> <td>2</td> <td>Texas</td> <td>one</td> <td>10</td> <td>USA</td> </tr> </tbody> </table>

.locations td:nth-child(n+4) selects any td element within an element of .locations class starting from the index of 4. Indices are calculated from the first occurence of td in its parent, and the first index is 1. .locations td:nth-child(n+4)从索引 4 开始选择.locations class 的元素中的任何td元素。索引从td在其父项中的第一次出现开始计算,第一个索引为 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM