How to add one more value to data array like DT_RowId

How to add one more value to data array like DT_RowId

MikluchoMaklayMikluchoMaklay Posts: 2Questions: 1Answers: 0

Hello team
As I see, it's possible to get an array of values for every row by using row().data()

var dtable = $('#' + src_table_id).DataTable( {
                            "lengthMenu": [ [10, 25, 50, 100, 500, -1], [10, 25, 50, 100, 500, "All"] ],
                            "pageLength": 25,
                            "ordering": false,
                            "dom": '<"wrapper"lfipt>', //'<"top"lfipt>rt<"clear">',
                            "search": {
                                "regex": true
                            },
                            "fnCreatedRow": function( nRow, aData, iDataIndex ) {
                                $( nRow ).attr('hash', <hash_value>);
                                //console.log("aData", aData, );
                            }
                        });
...
var dt_rows = dtable.rows('tr[id]');
...
dt_rows.every(function(){
                    var curr_row_data = this.data(); // $(this.node()).attr("hash")
...
                });
...

However row().data() method is return an array with the next structure : aData+ DT_RowId (attribute id in html)

this.data(): (4) […]
​0: "-9"
1: "19"
2: "17"
3: ""
DT_RowId: "19_17"
length: 4

<tr id="19_17" hash="1516110" role="row" class="odd">
   <td class="pkv numeric">-9</td><td class="pkv numeric">19</td><td class="pkv numeric">17</td><td></td>
</tr>

Could you please give me advice, is it possible to add one more value to the array - hash:<hash_value> like DT_RowId ?

Currently I have to use $(this.node()).attr("hash") to get hash value.
There is an option to include hash to aData, however this's definitely the data .
I'd like to avoid to mix data and hash and use this.data() option to get hash value from the array.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @MikluchoMaklay ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • MikluchoMaklayMikluchoMaklay Posts: 2Questions: 1Answers: 0

    Hello Colin

    I've created the test case here http://live.datatables.net/loboxegu/4/edit

    Unfortunately , DT_RowId: values aren't displayed in aData array in the site console, however they're displayed in web browser console(F12).

    As I understand correctly, there is no other options to include hash: values to array (to get it back via row().data() next) except to add it to aData array.
    Please clarify, maybe I'm wrong.

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    Hi @MikluchoMaklay ,

    Ah, I see - thanks for the test case, that helped. You can't get attributes with data() as you said, and as you also said, as it stands, you'll need to use row().node().

    The only other alternative is to create an additional hidden column in the table (columns.visible), which you insert the hash into with cell().data(). Other than that, you'll need to use the way you currently have (which IMHO is probably the tidiest).

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.