Return IDs of selected rows to server
Return IDs of selected rows to server

I use Datatables with MySql. In my index view, I display all posts from a table and I can start to filter/sort and I get a remaining selection of rows left.
In this view, I want a button - "Post selection" - that sends the ID's of all selected rows back to the server.
Let's say i have a table cars:
id
brand
color
populated like this:
1 Mazda Brown
2 Buick Red
3 Ford Red
Then I filter out all red cars and hit the button "Post selection"
I now want to recieve an array like: {2,3} in my controller
How can I achieve this?
Answers
First you would tell Datatables which column you want to use for the row ID using
rowId
. In your button click event you would userows().ids()
to get the row IDs and use thetoArray()
API to place them in an array. In order to get the IDs of the rows in the search result you would use theselector-modifier
of{search: 'applied'}
.Here is a simple example based on your example data:
http://live.datatables.net/jehiqafu/1/edit
Kevin
Thanks a lot! It looks great.
After the click, I like to do a redirect to a PHP controller that saves the array to a database and then display another page. Something like:
$('button').on('click', function () {
The PHP Controller:
function another_page()
{
$array = $_POST(ids);
// Do whatever with the array...
$this->load->view('another_page');
}
However, this doesn't work.
What is happening?
Do you get errors in your browsers console?
Are you getting the IDs correctly?
Kevin