On row click not working

On row click not working

HoneydewHoneydew Posts: 25Questions: 7Answers: 0
edited November 2018 in Free community support

The html code I have written so far is:

    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $.ajax({
                "url": "Handlers/jQueryDatatableHandler.ashx",
                "data": { Operation: 'InboxMessages', searchText: '' },
                success: function (data) {
                    json = JSON.parse(data);
                    columns = [];
                    // build column titles
                    for (var i = 0; i < json.colnames.length; i++) {
                        columns.push({ title: json.colnames[i] });
                    }

                    var table = $('#example').DataTable({
                        "responsive": true,
                        "processing": true, // for show progress bar  
                        "serverSide": false, // for process server side  
                        "order": [[4, 'desc']],
                        data: json.rows,
                        columns: columns,
                        columnDefs: [
                            {
                                targets: 0,                           
                                render: function (data, type, row) {
                                    if (type === 'display') {
                                        return '<input type="checkbox" class="editor-active">';
                                    }
                                    return data;
                                },
                                className: "dt-body-center",
                                "orderable": false,
                                "searchable": false
                            },
                            {
                                targets: 1,
                                visible: false
                            },
                            {
                                targets: -1,
                                visible: false
                            }
                        ]
                    });
                }
            });

            $('#example tbody').on('click', 'tr', function () {
                if ($(this).hasClass('active')) {
                    $(this).removeClass('selected');
                    alert('clicked!');
                }
                else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                    alert('not clicked!');
                }
            });
        });
    </script>
</head>
<body>
    <h2>Datatable example </h2>
    <table id="example" class="display" style="width:100%">
    </table>
</body>

Now the jQueryDatatableHandler.ashx code returns json like this:

{"colnames":[" ","EmployeeId","Name","Title","Department","Viewed"],"rows":[["0","47216","James Bond","General Manager","10/23/2018 10:39:45 PM","True"],["0","47218","Ashley Paul","HR","10/24/2018 1:37:57 AM","True"],["0","47220","James Corbett","Writer","10/26/2018 9:47:07 PM","False"]]}

  1. I want to show the employee details below the table when a row is clicked but it ($('#example tbody').on('click', 'tr', function () ) is not working.
  2. I want to change the row color on hover, how can I do that?
  3. Also on double click of the row it should open the employee details in new page.

Please advise.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited November 2018

    I want to show the employee details below the table when a row is clicked but it ($('#example tbody').on('click', 'tr', function () ) is not working.

    I think the problem is due to the tbody not being in the page when the event is established. Take a look at jQuery Understanding Event Delegation. I think you will want this:
    $('#example').on('click', 'tbody tr', function () {

    I want to change the row color on hover, how can I do that?

    You can try using CSS for this. Maybe something like this:

    table.dataTable.display tbody tr:hover { background: yellow; }
    table.dataTable.display tbody tr:hover > .sorting_1 { background: yellow; }
    

    Also on double click of the row it should open the employee details in new page.

    Use the same format as the click event but with dblclick.

    Kevin

  • HoneydewHoneydew Posts: 25Questions: 7Answers: 0

    Thanks Kevin, Just wondering how do I get EmployeeId for every row which is a hidden column

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited November 2018 Answer ✓

    You would use row() and data() APIs. This code will give you the full row of data:

      $('#example').on('click', 'tbody tr', function () {
        var row = table.row($(this)).data();
        console.log(row);   //full row of array data
        console.log(row[1]);   //EmployeeId
      });
    

    Kevin

This discussion has been closed.