1

I'm trying to make clickable table rows work with the jquery selectable function, but I'm running into difficulties. It works great with li elements, but as soon as I try to use a table, the click event quits working. Drag to select works, but I really need to be able to click as well. Here is my code:

$(function() {
  $("#selectable").selectable();
});
#feedback {
  font-size: 1.4em;
}
#selectable .ui-selecting {
  background: #FECA40;
}
#selectable .ui-selected {
  background: #F39814;
  color: white;
}
#selectable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 100%;
}
#selectable tr {
  margin: 0px;
  padding: 0.4em;
  font-size: 1em;
  height: 10px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->

<table id="selectable" style="width:100%">
  <tr class="ui-widget-content">
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr class="ui-widget-content">
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

Does anyone know how to get click or ctrl + click working in the above example?

0

2 Answers 2

2

You need to add a filter to the selectable().

Please see the documentation for filter.

I have updated your example, it's just a small change:

$(function() {
  $("#selectable").selectable({filter: 'tr'});
});
#feedback {
  font-size: 1.4em;
}
#selectable .ui-selecting {
  background: #FECA40;
}
#selectable .ui-selected {
  background: #F39814;
  color: white;
}
#selectable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 100%;
}
#selectable tr {
  margin: 0px;
  padding: 0.4em;
  font-size: 1em;
  height: 10px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->

<table id="selectable" style="width:100%">
  <tr class="ui-widget-content">
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr class="ui-widget-content">
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

5 Comments

this is not answering the OP question. It's just a bunch of code, please add some explanation on how this addresses OP concerns, what problem there was, and how you solved it.
@PA. it's not a "bunch of code", it's one small change, namely {filter: 'tr'} in order to make rows selectable. It solves OP's question completely.
@PatrickRoberts to be fair, PR posted this before I added the three lines above the code.
@mpf82 I looked at the edit history -- your initial answer was perfectly sufficient imo. +1
Both this answer and the one below were correct, the filter did indeed get this working. I guess it's only fair to give it to the first response.
1

use a filter for your case. http://api.jquery.com/filter/

$(function() {
  $("#selectable").selectable({filter: 'tr'});
});
#feedback {
  font-size: 1.4em;
}
#selectable .ui-selecting {
  background: #FECA40;
}
#selectable .ui-selected {
  background: #F39814;
  color: white;
}
#selectable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 100%;
}
#selectable tr {
  margin: 0px;
  padding: 0.4em;
  font-size: 1em;
  height: 10px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->

<table id="selectable" style="width:100%">
  <tr class="ui-widget-content">
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr class="ui-widget-content">
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.