将列附加到从右侧偏移的HTML表 [英] Append columns to an HTML table offsetting from right

查看:245
本文介绍了将列附加到从右侧偏移的HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算表格第一行中的列数,减1,然后在该位置插入一列到我的表中?我希望该列的第一行与其余列不同。

How can I count the number of columns in the first row of a table, subtract by one and then insert a column at that location into my table? I would like the first row of that column to be different than the rest of them.

这是一个基本的小提琴,它将列插入到固定位置的表格中。

本主题讨论插入新列。问题是它使用固定的,从左侧开始插入列。它也不会更改顶部列。

This topic discusses inserting new columns. Problem is that it uses a fixed, from the left, position to insert the column. It also does not change the top column.

以下是我从该帖子改编的示例代码:

Here is my sample code adapted from that post:

$(document).ready(function(){
   $('#AddOT').click(function(){
      $('#Table1').find('tr').each(function(){
          $(this).find('td').eq(0).after('<td>cell 1a</td>');
      });
    });
 });

本主题讨论确定表格中的列数。示例似乎不起作用,因为在HTML中放置多个表会产生问题:

This topic discusses determining the number of columns in "tables". The sample does not seem to work because placing multiple tables in the HTML creates a problem:

    $(function() {
        var colCount = 0;
        $('tr:nth-child(1) td').each(function () {
            if ($(this).attr('colspan')) {
                colCount += +$(this).attr('colspan');
            } else {
                colCount++;
            }
        });
    });

有一个 jsfiddle 同时计算列数。

编辑:问题已解决,我能够根据行索引值将修改后的小提琴放入不同的行中。

Problem is solved and I was able to make a revised fiddle that places different content into different rows according to the row index value.

这将非常有用,因为我在不同的行上有不同类别的文本输入框,我根据类对它们求和。我现在可以动态地向所有行添加新列,但仍然在每行中保留唯一的单元类。太棒了!

This will be very useful as I have different classes of text input boxes on different rows and I sum them according to class. I can now dynamically add new columns to all the rows but still keep unique classes of cells in each row. Awesome!

推荐答案

我不确定我是否理解你的要求。看看演示

I'm not sure if I understand your requirement correctly. Take a look at the demo

$(document).ready(function(){
            $('#AddOT').click(function(){
                   var count = countColumn();
                   var insertedPosition = count-2;
                    $('#Table1').find('tr').each(function(index){
                        if (index == 0){
                            var colspan = $(this).attr('colspan') || "1";
$(this).attr('colspan',parseInt(colspan)+1);                            
                        }
                        else
                        {
                        $(this).find('td').eq(insertedPosition ).after('<td>cell 1a</td>');
                        }
                    });

            });
});

function countColumn() {
    var colCount = 0;
    $('tr:nth-child(1) td').each(function () {
        if ($(this).attr('colspan')) {
            colCount += +$(this).attr('colspan');
        } else {
            colCount++;
        }
    });
    return colCount;
}

这篇关于将列附加到从右侧偏移的HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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