Selecting a checkbox and not sorting the dataTables

Asked

Viewed 823 times

3

I’m using the dataTables and in one of the columns I placed a input:checkbox for the user to be able to mark all columns or uncheck, remembering that in the columns I have to sort, and in this column I also have this, because I need to know which items are already marked or not. So far everything is working.

What I’m not sure how to do is that when the user clicks on checkbox do not sort, only do this sort when you click on the column itself. Today, when he clicks to mark or uncheck already makes the ordering, and that can’t happen.

inserir a descrição da imagem aqui

The snippet I create of html is as follows::

<table id="datatables">
    <thead>
        <th>
            <tr>Tema</tr>
            <tr>Monitoramento</tr>
            <tr><input type="checkbox"></tr>
        </th>
    </thead>
    <tbody>
        <tr>
            <td>Alimentos</td>
            <td>monitoramento alimentos 2</td>
            <td><input type="checkbox"></td>
        </tr>
        <tr>
            <td>Alimentos</td>
            <td>monitoramento alimentos 3</td>
            <td><input type="checkbox"></td>
        </tr>
    </tbody>
</table>
  • would you please post some snippet of your html code from the datatables?

  • I already edit the question, but already commenting that it is an html of a common table.

2 answers

2

Marcelo, I imagine the problem here is that the event click propagates to the parent element of input, to which dataTables ties an Event Listener to re-sort. So I think the important thing here is to use the .stopPropagation()

$('table th input[type=checkbox]').on('click', function (e) {
    e.stopPropagation();
});

Example

Note that your table syntax is incorrect. The descendant of <thead> is <tr> and then <th> not the other way around.


Edit:

In the case of anti-user browsers test like this:

$(document).ready(function () {
    $('#datatables').dataTable();
    $('table th input[type=checkbox]').on('click', function (event) {
        event.stopPropagation ? event.stopPropagation() : event.returnValue = false;
    });
});

Example

  • There is only one problem when using stopPropagation(), it only runs on IE9, would you tell me if you have something to work on the lower ones, because unfortunately you still have a lot of user and client with these old browsers. Still thank you very much

  • @Marcelodiniz, I edited the answer with an addition. I tested on IE8 and worked well

  • Here where I am I have no IE to test, but I imagine it works like you said. I don’t know, but I saw this link http://stackoverflow.com/questions/20045162/event-returnvaluis-deprecated-please-use-standard-event-preventdefault which says that this return is deprecated. More to the point. Valew

  • @Marcelodiniz, take a look at Rafael’s response. If the plugin already supports seems to me the best way.

  • posted a comment explaining why pq can’t be. But still Valew.

2

Datatables itself has solution for this, using column definitions.

I do like this:

     "aoColumnDefs": [
  { "bSearchable": false, "aTargets": [ 7 ] },

This has to be done in the javascript that calls the datatables.

link to parameter documentation. http://www.datatables.net/ref#bSearchable

  • 1

    So I even use this parameter in some tables of mine, but as I said, in this column I need to sort to know which items are marked or not.

  • 2

    Hello Marcelo, sorry I didn’t take into consideration that you still needed to order the column. According to this link http://datatables.net/forums/discussion/11872/is-it-possible-to-sort-only-by-clicking-the-sorting-arrows/p1 from the datatables forum you can try (in addition to stop Propagation) is to give unbind on the added Listener Event on DT startup and then add the fnSortListener manually and choose a div that will sort the text and leave the checbox out of that div. But I’ve never tried this and also can’t tell if the datables will know how to sort by selected and not selected.

  • No problem @Rafael . I haven’t seen this in the forum yet, I’ll take a look. Valew

Browser other questions tagged

You are not signed in. Login or sign up in order to post.