简体   繁体   English

使用jQuery修复HTML表的各个部分

[英]Making sections of an html table fixed using jQuery

I have an HTML table which is dynamically populated from data fetched from a server. 我有一个HTML表,该表是根据从服务器获取的数据动态填充的。 Since I don't know the schema or the number of rows before hand, I have its, and its row's & cell's position defined as relative. 由于我不知道架构或行前的行数,因此将其及其行和&单元格的位置定义为相对。 Now once the table is populated, I want to delete a set of rows from it(specified by an array of row indexes). 现在,一旦表被填充,我想从中删除一组行(由行索引数组指定)。 I am trying for the following animation for these row deletions. 我正在为这些行删除尝试以下动画。 The rows which are supposed to be deleted fade out leaving blank space in the table.->The other rows slide up to fill the space created between them. 应该删除的行逐渐消失,表格中留有空白。->其他行向上滑动以填充它们之间创建的空间。

I have tried out following things: 我已经尝试了以下方法:

  1. Simply fading out or sliding up the rows to be deleted using jquery. 只需使用jquery淡出或滑动要删除的行即可。 The problem with this is, the animation is jerky, and all I can see is the bunch of rows disappearing at once. 问题在于,动画是生涩的,我只能看到一堆行立即消失。

  2. Setting the 'position' of all other rows except the ones to be deleted as fixed, Fading out the rows to be deleted, and then sliding up the rows which were fixed. 将除要删除的行以外的所有其他行的“位置”设置为固定,淡出要删除的行,然后向上滑动固定的行。 The problem with this is - Since the rows were relatively placed, once I make them fixed they all lose all their previous styles, contract, and lose the well defined structure and overwriting each other. 问题是-由于行是相对放置的,一旦我将它们固定,它们都将失去以前的所有样式,收缩,并失去定义良好的结构并相互覆盖。 What would be the best way to implement the required kind of animation? 实现所需动画种类的最佳方法是什么?

Also, how would one go about specifying a bunch of rows (specified by a list of indexes) for a jQuery selector. 另外,如何为jQuery选择器指定一堆行(由索引列表指定)。 Right now I am creating the tr:nth-of-type(i) selector for each element of the list, and concatenating them in a large string separated by ',', and using this as the selector for all the rows. 现在,我正在为列表中的每个元素创建tr:nth-​​of-type(i)选择器,并将它们连接到以','分隔的大字符串中,并将其用作所有行的选择器。 Is there a better way to do this? 有一个更好的方法吗?

jsBin demo jsBin演示

Don't animate table elements. 不要为表格元素设置动画。 Ever. 永远。
Instead animate DIV elements inside your row cells. 而是为行单元内的DIV元素设置动画。 Once they faded out and animated to height 0 , than you're free to remove the parent TR 一旦它们淡出并设置为动画高度0 ,您就可以随意删除父TR

Example: 例:

 $("button").on("click", function(){ // Table with no DIV elements (Animate ROWS) $("#noDivs tr:eq(1)").animate({opacity:0}, 800, function(){ $(this).slideUp(); }); // Table with DIVs (Animate DIV) $("#divs tr:eq(1) div").animate({opacity:0}, 800, function(){ $(this).slideUp(); }); }); 
 table{ border-collapse: separate; border-spacing: 0px; } table td{ padding:0; margin:0; } table#noDivs td, table#divs div{ border:1px solid #ddd; padding:10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Delete rows 2</button> <br><br> Animate DIV instead: <table id="divs"> <tr> <td><div>Cell 1</div></td> <td><div>Cell 2</div></td> <td><div>Cell 3</div></td> <td><div>Cell 4</div></td> </tr> <tr> <td><div>Cell 1</div></td> <td><div>Cell 2</div></td> <td><div>Cell 3</div></td> <td><div>Cell 4</div></td> </tr> <tr> <td><div>Cell 1</div></td> <td><div>Cell 2</div></td> <td><div>Cell 3</div></td> <td><div>Cell 4</div></td> </tr> <tr> <td><div>Cell 1</div></td> <td><div>Cell 2</div></td> <td><div>Cell 3</div></td> <td><div>Cell 4</div></td> </tr> </table> Animate TR (Issue) <table id="noDivs"> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> </tr> </table> 

you should add/remove a class and use CSS to animate. 您应该添加/删除类并使用CSS进行动画处理。

here an example (only css for test ONLY) to see how it would/could work 这是一个示例(仅用于测试的css)以查看其如何/可以工作

 table { width:600px; border:solid; } td { border:solid; } /* demo purpose , instead js */ td { pointer-events:none; font-size:1.2em; opacity:1; } tr:focus td { font-size:0; border:solid 0 transparent; opacity:0; transition:1s;/*with steps or css animation is fine too */ } 
 <table> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> <tr tabindex="0"> <td>hide</td> <td>my</td> <td>row</td> </tr> </table> 

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

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