javascript - How to get column value with click of each column button in DataTables? - Stack Overflow

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

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.

  <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>

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.

  <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>
Share Improve this question edited Nov 30, 2016 at 20:15 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Nov 30, 2016 at 17:15 Sadequer RahmanSadequer Rahman 1335 silver badges11 bronze badges 2
  • can you provide us with your html? or better yet a fiddle? – Hoang Vu Commented Nov 30, 2016 at 17:26
  • @Hoang Thanks for your ments. After <script> That's all HTML code referring to this code. – Sadequer Rahman Commented Nov 30, 2016 at 17:30
Add a ment  | 

1 Answer 1

Reset to default 6

You need to use Object.keys

Further more, I remend 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]]);
 });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313983a4420464.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信