row().id() null handling

row().id() null handling

blabablabablabablaba Posts: 47Questions: 19Answers: 0
edited November 2019 in Free community support

Hello,

Referring to documentation: row().id()

It states:

If the row does not have an id available undefined will be returned

I intentionally have some rows with null DT_RowId. I want to set the field 'dir_ID' to null. I have used the code below:

// Append a folder
$('#adminDt').on('click', 'button.editorAppend', function (e) {

    e.preventDefault();

    console.log(table.row($(this).closest('tr')).id())

    editor.field('dir_ID').def( table.row($(this).closest('tr')).id() )

    editor.create( {
            title: 'Append directory',
            buttons: 'Append'
    } )

} )

Unexpectedly I get null though the docs suggest undefined should be returned.

The result is that null is sent to the server, which is undesirable.

I would like to send null to the server.

Any advice appreciated!

Steve

This question has an accepted answers - jump to answer

Answers

  • blabablabablabablaba Posts: 47Questions: 19Answers: 0

    A workaround is to use DT_RowId accessed from row().data()

        editor.field('dir_ID').def( table.row($(this).closest('tr')).data().DT_RowId )
    

    DT_RowId provides a genuine null instead of "null" or undefined

    Should row().id() provide null ?

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    If the row does not have an id available undefined will be returned

    I think what that is saying is if Datatables doesn't have a column used is the row ID then it will return undefined. As shown in this example:
    http://live.datatables.net/wobakude/1/edit

    However if you have a column identified as the row ID then it will return the value in the column. If you have null instead of null in the data then that is what will be returned. You could investigate what happens to the null value from the DB and why Datataables has it as null. My guess is the JSON parser in your server is converting it.

    Kevin

  • blabablabablabablaba Posts: 47Questions: 19Answers: 0

    Thank you Kevin and for the live example demonstrating undefined.

    I'm unsure why the following results in null (in console)

    table.row($(this).closest('tr')).id()
    

    While the following (based on same data) results in null (in console)

     table.row($(this).closest('tr')).data().DT_RowId 
    

    Given the two results are different in console on the client, I don't think it's a server issue.

    But I do have a way forward :-) Thank Kevin

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    Answer ✓

    Given the two results are different in console on the client, I don't think it's a server issue.

    I think you are right. I believe it has to do with automatic type detection as described in columns.type. The default type is string. I think that is what happens to the null is it gets converted to a string by the type detection process.

    Kevin

This discussion has been closed.