【问题标题】:How to permute 2 cells of a table如何置换表格的 2 个单元格
【发布时间】:2013-02-27 23:52:09
【问题描述】:

我想做一个小函数,用 jQuery(或 PHP/Ajax)置换表格的 2 个单元格。 假设我有下表:

<table class="table">
    <tr>
        <td class="btn" data-cellN="1">
            01
        </td>
        <td class="btn" data-cellN="2">
            02
        </td>          
    </tr>

    <tr>
        <td class="btn" data-cellN="3">
            05
        </td>
        <td class="btn" data-cellN="4">
            06
        </td>
    </tr>
</table>   

如何选择 2 个单元格并排列它们?

编辑:

不过,多亏了您的建议,我才完成了这个功能。这是第一次出现错误我从 c1 c2 c3 变量中得到一个奇怪的错误,我不知道为什么, 谁能帮我完成我的功能吗?

这是我的 Javascript:(它已包装在准备好的文档中)

        var nbbuttonclicked=0; // to trigger the movecell function after the second button was clicked
        var count=0; //Number of tinme we moved a cell

        var table = {

            c1:null,
            c2:null,
            c3:null,

            check: function(){
                $('td').on('click', function(){ //When the user click on a button we verify if he already clicked on this button

                    if( $(this).hasClass('btn-info') ) {

                        //If yes, then remove the class and variable c1
                        $(this).removeClass('btn-info');
                        table.c1=0;
                        nbbuttonclicked--;

                    }else{
                        //if no, then add a class to show him it is clicked and remember the value of the cell
                        $(this).addClass('btn-info');
                        if( typeof table.c1 === 'undefined' || table.c1===null) {
                            table.c1 = $(this);
                          console.log('c1:'+table.c1.html());
                        }else{
                            table.c2 = $(this);
                          console.log('c2:'+table.c2.html());
                        }

                        nbbuttonclicked++;

                        if( nbbuttonclicked==2 ) {
                            table.movecell(table.c1,table.c2); // we trigger the function to permute the values
                        }
                    }
                });
            },

            movecell: function(c1, c2){
                //We reset that variable for the next move
                nbbuttonclicked=0;

              //we move the cells
              table.c3=c1.html();
              c1.html(c2.html());
              c2.html(table.c3)

              c1.removeClass('btn-info');
              c2.removeClass('btn-info');
              table.c1=table.c2=table.c3=c1=c2=null;

            },

            // movecell: function(c1, c2){
                // We check if the cells.html() are = to data-cellN. If yes, you won
                // foreach()
            // },

            // var row = $('table tr:nth-child(1)')[0];
            // moveCell(row, 0, 3);
        };
        table.check();

这是一个示例: http://jsbin.com/exesof/2/edit

【问题讨论】:

  • 您的意思是当您更改表值时,它也会影响到数据库?
  • 我认为他的意思是合并它们,但不确定xD
  • 您的 HTML 标记无效。
  • 我刚刚修复了它 xD 让我们看看他是否接受编辑
  • 感谢编辑人员,是的,我想替换/置换相关 td 的值

标签: javascript php jquery html-table cell


【解决方案1】:

试试这个代码:

var cell0,
    cell1,
    next,
    row0,
    row1;

$('td').on('click', function() {
  if(!cell0) {
    cell0 = this;
    row0 = cell0.parentNode;
    return;
  } else if(!cell1) {
    cell1 = this;
    row1 = cell1.parentNode;

    // === Switch cells ===
    next = cell0.nextSibling;
    row1.insertBefore(cell0, cell1);
    row0.insertBefore(cell1, next);
    // ====================

    row0 = row1 = cell0 = cell1 = next = null;
  }
});

您可以在这里尝试的工作示例:http://jsbin.com/irapeb/3/edit

单击其中一个单元格,然后单击另一个单元格,然后查看它们是否已切换:)

【讨论】:

  • thx 伙计,但我意识到我不需要置换 td 而只需要置换它们的值,因此当我有正确顺序的数字(1234 ...)时,它会触发一个事件.所以我想简单地改变 td.html 的值。看看我的代码,我是用 jquery 做的,但是由于某种原因,我在第一个之后的排列出现了一个错误。你知道为什么会这样吗?
  • 你不能使用this.movecell,因为它会在jQuery回调函数的范围内被调用,在这种情况下this将是window。您的代码有一个错误 - 您将 movecell 的参数称为与全局变量相同的参数,因此您取消了这些参数而不是全局变量。
  • 就是这样。但是我不明白当我从这个movecell: function(c1,c2) 获得 c1 和 c2 时会发生什么,它们是这里的全局变量还是 fct 内部的?我刚刚再次编辑了我的答案
【解决方案2】:

此代码会将第 4 个 td(引用为 cell[3])移动到第一行中第一个单元格 (cell[0]) 的位置(第 1 个变为第 4 个):

function moveCell(row, c1, c2) {
    var cell = row.cells;
        arr = [c1, c2].sort(),
        c1 = arr[1],
        c2 = arr[0];
    row.insertBefore(row.insertBefore(cell[c1], cell[c2]).nextSibling, cell[c1].nextSibling);
}

var row = $('table tr:nth-child(1)')[0];
moveCell(row, 0, 3);

http://jsfiddle.net/2N2rS/3/

我写了这个函数来简化过程。它使用本机表方法。

修改表格单元格的行数 (:nth-child(1)) 和索引,您可以根据需要对其进行置换。

【讨论】:

  • 谢谢,我更熟悉 jQuery。有没有办法用 jQuery 做到这一点?
  • 在这种情况下,jQuery 不会提供很多优势。在表中选择必要的行并将其与td 索引一起传递给我编写的函数。
  • 知道了,我在做$('td').on('click',function() { //move cell }); 但是如何等待第二个按钮被点击?谢谢
  • 按钮在说什么?
【解决方案3】:

使用 jQuery,你可以这样做:

$(document).ready(function() {

    $("table.table tbody tr td:first-child").each(function(index, element) {
    // this and element is the same    
    $(this).insertAfter($(this).next());
    });

});

假设您没有省略 tbody 元素(某些浏览器会自动将其添加为默认行为)

working example

如果您想在单击第一个单元格时执行该过程,您只需使用带有单击事件的on 而不是每个。

$(document).ready(function() {

    $("table.table tbody tr td:first-child").on('click', function(clickEvent) {
        $(this).insertAfter($(this).next());
    });

});

【讨论】:

  • 谢谢,点击时我需要选择一个单元格,所以我想我会使用 onclick 或 on('click') 但我真的不知道如何
【解决方案4】:

这可以在纯 javascript 中完成:

  1. 获取点击销售计数;
  2. 获取两个单元格的数据,当它们被选中时;
  3. 将单元格值替换为相反的值;
  4. 通过 ajax 调用在 DB 中存储新值。

现在由你来编写脚本。

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2021-11-29
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 2012-09-09
    • 2011-09-22
    相关资源
    最近更新 更多