Select and save datatables dropdown selection

Select and save datatables dropdown selection

austin5577austin5577 Posts: 2Questions: 1Answers: 0

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

  • jvretamerojvretamero Posts: 26Questions: 0Answers: 3

    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 the table rather than the select.

    You can use the createdRow do assign the change event to the select on every row. This way you'll be able to get the correct "Format" for every row.

  • austin5577austin5577 Posts: 2Questions: 1Answers: 0

    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.emit('getBarcodeList');
    socket.emit('updateXml');
    
    
    $('#updateButton').click(function(){
        console.log('updateButton pressed');
        socket.emit('updateXml');
        mainTable.fnDestroy();
        socket.emit('getBarcodeList');
    });
    $('#cancelButton').click(function(){
        socket.emit('sendCancel');
        console.log('Cancel pressed');
    });
    
    $('#Slapper').on( 'change', 'input.editor-print', function () {        
          socket.emit('updatePrint', print = { printA: printArray[this.id], id: this.id } );                 
    });
    $('#Slapper').on('change', function(){
          var value = $('#Slapper option:selected').val();
          console.log(value);
     });
    

    });

    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,

            });
            $.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');
        }
      },  
    
    ],
    rowCallback: function ( row, data ) {
            // Set the checked state of the checkbox in the table
            $('input.editor-print', row).prop( 'checked', data.printYesNo == 1 );
        }
     });
    

    };

  • jvretamerojvretamero Posts: 26Questions: 0Answers: 3

    So, yes, createdRow may help in this case, you'll also be able to access the row data.

This discussion has been closed.