1

I am using datatable jquery.I want to use Sr_no in data-id of td tag but How to get data of Sr_no.

or you can say that I want particular data from data reponse

I have highlighted in code

$("#fir").dataTable({
    "bProcessing": true,

    "sAjaxSource": "paid.php",
    "sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>",
    "sPaginationType": "full_numbers",
    "aoColumns": [

        {"mData": "Sr_no"}, <------ I dont want to add this column in table but I want to use value
        {"mData": "Category_Name"},
        {"mData": "Vendor_Name"},
        {"mData": "date"},
        {"mData": "Price"},
        {"mData": "Payment_Mode"},
        {"mData": "remark"},
        {"mData": "edit"},
        {"mData": "cancel"}
    ],
    'columnDefs': [
        {
           'targets': 1,
           'createdCell':  function (td, cellData, rowData, row, col) {
               console.log(row,col);
              $(td).attr('data-id',Sr_no);  <----------------
           }
        }

     ]

});



4
  • What version of DataTable are you using? Commented Apr 29, 2022 at 10:14
  • datatable version --1.11.5 Commented Apr 29, 2022 at 10:21
  • I just need to Sr_no in data-id attribute so I can identify using selector but I dont know how to use response data in td data-id attribute Commented Apr 29, 2022 at 10:24
  • If you want this to make changes to the rendered HTML, then you're probably not using datatables fully. eg clicking a button on a datatable should be able to give you all the data for that row via the datatables api, not just what's been rendered. Commented Apr 29, 2022 at 10:31

1 Answer 1

2

You can use rowData.Sr_no to get the value of Sr_no for each row. Change targets: 1 to

{
   'targets': 1,
   'createdCell':  function (td, cellData, rowData, row, col) {
       console.log(row,col);
      $(td).attr('data-id', rowData.Sr_no);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.