Get checkbox values in Datatable with paging

Asked

Viewed 1,122 times

2

I have a table with the plugin Datatables.

In every row of my table, I own a <input type="checkbox" /> . I have a button to enable/disable the items described in each table row.

Basically, I go through all the checkbox and sending them to another method that there does certain actions related to the database, irrelevant part of the question.

My problem starts when I use table pagination because going through the checkbox, I can only get the values of the ones on screen. From what I noticed, the Datatables not a simple hide() in the records of the other pages, he will assemble the tr of table according to the current page.

How can I proceed to independent of the page, get all the checkbox selected?

  • This bDestroy solution is quite useful. In fact, I solved my problem using this startup setting.I just don’t really understand serialize(), since only bDestroy solves the issue of pagination.

2 answers

1


Resolved as follows:

var table = $('#example').DataTable({
    "bDestroy": true,
    "bSort": false
});
var Check = table.$('input:checked').serialize();

I re-start loading the table example to be able to treat as an object DataTable, as with the serialize send all data to input:checked, data I deal with in my C# layer via Ajax.

  • 1

    Very good guy, helped me a lot!

0

My suggestion in this case is to use JSF... because it has a lot of sample code and in it you can create tables with ease.

for example:

In the code snippet below, you have a ManagedBean, bean named, and this Managedbean has a list of objects....

With that element h:dataTable you can in this simple code snippet assemble a table with all the elements of the list, using the values present in the attributes of each object.

Detail: as in the checkbox value is : #{item.checked}, your item object must have an attribute boolean checked with their respective getters and setters. Thus, when you mark/deselect the item, your attribute will be updated with the new value. Hence, when you need to know if the object was selected or not, just check this attribute.

<h:dataTable value="#{bean.list}" var="item">
    <h:column>
        <h:selectBooleanCheckbox value="#{item.checked}" />
    </h:column>
    ...
</h:dataTable>

JSF has several sites with examples of elements used, such as: Primefaces and Richfaces

For the construction of a Managedbean, see here to begin with!

Browser other questions tagged

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