How can I make a number that I enter actually be a number

How can I make a number that I enter actually be a number

jgw1jgw1 Posts: 1Questions: 1Answers: 0

I've spent hours searching the documentation and forums but cant find the answer. When I enter a number into a cell it seems to be stored as a character value. When I use .toArray() to get the data from the table I can see that other values in the column are numeric, but the one I entered is character. This causes other things in my app to fail later.

Here is some of my code that builds the table.

        // use it in an editable grid
        editor = new $.fn.dataTable.Editor( {
            data: res.sorted,
            table: '#sorted',
            paging: false,
            idSrc: 'NAME', // row id is either DT_RowId or what is set with this option
            fields: [
                { name: "TIME" ,label:"Time"},
                { name: "NAME" ,label:"Name"},
                { name: "SEX2",label:"New Sex",
                    type: "select",
                    options: [
                    {label: "Male", value:"M"},
                    {label: "Female", value:"F"},
                    ]
                    },
                { name: "AGE2", label:"New Age" },
                { name: "HEIGHT2",label:"New Height" },
                { name: "WEIGHT2" ,label:"New Weight"}
            ]
        } );    

        $('#sorted').DataTable( {
            dom: "Bfrtip",
            data: res.sorted,
            paging: false,
            order: [[ 1, 'asc' ]],
            columns: [
                { data: "TIME" },
                { data: "NAME" },
                { data: "SEX2" },
                { data: "AGE2" },
                { data: "HEIGHT2" },
                { data: "WEIGHT2" }
            ],
            keys: {
                columns: ':not(:first-child)',
                keys: [ 9 ],
                editor: editor,
                editOnFocus: true
            },
            select: {
                style:    'os',
                selector: 'td:first-child'
            },
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor }
            ]
        } ); // end #after.datatable

When I use the following code to look at the array of data from the table, I can see that the number I entered is now a text value. How can I make numbers I enter be numeric values?

    var table = $('#after').DataTable();
    var plainArray = table
        .data()
        .toArray();
        console.log("==> plainArray, showing table content");
        console.log(plainArray);

Here is some of the data shown in the console. Age2 has the number I entered, and you can see it is character. Please help.

AGE:13
AGE2:"33"
HEIGHT:56.5
HEIGHT2:56.5
NAME:"Alice"
SEX:"F"
SEX2:"F"
WEIGHT:84
WEIGHT2:84

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Use preSubmit to effectively cast the input as a number (since it is coming from an HTML input element it is always going to be read as a string:

    editor.on( 'preSubmit', function ( e, data, action ) {
      $.each( data.data, function ( key, values ) {
        data.data[ key ][ 'AGE2' ] = parseInt( values[ 'AGE2' ], 10 );
      } );
    } );
    

    Allan

This discussion has been closed.