0

以下是我用来提高表格可用性的 4 个函数:

  1. 如果单元格包含一个复选框并且您在复选框外部单击
  2. tr包含然后data-url整行是“可点击的”,具有 CSS 悬停效果并在点击时重定向。
  3. 如果表中的最后一列包含相对/绝对 URL,则在点击时也会重定向。
  4. 选中所有复选框。

这是我的代码:

// Click table cell auto check checkbox
$('table tr td').has("input[type=checkbox]").click(function(event) {
    // Onl call this if click outside of the checkbox itself
    if (event.target.type !== 'checkbox') {
        $(':checkbox', this).trigger('click');
    }
});

// Table row click 
$('table tr[data-url]').each(function(){
    var url = $(this).attr("data-url");
    if(url.length){
        $(this)
            .addClass("clickable")
            .find("td").click(function(){
                window.location = url;
                return false;
            }); 
    }
});

// Double click row, open edit link
$('table:not(.noDoubleClick) tr td').dblclick(function(e) {
    var linkEl = $(this).parents('tr').find('td:last-child a');
    var linkElHref = linkEl.attr('href');

    // Check if has href and http protocol
    if(!linkElHref.length || linkEl.prop('protocol').indexOf("http") !== 0){
        e.preventDefault();
        return false;
    }

    if (linkElHref && linkEl.attr('onclick') === undefined && !linkEl.hasClass("popme")) {
        document.location = linkElHref;
    } else {
        linkEl.click();
    }
});

// Check all checkboxes
$('table input[type=checkbox][name^="checkall"]').live("click",function() {
    var parent = $(this).parents('table');
    if(!$(this).parents('table').length){
        parent = $(this).parents("form");
    } 
    parent.find(':checkbox[name^="' + $(this).attr("data-name") + '"]').prop("checked", this.checked);
});

问:如何将其修改为一个函数,以便 jquery 只需搜索一次表?

示例 jsfiddle

感谢每个机构的建议,我最终采取了稍微不同的方法

$('table').each(function(){
    var $table= $(this), $cell, $row;

    // TABLE ROWS
    $table.find('tr').each(function(){
        $row = $(this);

        // Row click redirect to data-url
        var url = $row.attr("data-url");
        if(url && url.length){
            $row.addClass("clickable").find("td").click(function(){
                window.location = url;
                return false;
            });    
        }  

        // TABLE HEADING CELLS
        $row.find('th, thead td').each(function(){
            $cell = $(this);

            // Check all checkboxes
            $cell.find('input.checkall').live("click",function() {
                var parent = $(this).parents('table');
                if(!$(this).parents('table').length){
                    parent = $(this).parents("form");
                }
                parent.find(':checkbox[name^="' + $(this).attr("data-name") + '"]').prop("checked", this.checked);
            });
        });

        // TABLE CELLS
        $row.find('td').each(function(){
            $cell = $(this);

            // Check checbox onClick
            if($cell.find("input[type=checkbox]")){
                $cell.click(function(e) {
                    if(e.target.type !== 'checkbox') $(':checkbox', this).trigger('click');
                });
            }

            // Double click open edit link
            if(!$table.hasClass("noDoubleClick")){
                $cell.dblclick(function(e){
                    var linkEl = $(this).parents('tr').find('td:last-child a');
                    var linkElHref = linkEl.attr('href');

                    // Check if has href and http protocol
                    if(linkElHref && (!linkElHref.length || linkEl.prop('protocol').indexOf("http") !== 0)){
                        e.preventDefault();
                        return false;
                    }

                    if (linkElHref && linkEl.attr('onclick') === undefined && !linkEl.hasClass("popme")) {
                        document.location = linkElHref;
                    } else {
                        linkEl.click();
                    }
                });
            }          
        }); // end CELLS                 
    }); // end ROWS
}); // end TABLE
4

3 回答 3

2

您应该使用 .on , .live 已被弃用:

  $(document).on('click', 'table tr td', function(event)
   {
       if( $(this).has("input[type=checkbox]"))
       {
            if (event.target.type !== 'checkbox') 
                $(':checkbox', this).trigger('click');
       }
    });

    // Table row click             
    $(document).on('click', 'table tr[data-url] td', function(e)
    {
        var url = $(this).parent().attr("data-url");
        if(url.length)
        {
           window.location = url;
           return false;
        }
    });


    $(document).on('dblclick', 'table:not(.noDoubleClick) tr td', function(e) {
        debugger;
        var linkEl = $(this).parents('tr').find('td:last-child a');
        var linkElHref = linkEl.attr('href');

        // Check if has href and http protocol
        if(!linkElHref.length || linkEl.prop('protocol').indexOf("http") !== 0){
            e.preventDefault();
            return false;
        }

        if (linkElHref && linkEl.attr('onclick') === undefined && !linkEl.hasClass("popme")) {
            document.location = linkElHref;
        } else {
            linkEl.click();
        }
    });

    // Check all checkboxes
    $(document).on('click', 'table input.checkall', function() {
        var parent = $(this).parents('table');
        if(!$(this).parents('table').length){
            parent = $(this).parents("form");
        } 
        parent.find(':checkbox[name^="' + $(this).attr("data-name") + '"]').prop("checked", this.checked);
    });​
于 2012-10-11T14:38:38.867 回答
1

我在这里做了基本的存根,我不想重写你所有的代码。

  $(document).ready(function(){
    $('table').each(function(){
        var table = $(this);
        table.find('tr').each(function (){
            var tr = $(this);
            tr.find('td').each(function(){
                var td = $(this);
                td.has("input[type=checkbox]").click(function(event) {
            // Only call this if click outside of the checkbox itself
                    if (event.target.type !== 'checkbox') {
                        $(':checkbox', this).trigger('click');
                    }
                });
            });
        });
    });
});
​

逻辑是:查找所有表,遍历所有 tr,然后遍历 td。我已经完成了您的第一个功能,并希望能解释如何使用它?

​</p>

于 2012-10-11T14:30:02.823 回答
1

在这种情况下,最好的办法是:

  1. 获取页面中的所有表格
  2. 循环遍历每个表
  3. 根据需要查找并将逻辑应用于各个元素
$('table').each(function(){
   var table = $(this),
       rows  = table.find('tr[data-url]'),
       cells = table.find('td'),
       all   = table.find('input[type=checkbox][name^="checkall"]'),
       edit  = table.is('.noDoubleClick');

   cells.each(function(){
      //Apply your logic here
      if(edit === true){
         //Apply your logic to this cell here
      }
   });
   rows.each(function(){
      //Apply your logic to this row here       
   });
   all.on('click',function(){
       //Apply your logic here
   });

});
于 2012-10-11T14:51:02.023 回答