1

I have done a datatable and custom it's data but when i click the button on the first column, it only works on the first page and cannot click on other forward pages.

$('#tbl').DataTable( {
   responsive: true,
   data: data1,
   autoWidth: false,
   "order": [[ 7, "asc" ]],
   "iDisplayLength": 5,
   "pagingType": "full_numbers",
   "dom": '<"top">rt<"bottom"p><"clear">',
   "oLanguage": {
        "sEmptyTable":     "Not Record"
    },
    "columnDefs": [
        { "visible": false,  "targets": [ 6,7,8 ] }
    ],
    "columns": [
        {},{"sClass": "dt-body-justify"},{},{},{},{},{},{},{},{}
    ]
} );

BUT when for the click function in live mode, it still cannot work

$('#tbl tbody tr #edit_current_product').delegate('a', 'click', function () {
    ....... 
} );

1 Answer 1

1

id's must be unique. We dont know your markup but

$('#tbl tbody tr #edit_current_product').delegate('a', 'click', function () 

seems utterly wrong. Either you have multiple <a>'s with the same id #edit_current_product or the right thing actually happens, you have paginated away from the page where #edit_current_product is present.

I guess that what you really want is

$('#tbl').on('click', 'tbody tr a', function() 

or use a class instead of an id

$('#tbl').on('click', 'tbody tr .edit_current_product', function() 

BTW, why

"columnDefs": [
    { "visible": false,  "targets": [ 6,7,8 ] }
],
"columns": [
    {},{"sClass": "dt-body-justify"},{},{},{},{},{},{},{},{}
]

you just need

"columnDefs": [
    { "visible": false,  "targets": [ 6,7,8 ] },
    { "sClass": "dt-body-justify", targets : [1] }
]
Sign up to request clarification or add additional context in comments.

3 Comments

It can success click on other pages of the table, Thank you so much!!!! But now have another problem occurs, the data retrieved on the same row is different. For example, first page maximum is five row, on the second page, when I click the first row, it still give me the first row data of the first page, not first row data of the second page.
var rowindex=$(this).closest('tr').index(); var data1 = $('#tbl').DataTable().row( this.row_selected ).columns(8).data(); var a= data1[0][rowindex];
@EliceEe - this is because $(this).closest('tr').index(); give you the jQuery DOM index, you need the dataTables index -> table.row(this).index() (if it is inside a tr-click handler)

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.