jQuery-JTable: add on click event for row?

James Carter picture James Carter · Apr 8, 2013 · Viewed 16.1k times · Source

I have to following code to show my user table, this is achieved by JTable.

<script type="text/javascript">
    $(document).ready(function() {              
        $('#userTableContainer').jtable({
            title: 'Users',
            selecting: false,
            paging: true,
            pageSize: 15,
            sorting: true,
            addRecordButton: false,
            saveUserPreferences: false,
            create: false,
            edit: false,
            actions: {
                listAction: 'user/getUsers.htm',
            },
            fields: {
                username: {
                    title: 'username'
                },
                firstname: {
                    title: 'firstname'
                },
                lastname: {
                    title: 'lastname'
                },
                company: {
                    title: 'company'
                }
             }
        });
        $('#userTableContainer').jtable('load');              
    });    

</script>

<div id="content">
    <h1>Users</h1>      

    <br />
    <div id="userTableContainer">

    </div>
</div>

Is it possible to add a custom action event for every row? So that i could submit a request like "user/showUser.htm" to my controller.

Answer

Jules Colle picture Jules Colle · Apr 9, 2013

This should get you on your way:

$('#userTableContainer').jtable({
    ....
    recordsLoaded: function(event, data) {
        $('.jtable-data-row').click(function() {
            var row_id = $(this).attr('data-record-key');
            alert('clicked row with id '+row_id);
        });
    }
});