Combine datatables and a select option
Combine datatables and a select option

I want to combine a select box with information from selected rows in a datatables object. My idea was to:
- Get value of selected option ( item.id )
- get serial numbers of selected items in datatables object
- Create an array that consists of the item.id and the datatables serial [item.id, serial1, serial2 ...] ( changevalues )
- change the value of the option to the combined value from 3 so it gets transmitted when updateButton is clicked
I succeeded with 1-3 but I made some mistake with 4. I can write the output to the console though. Does someone have an idea for me? Is this a bad idea to do it like this at all?
my select code:
<div class="container">
<form action="/itemselect" method="post">
<div class="row">
<select class="selectpicker form-control" name="updates_list" id="itemSelector">
{% for item in items %}
<option value="{{ item.id }}">{{ item.name }}</option>
{% endfor %}
</select>
<button class="btn btn-default" name="submit" value="submit" id="updateButton" disabled > Submit</button>
</div>
</form>
</div>
my ajax call for the datatables update:
$(document).ready(function() {
function getData() {
$.ajax({
url: "/refreshLiveTableRoute",
success: function (data) {
// Get Datatable API
var table = $('#live_table').DataTable();
// Get IDs of selected rows.
var selected = table.rows({selected: true}).ids().toArray();
// Add '#' to the IDs of each row
selected.forEach(function(part, index) {
this[index] = '#' + this[index];
}, selected);
// Clear table.
table.clear();
// Add new row data, draw and stay on same page.
table.rows.add(data).draw(false);
// Reselect rows based on ID.
table.rows( selected ).select();
// this is to give the selected rows to the update button
table.on( 'select', function () {
var rowData = table.rows( { selected: true } ).data();
var rowSerial = [];
$.each( rowData , function(i, item) {
rowSerial.push(item.serial);
});
var e = document.getElementById("itemSelector");
var selectoption = e.options[e.selectedIndex].value;
var changevalue = [selectoption,rowSerial]
console.log(changevalue) // <--- works!
$("#itemSelector").change(function() {
$("#itemSelector option[value=selectoption]").val(changevalue);
} );
} );
});
}
});
}
This discussion has been closed.
Replies
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
I solved the problem thanks to a buddy of mine and I want to share the answer:
I created a Datatables variable:
var datatablesobject = $('#live_table').DataTable( { ...})
and checked if it's sate is selected and if, I attached the value from the select to it:in the HTML part I created an extra field that I filled in the start with the same value ("data-value-original") and I used the function above to change the original value to have the uid included - this way I could fetch the information after pressing the button.
hope that helps someone in the future.