How to get attribute of a cell in DataTable ?

How to get attribute of a cell in DataTable ?

ramnaresh437ramnaresh437 Posts: 3Questions: 1Answers: 0

*Below is my test case:

<td class=" text-center">
<a class="score" id ="typ"></a>
</td>


i want like below::::

<td class=" text-center" class="score" id ="typ"> </td>

Answers

  • kthorngrenkthorngren Posts: 21,361Questions: 26Answers: 4,956

    Use cell().node() to get the cell. Like this:
    http://live.datatables.net/kufepaja/1/edit

    Kevin

  • ramnaresh437ramnaresh437 Posts: 3Questions: 1Answers: 0

    Thanks for the response Kevin..

    But this is not what I wanted !

    I have a dynamic row and table.

    In that when I click on empty cell, I wanted the ID to be displayed.

    <td class=" text-center" class="score" id ="typ"> </td>

  • kthorngrenkthorngren Posts: 21,361Questions: 26Answers: 4,956

    In that when I click on empty cell, I wanted the ID to be displayed.

    Where do you want it displayed?

    You can create a click event handler, like this example and use jQuery to get the id of the clicked cell. You can use cell().data() to update that cell with the id if that is what you are wanting.

    Please build a simple running test case that show an example of what you have and provide details of what you want so we can offer more specific suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • ramnaresh437ramnaresh437 Posts: 3Questions: 1Answers: 0
    edited April 2022

    My Javascript****************

    function add_Row(pParent, pName, stabilityIdList) {
        //adding/removing row from datatable datasource
        //create test new record
        
        console.log(stabilityIdList);
            
        var aoCols = data_table.fnSettings().aoColumns;
        //debugger;
        var newRow = new Object();
        var nrow_cell = $('#example tr').length;
        var ncol_num = aoCols.length;
        if (ncol_num == 4) {
            return alert("Please add stability");
        }
        for (var iRec = 0; iRec < (aoCols.length); iRec++) {
            if (aoCols[iRec]._sManualType === "string") {
                switch (iRec) {
                    case 0:
                        newRow[aoCols[iRec].data] = pParent;
                        break;
                    case 1:
                        newRow[aoCols[iRec].data] = pName;
                        break;
                    case parseInt(aoCols.length - 2):
                        newRow[aoCols[iRec].data] = "";
                        break;
                    case parseInt(aoCols.length - 1):
                        newRow[aoCols[iRec].data] = "<a class='buttonRR'><i class='fas fa-trash-alt'></i></a>";
                        break;
                    default:
                                            
                        newRow[aoCols[iRec].data] = "This is my value ";
                      // newRow[aoCols[iRec].data] = "<a class='score' data-attr-id='" + pParent + "'></a> ";
                        //newRow[aoCols[iRec].DT_RowId] =  pParent ;
                        break;
                }
            }
        }
        results.splice(nrow_cell + 1, 0, newRow);
        data_table.fnDestroy();
        initDT();
        addDBClikHandler();
    

    I do not have the ID of the default element so that i can fetch the data from that attribute id.


    my HTML:


    <td class=" text-center">  // here I am only getting class, not the ID
    <div class="cellTextbox dx-show-invalid-badge dx-textbox dx-texteditor dx-show-clear-button dx-editor-outlined dx-texteditor-empty dx-widget" "=""><div class="dx-texteditor-container"><div class="dx-texteditor-input-container"><input autocomplete="off" id="parameter-rating" class="cellTextbox dx-texteditor-input" type="text" spellcheck="false" tabindex="0" role="textbox"><div data-dx_placeholder="" class="dx-placeholder"></div></div><div class="dx-texteditor-buttons-container"><span class="dx-clear-button-area"><span class="dx-icon dx-icon-clear"></span></span></div></div></div></td>
    

    my expectation:
    */***************************************

    <td class=" text-center" class="score" id ="typ"> </td>
    

    Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

  • kthorngrenkthorngren Posts: 21,361Questions: 26Answers: 4,956

    I would use jQuery to add the id to the td in your add_Row function. Maybe something like this:

    $( myCell ).attr('id', 'value');
    

    I don't believe there is anything built into Datatables for this. You can use columns.className to automatically add classes.

    Kevin

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    If the id information is available in the row's data, then use columns.createdCell to assign an id (or any other attribute) to the created node.

    Allan

Sign In or Register to comment.