In a site that features data tables with lots for numbers I've added a helper function to select a given table so the user can copy data to clipboard and paste it in his favourite spreadsheet software. Please note that my function does not copy anything to clipboard, it simply highlights the appropriate HTML elements (i.e., the full <table>
). This part is working fine.
I'm now implementing a feature request: images inside the table should not get copied along. My first idea was to just hide them when I select the table and, again, that's working fine. But I'd like to show them back when the table is no longer selected and there's my problem: I'm not sure about how to accomplish that part.
I've composed a fiddle with a simplified example. A typical table looks like this:
<div>
<table class="data">
<tr>
<th>Table</th><td>#1</td><td><img src="/img/logo.png"></td>
</tr>
</table>
<input type="button" class="select" value="Select">
</div>
It can be selected by two means:
- The "Select" button below
- Double-clicking on the table itself
... and the code to accomplish it is:
jQuery(function($){
$("body").on("dblclick", "table.data", function(){
selectTable($(this));
return false;
});
$("body").on("click", ".select", function(){
selectTable($(this).siblings("table.data:first"));
return false;
});
$(document).on("click", noTableSelected);
});
function selectTable($table){
$table.find("img").hide();
selectNode($table[0]);
}
function noTableSelected(){
$("table.data img").show();
}
... where selectNode()
takes care of creating a text range and selecting it.
I need help with the $(document).on("click", noTableSelected);
bit. Apart from being an overkill, it also triggers when user makes use of the context menu to reach the "Copy to clipboard" command. I've also tried capturing the blur
event on the table but such event does not trigger when I thought it would (I guess it's useless when not dealing with form controls).
Any idea to launch noTableSelected()
when applicable? Or, is there a better way to avoid copying images to clipboard?