开发者

how to re-order blank cells from html table using jquery

开发者 https://www.devze.com 2022-12-18 05:07 出处:网络
as per question i want to re-order blank cells from table using jquery, do not want to remove the cells.. all cells with data will be first and then empty cells should be there.

as per question i want to re-order blank cells from table using jquery, do not want to remove the cells.. all cells with data will be first and then empty cells should be there.

how todo it best.

my table contains 10 cols and some rows... cols will be fixed. just want to reord开发者_StackOverflow中文版er the without removing cells.


You could try tablesorter jQuery addon, so you don't have to invent wheel again.

Don't know if it suits your needs tho. I'm not really sure what are you actually trying to achieve. It'd be best if you edited your original post with some code examples.


If what you're wanting to do is move the empty "cells" as you say and not "rows" this works...

$(document).ready(function(){
  $("td").each(function(){
    if ($(this).text().length == 0){
      $(this).appendTo($(this).closest("tr"));
    }
  });
});

That should move all empty cells to the end of each row. It may not be the fastest or most elegant but it appears to work.

However if what you're trying to do is move empty "rows" to the end of the table then this works...

$(document).ready(function(){
  $("tr").each(function(){
    var rowtext = "";
    $(this).find("td").each(function(){
      rowtext += $(this).text();
    });

    if (rowtext.length  == 0){
      $(this).appendTo($(this).closest("table"));
    }
  });
})

If these don't execute fast enough you can try speeding up this code by using a more specific selector in the first line under $(document).ready().

0

精彩评论

暂无评论...
验证码 换一张
取 消