Note that there are some explanatory texts on larger screens.

plurals
  1. POMerging 4 similar click functions into one
    primarykey
    data
    text
    <p>Here are 4 functions I use to improve the usability of a table by:</p> <ol> <li>If cell contains a checkbox and you click outside of a checkbox</li> <li>The the <code>tr</code> contains <code>data-url</code> then the whole row is "clickable" with CSS hover effect and redirects on click.</li> <li>If the last column in the table contains a relative/absolute URL then also redirects on click.</li> <li>Check all checkboxes.</li> </ol> <p>Here's my code:</p> <pre><code>// 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 &amp;&amp; linkEl.attr('onclick') === undefined &amp;&amp; !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); }); </code></pre> <p><strong>Q:</strong> how can I modify this into one function so that jquery only has to search for tables once?</p> <p><a href="http://jsfiddle.net/g9uLV/" rel="nofollow">Example jsfiddle</a></p> <p>Thanks for every bodies suggestions I have ended up taking a slightly <a href="http://jsfiddle.net/g9uLV/11/" rel="nofollow">different approach</a>:</p> <pre><code>$('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 &amp;&amp; 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 &amp;&amp; (!linkElHref.length || linkEl.prop('protocol').indexOf("http") !== 0)){ e.preventDefault(); return false; } if (linkElHref &amp;&amp; linkEl.attr('onclick') === undefined &amp;&amp; !linkEl.hasClass("popme")) { document.location = linkElHref; } else { linkEl.click(); } }); } }); // end CELLS }); // end ROWS }); // end TABLE </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload