Get/Set Table data from id

Get/Set Table data from id

JenDJenD Posts: 8Questions: 3Answers: 0

I am using Editor, so each row has an id. I can print out the id as part of the create event, so I have the id, but can't figure out how to pull the row data back (and once i do, will want to set the data in the table for a column). i want to pull the data back outside of the create event once i store the id.

Say my row id was "1234566777"

I have tried both
table.row("#1234566777").data() and table.row("#1234566777").node(), but both come back as undefined. Is there a method to call to get this info?

thanks,

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited February 2022

    Sounds like you need to set the Datatables option rowId to rowId: 'id' or whatever the ID object is for the row. See the string ID example of the row-selector docs.

    Kevin

  • JenDJenD Posts: 8Questions: 3Answers: 0

    sorry, don't understand that. I use string ID for the row id. i get the row id fine -- the question is once I have a valid row ID, how do I pull back the content in that specific row?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    how do I pull back the content in that specific row?

    Where are you trying to pull it back from? Please post your relevant code with a description of what you are trying to do and where the problem is happening.

    Kevin

  • JenDJenD Posts: 8Questions: 3Answers: 0

    editor.on( 'create', function ( e, json, data, id ) {

        console.log("ID = " + id);   // shows the id fine. , for example "ID = 16455421175110"
    
        // do some stuff...
    
        // testing pulling row data back from id (I won't do this in this function ultimately, but testing in here for now)
    
        // say I have a column of "email" and then the id.
        // trying to figure out how to get/set the "email" and "id" columns for this row.
    
        email_Value = table.row("#" + id).data("email"));   //  this doesn't work
        id_Value = table.row("#" + id).data("id"));  // this doesn't work
    
        console.log("EMAIL for this row is " + email_value);
        console.log("ID for this row is " + id_value);
    

    } );

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    // shows the id fine.

    This id is derived from the Editor's idSrc option.

    table.row("#" + id)

    The Datatables rowId sets the id property of the tr. This is the id used by using "#" + id as the row() selector. Have you defined rowId in your Datatables config?

    I created a simple test case showing your above method works:
    http://live.datatables.net/guwafemu/248/edit

    If its a timing issue maybe use postCreate.

    Kevin

  • JenDJenD Posts: 8Questions: 3Answers: 0

    ah ok, thank you for the example. I was missing " rowId: 'name', in my table. I had idSrc in my Editor.

    I was able to also then set/update a column in that row I pulled back from the ID, using some code I found here:

            const dtrow = table.row("#" + id);
            const field1Cell = table.cell(dtrow, 5);
            field1Cell.data('New Info');
    

    My only open question is how do I update the row id value for that row (dtrow)?

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited February 2022

    If its a timing issue maybe use postCreate.

    Yes, it is a timing issue. This is an example from my own coding using "submitSuccess" which you can use as well if you specify the correct action.

    editor.on( 'submitSuccess', function( e, json, data, action ) {
       //after submitting the contract the submitted contract 
       //needs to be selected automatically 
        if ( action === 'create' &&
             typeof json.data[0] !== 'undefined'  ) {
             var selectedId = '#' + json.data[0].DT_RowId; //"#row_99"
             setTimeout(function () {
                 table.row(selectedId).select();
             }, 500);
         }
    });
    

    If you just need the id itself you could do:

    var id = json.data[0].DT_RowId.substr(4);
    

    After selecting a row, e.g. for editing you can use this instead:

    var id = table.row({selected: true}).id();    or
    var id = table.row({selected: true}).id(true);
    

    The latter prepends a # to the start of the row id.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I was missing " rowId: 'name', in my table

    Not sure what you set this to but likely you will want it the same as your idSrc value.

    My only open question is how do I update the row id value for that row (dtrow)?

    Not sure I understand the question. Whatever id you set using rowId should automatically be updated, assuming the created data returned from the server has that object property.

    Kevin

  • JenDJenD Posts: 8Questions: 3Answers: 0

    Sorry, what I mean on row ID, is I want to update the value of the rowid attribute for a specific row. so say my row comes back as "1113434343". I have the row and want to update the row id to "XYZ".

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    I will assume the property for your row id is id. Use the row().data() API to update the id value. For example:

    var row = table.row( '#1113434343' );  // Get the row
    var data = row.data();  // Get the row data
    data.id = 'XYZ';  //. Update the id property
    
    row.data( data ).draw();  // Update the row data and redraw
    

    Make sure the id you are updating to is unique on the page.

    Kevin

  • JenDJenD Posts: 8Questions: 3Answers: 0

    works great, thanks for your help

Sign In or Register to comment.