Select and save datatables dropdown selection
Select and save datatables dropdown selection
I have a node.js program that I am working on but have not been able to get my dropdown column to work properly. I query a sql server database and populate the datatable. The last column is my "Format" column with a dropdown list where the user can select a format number (1-10). I need to be able to save whatever format selection is made and update the database after. I haven't been able to even get the correct selection to console.log.
//Here is where I have tried to console.log the selection after a selection is made.
//I only get whatever value is selected in the first row, not the row that was actually changed.
$('#Slapper').on('change', function(){
var value = $('#Slapper option:selected').val();
console.log(value);
});
//Here is where I create the datatable. The dropdown list looks fine visually but doesn't function at all:
function createBarcode(data){
mainTable = $('#Slapper').dataTable({
data: data,
columns: [
{title: "Barcode ID",
data: "barcodeID",
orderable: true,
visible: false},
{title: "Print",
data: "printYesNo",
render: function ( data, type, row ) {
if ( type === 'display' ) {
printArray[row.barcodeID] = row.printYesNo;
// console.log(printArray);
return '<input type="checkbox" class="editor-print" id='+ row.barcodeID+' >';
}
return data;
},
orderable: true,
visible: true},
{title: "Code",
data: "codeText",
orderable: true},
{title: "Commodity",
data: "Commodity",
orderable: true},
{title: "Variety",
data: "Variety",
orderable: true},
{title: "Grade",
data: "Grade",
orderable: true},
{title: "Style",
data: "Pack",
orderable: true},
{title: "Size",
data: "Size",
orderable: true},
{title: "Is Active",
data: "active",
orderable: true,
visible: false},
{title: "Label",
data: "Label",
orderable: true,
visible: true},
{title: "PLU",
data: "Lot",
orderable: true,
visible: true},
{
title: "Format",
data: "Format",
render: function(d,t,r){
var $select = $('<select id=' + r.barcodeID + '></select>', {
'id': r +'start',
'value': d,
});
$.each(formats, function(k,v){
var $option = $('<option></option>', {
'text': v,
'value': v
});
if(d === v){
formatArray[r.barcodeID] = r.Format;
$option.attr('selected', 'selected')
}
$select.append($option);
});
return $select.prop('outerHTML');
}
},
],
Answers
It would be nice if you show the complete code, but by reading the desired behaviour and the partial code, it seems that you are assigning the
change
event to thetable
rather than theselect
.You can use the
createdRow
do assign thechange
event to theselect
on every row. This way you'll be able to get the correct "Format" for every row.Thanks for the reply, I will see if I can get that to work. Also, here is the complete javascript file.
var socket = io('localhost:82'),
mainTable,
printArray = [],
formatArray = [],
print,
editor;
formats = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
];
$(document).ready(function() {
});
socket.on('getBarcodeList', function(currentData){
console.log(currentData);
createBarcode(currentData);
})
socket.on('refreshJava', function(){
socket.emit('getBarcodeList');
socket.emit('updateXml');
})
function createBarcode(data){
mainTable = $('#Slapper').dataTable({
data: data,
columns: [
{title: "Barcode ID",
data: "barcodeID",
orderable: true,
visible: false},
{title: "Print",
data: "printYesNo",
render: function ( data, type, row ) {
if ( type === 'display' ) {
printArray[row.barcodeID] = row.printYesNo;
return '<input type="checkbox" class="editor-print" id='+ row.barcodeID+'>';
}
return data;
},
orderable: true,
visible: true},
{title: "Code",
data: "codeText",
orderable: true},
{title: "Commodity",
data: "Commodity",
orderable: true},
{title: "Variety",
data: "Variety",
orderable: true},
{title: "Grade",
data: "Grade",
orderable: true},
{title: "Style",
data: "Pack",
orderable: true},
{title: "Size",
data: "Size",
orderable: true},
{title: "Is Active",
data: "active",
orderable: true,
visible: false},
{title: "Label",
data: "Label",
orderable: true,
visible: true},
{title: "PLU",
data: "Lot",
orderable: true,
visible: true},
{
title: "Format",
data: "Format",
render: function(d,t,r){
var $select = $('<select id=' + r.barcodeID + '></select>', {
'id': r +'start',
'value': d,
};
So, yes,
createdRow
may help in this case, you'll also be able to access the row data.