【问题标题】:How to calculate the table row that has rowspan attribute如何计算具有 rowspan 属性的表行
【发布时间】:2015-12-17 20:28:11
【问题描述】:

您好,我正在动态创建一个表结构。当我单击一个包时,我将子行插入到表中,并添加了一个带有子值计数的行跨值。现在我必须对我使用的行索引的行进行编号,但是当存在行跨度时,它应该考虑整行,因为说 1 下一个数字必须是 2。当我单击删除时,我需要删除所有跨行的子行. 这是我实现的代码,

  var rowspans;
  var servicecount;
  var cnt;
  var ser = [];
  var refForEmployee = new Firebase("https://kekranmekrandubai.firebaseio.com/package");

  refForEmployee.on("value", function(snapshot) {
    var data = snapshot.val();
    var list = [];

    for (var key in data) {
      if (data.hasOwnProperty(key)) {
        name = data[key].image ? data[key].image : '';
        emp_name = data[key].package_name ? data[key].package_name : '';
        service = data[key].service ? data[key].service : '';
        servicecount = service.length;

        console.log("service data");
        console.log(service);
        console.log(servicecount);
        if (name.trim().length > 0) {
          list.push({
            image: name,
            emp_name: emp_name,
            services: service
          })
        }
      }
    }
    // refresh the UI
    refreshUI(list);


  });
  function refreshUI(list) {
    var lis = '';
    for (var i = 0; i < list.length; i++) {
      var empname = list[i].emp_name;
      var serc = [];
      serc = list[i].services;
      lis += '<div class="outlining"><div class="customize"><img class="employeeimages" src="' + list[i].image + '"></img><img src="img/bookingemployeeid.png" class="employee_id_display"><p class="firstname">' + list[i].emp_name + '<p class="lastname">Last name</p><p class="emps_id">1001</p><p class="arrays">' + serc + '</p></div></div>';

    };

    document.querySelector('#employee_list').innerHTML = lis;
  };
  $('body').on('click', '.employeeimages', function() {
    var name = $(this).closest('.customize').find('.firstname').text();
    var service = [];
    service = $(this).closest('.customize').find('.arrays').text();
    console.log(service);

    //var myString = "Mr. Jack Adams";
    // Create a variable to contain the array
    var mySplitResult;
    // Use the string.split function to split the string
    mySplitResult = service.split(",");
    for (i = 0; i < mySplitResult.length; i++) {

      console.log(mySplitResult[i]);
      $("#booktable").append('<tr><td><div class="maindiv"><div class="productdiv"><p class="select_product">Select Items</p></div><div class="employeediv"><p>' + mySplitResult[i] + '</p></div><div class="pricediv">Price</div></div></td></tr>');

    }
    rowspans = mySplitResult.length;
    get_values(name, table_selected_index);

    $(".select_employee_div").css("display", "none");

  });
  //booking main table
  //add row to booking table
  $(".useradd").click(function() {
    var rows = $('#booktable tr').length;
    var rowcount = rows + 1;

    $("#booktable").append('<tr><td id=' + rowcount + ' class="f">' + rowcount + '</td><td><div class="maindiv"><div class="productdiv"><p class="select_product">Select Items</p></div><div class="employeediv"><input type="button" class="select_employee" value="Select Employee"></div><div class="pricediv">Price</div><div class="actiondiv"><input type="button" value="x" class="remove"/></div></div></td></tr>');
  });

  //remove row and dynamically changing the index value of the removed row

  $("body").on("click", ".remove", function(event) {
    event.preventDefault();
    var table = 'booktable';
    var row = $(this).closest('tr');

    setTimeout(function() { // Simulating ajax
      var siblings = row.siblings();
      row.remove();
      siblings.each(function(index) {
        $(this).children().first().text(index + 1);
      });
    }, 100);
  });

  $('#booktable').on('click', '.select_employee', function() {
    $(".select_employee_div").css("display", "block");
    var indexOfTheChangedRow = $(this).closest("tr").index();
    table_selected_index = indexOfTheChangedRow;

  });

  function get_values(val, rowIndex) {
    console.log("inside get_values function");

    console.log(val);

    $('#booktable tr:eq(' + rowIndex + ')').find(".select_employee").val(val);
    $('#booktable tr:eq(' + rowIndex + ')').find(".f").attr("rowspan", rowspans + 1)

  };

