2

I am trying to set edit button for each column on Datatables expandable row framework.

How to get column value with click of each column button in DataTables? When I click a button I'm getting data1 and data[2] value undefined. Can anyone tell me where I am doing wrong? Please refer to the image and snippet included.

enter image description here

  <script>
    $(document).ready(function () {

            var table = $('#example').DataTable({
                "ajax": "user.json",

                "columns": [
                    {
                        "className": 'details-control',
                        "orderable": false,
                        "data": null,
                        "defaultContent": ''
                    },
                    {"data": "Name"},
                    {"data": "Phone"},
                    {"data": "Role"},
                    {
                        "targets": -1,
                        "data": null,
                        "defaultContent": "<button>Edit</button>"
                    }
                ],
                "order": [[1, 'asc']]
            });
            $('#example tbody').on('click', 'button', function () {
                var data = table.row($(this).parents('tr')).data();
                alert(data[1] + "'s Phone is: " + data[2]);
            });

            // Add event listener for opening and closing details
            $('#example tbody').on('click', 'td.details-control', function () {
                var tr = $(this).closest('tr');
                var row = table.row(tr);

                if (row.child.isShown()) {
                    // This row is already open - close it
                    row.child.hide();
                    tr.removeClass('shown');
                }
                else {
                    // Open this row
                    row.child(format(row.data())).show();
                    tr.addClass('shown');
                }
            });
        });

    </script>

    <html>
    <table id="example" class="display" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th></th>
                            <th>Name</th>
                            <th>Phone</th>
                            <th>Role</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th></th>
                            <th>Name</th>
                            <th>Phone</th>
                            <th>Role</th>
                            <th></th>
                        </tr>
                        </tfoot>
                    </table>
    </html>
2
  • can you provide us with your html? or better yet a fiddle? Commented Nov 30, 2016 at 17:26
  • @Hoang Thanks for your comments. After <script> That's all HTML code referring to this code. Commented Nov 30, 2016 at 17:30

1 Answer 1

6

You need to use Object.keys

Further more, I recommend you to use .closest function, because you need to return the first match.

 $('#example tbody').on('click', 'button', function () {
       var data = table.row($(this).closest('tr')).data();
       alert(data[Object.keys(data)[0]]+' s phone: '+data[Object.keys(data)[1]]);
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I tried .closest('tr') doesn't work.

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.