Using a string value for DT_RowId

Using a string value for DT_RowId

northmoornorthmoor Posts: 10Questions: 5Answers: 0

I have a simple problem that I just cannot figure out how to solve. My test case shows it quite simply. If I populate my table using a numeric value for DT_RowId, I can later on retrieve rows using that row Id. As soon as I change it to a String value, it no longer works. Unfortunately, I need to use a String value as the row Id as it links to other aspects of my application, so I cannot easily change it to be numeric.

By changing the way the ID value is constructed to either numeric or non-numeric (as shown in my example below), it's easy to replicate the problem. I searched online extensively and I can't find anything that details that I am doing something wrong?

Thanks!

<html>  
    <head>  
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css">
        <script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.js"></script>
    </head>
    <body>
        <script>
            var mytable;
        
            $(document).ready(function () {
            
                    mytable = $('#myTable').DataTable({
                    
                        columns: [
                            {data: "Column1"},
                            {data: "Column2"},
                            {data: "Column3"},
                            {data: "Column4"},
                        ]
                    });         

                    for (i=0;i<100;i++) {
                    
                        //does not work
                        var id = "A" + i;
                        
                        //does work
                        //var id = i;

                        console.log("ID=" + id);
                    
                        mytable.row.add({
                            "DT_RowId":id,
                            "Column1":"Content1" + i,
                            "Column2":"Content2" + i,
                            "Column3":"Content3" + i,
                            "Column4":"Content4" + i}).draw();
                            
                        var rowId = mytable.row(id).id();
                        console.log("row ID =" + rowId);                            
                    }
                } 
            );
        </script>
        <table id="myTable" class="display">
            <thead>
                <tr>
                    <th>HEADER1</th><th>HEADER2</th><th>HEADER3</th><th>HEADER4</th>
                </tr>
            </thead>
        </table>
    </body>
</html> 

This question has an accepted answers - jump to answer

Answers

This discussion has been closed.