这是我需要的表结构

 

这是一个我已经实现的小提琴演示。我对更改行索引和删除操作感到震惊。Fiddle Demo 请帮我编码。提前谢谢你。

【问题讨论】:

  • 你能解释一下我需要删除所有跨行的子行吗?
  • 谢谢。我将解释@ Rayon Dabre ...我有一行行跨度值为 3,然后必须从该当前行删除接下来的 3 行。因为它们是跨行的子 roes。
  • 删除操作后的循环?
  • 你试过用 $('tr>td[rowspan]').length 吗?
  • 如果你必须删除所有行,使用 $('tr').remove() 或者如果你想删除所有行的行 $('td [rowspan]').closest('tr').remove()

标签: jquery html css


【解决方案1】:

这可能就是你要找的东西:

$('body').on('click', '.remove', function(e){
    e.preventDefault();
    var row=$(this).closest('tr');
    row.nextAll().each(function(){
        if ($('td',this).is('[rowspan]')){
            $(this).remove()
        } else {
         row.remove(); // before exit from loop
         return false;
        }
    })
    row.remove(); //for the last TR
    $('tr').each(function(i) {
        $('td:first',this).text(i+1); //For re-index
    });
})

或者,如果您只需要删除第一行和接下来的 2 行,这应该可以解决您的问题:

$('body').on('click', '.remove', function(e){
    e.preventDefault();
    var row = $(this).closest('tr');
    row.add(row.nextAll().slice(0,2)).remove();
});

感谢答案:Jquery Next/NextAll/NextUntil with count limit

【讨论】:

  • 非常感谢@Vixed.. 第一个解决方案仅删除第一行。在第二个解决方案中,我们可以动态地将索引发送到 slice 方法,以便它与我的代码一起正常工作吗?
  • 我使用了以下代码。 $("body").on("click", ".remove", function(event) { var table = 'booktable'; var row = $(this).closest('tr'); var rowspans1 = rowspans+ 1; row.add(row.nextAll().slice(0,rowspans)).remove(); setTimeout(function() { var Siblings = row.siblings(); Siblings.each(function(index) { $( this).children().first().text(index + 1); }); }, 100); });
  • 但我无法正确重新索引和删除索引@Vixed...请帮助我
  • 它在表格的末尾工作,但不在@Vixed 之间
  • 我稍后会处理它。抱歉@Anu,我现在不能。
【解决方案2】:

您可以使用下面的代码来删除基于 rowspan 的行并重新索引剩余的行:

$('body').on('click','.remove',function(e) {
    e.preventDefault();
    var row=$(this).closest('tr');
    var rowspan=parseInt(row.children().first().attr('rowspan'));

    //remove row
    if (isNaN(rowspan)) {
        row.remove();
    } else {
        $('#booktable tr').slice(row.index(), row.index() + rowspan).remove();
    }


    //reindex
    var newIndex = 0;
    $('#booktable tr').each(function(i) {
        if ($(this).children().first().attr('id') !== undefined) {
            newIndex++;
            $(this).children().first().text(newIndex);
        }
    });
});

【讨论】:

  • 非常感谢@dee.ronin... 删除行非常有效,但重新索引有些问题,请参考此链接fiddle.jshell.net/anusibi/s2bod97t/116
  • 我看到索引的行为发生了变化。更新代码以添加变量 newIndex。你可以试试更新后的代码。
【解决方案3】:

你可以使用rowspan的值来计算你要删除多少行$('td:first',row).attr('rowspan')-1

http://jsfiddle.net/VixedS/af0Ljbzt/

  $('body').on('click', '.remove', function(e){
    e.preventDefault();
    var row=$(this).closest('tr');
    row.nextAll().slice(0,$('td:first',row).attr('rowspan')-1).each(function(){
      $(this).remove()
    });
    row.remove();
    $('tr:has(".f")').each(function(i) {
        $('td.f',this).text(i+1);
    });
  });

【讨论】:

  • 最后……我们看到了太阳。
  • 如果我选择了已经有数据覆盖的员工并弄乱了结构:(@Vixed
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2020-10-25
  • 2016-07-29
  • 2010-10-29
相关资源
最近更新 更多