1

I have the following jQuery code

$(document).ready(function () {
    $('.data').each(function (i) {
        alert('Test' + i);
        $(.data #forceclock).click(function () {
            console.log('Done');
            var id = $('employee_id').attr('value');

            $.ajax({
                'type': 'POST',
                'data': { employee_id: id },
                'datatype': 'text',
                'success': function (textStatus) {
                    alert("Successfully logged out a user!" + textStatus);
                    location.reload();
                },
                'error': function (textStatus) {
                    alert("failure" + textStatus);
                },
                'url': '/clockoutjs',
                'cache': false
            });
            return false;
        });

    });
});

The HTML is the following:

<table class="table table-striped table-bordered table-condensed table-hover">
    <thead>
    <tr>
        <th style="text-align: center">Employee Name</th>
        <th style="text-align: center">Employee ID</th>
        <th style="text-align: center">Clocked In</th>
        <th style="text-align: center"># of Clock ins</th>
        <th style="text-align: center"><a id="tooltip" data-toggle="tooltip" data-placement="top"
                                          title="As of last clock out in hours!">Total Clocked Time
            Today</a>
        </th>
        <th style="text-align: center">Time Since Last Checkin</th>
        <th style="text-align: center">Force clock out</th>
    </tr>
    </thead>
    {% for log in logs %}
        <tr id="data">
            <th style="text-align: center">{{ log.employee_name }}</th>
            <th class="employee_id" stlye="text-align: center"> {{ log.employee_id }}</th>
            <th style="text-align: center">{{ log.clocked_in|last }}</th>
            <th style="text-align: center">{{ log.clocked_in|length }}</th>
            <th style="text-align: center">{{ log.total_time|floatformat:2 }} Hours</th>
            <th style="text-align: center">{{ potential_times.0|floatformat:2 }} Hours</th>
            <th style="text-align: center"><div class="forceclock"><a href="#" class="btn btn-danger">Clock out!</a></div>
            </th>
        </tr>
    {% endfor %}
</table>

The click function makes an ajax request to the server that should clockout a user. Unfortunately I'm not able to supply the proper id nor create the click function for every button I've created.

How do I generate the click function for every element that was created in that table and make take the #employee_id from that <tr id="data">?

I'm using webapp2 and GAE to generate the content.

Dyrandz Famador
  • 4,303
  • 5
  • 18
  • 38
Borko Kovacev
  • 890
  • 2
  • 10
  • 29

4 Answers4

0

I think that your problem may be the jQuery look up... needs a larger scope - because it doesn't know your new stuff is there. Put an ID on your table.. #table... etc....

$('#table').on('click', '.button-youre-targeting', function() {
  // whatever
});

So, it's watching for events in the larger scope...

Shot in the dark. Not really sure if I'm on the right track - because I don't see a click handler...

sheriffderek
  • 8,217
  • 6
  • 38
  • 64
0

You can do something like

    <table id="employee-info" class="table table-striped table-bordered table-condensed table-hover">
        <thead>
        <tr>
            <th style="text-align: center">Employee Name</th>
            <th style="text-align: center">Employee ID</th>
            <th style="text-align: center">Clocked In</th>
            <th style="text-align: center"># of Clock ins</th>
            <th style="text-align: center"><a id="tooltip" data-toggle="tooltip" data-placement="top"
                                              title="As of last clock out in hours!">Total Clocked Time
                Today</a>
            </th>
            <th style="text-align: center">Time Since Last Checkin</th>
            <th style="text-align: center">Force clock out</th>
        </tr>
        </thead>
        {% for log in logs %}
            <tr class="data">
                <th style="text-align: center">{{ log.employee_name }}</th>
                <th class="employee_id" stlye="text-align: center"> {{ log.employee_id }}</th>
                <th style="text-align: center">{{ log.clocked_in|last }}</th>
                <th style="text-align: center">{{ log.clocked_in|length }}</th>
                <th style="text-align: center">{{ log.total_time|floatformat:2 }} Hours</th>
                <th style="text-align: center">{{ potential_times.0|floatformat:2 }} Hours</th>
                <th style="text-align: center"><div class="forceclock"><a href="#" class="btn btn-danger">Clock out!</a></div>
                </th>
            </tr>
        {% endfor %}
    </table>

then inside the click handler use the this reference to find the employee-id of the clicked row

$(document).ready(function () {
    $('#employee-info .forceclock').on('click', function (i) {
        console.log('Done');
        var id = $(this).closest('tr').find('.employee_id').text();

        $.ajax({
            'type': 'POST',
                'data': {
                employee_id: id
            },
                'datatype': 'text',
                'success': function (textStatus) {
                alert("Successfully logged out a user!" + textStatus);
                location.reload();
            },
                'error': function (textStatus) {
                alert("failure" + textStatus);
            },
                'url': '/clockoutjs',
                'cache': false
        });
        return false;
    });

});

If you want to use event delegation change the handler to

$(document).ready(function () {
    $('#employee-info').on('click', '.forceclock', function (i) {
        //click handler code
    });
});
Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
0

Your markup should be:

<tr class="data">

Not <tr id="data">. And to get the employee ID use the following:

var id = $('employee_id', this).text();

Also, note that you do not need to use .each(), jQuery will internally iterate through all elements that match the selector. So your code should look like so:

$('.data').click(function (e) {
    e.preventDefault();
    var id = $('employee_id', this).text();

    $.ajax({
        'type': 'POST',
        'data': { employee_id: id },
        'dataType': 'text',
        'success': function (data) {
            alert("Successfully logged out a user!" + data);
            //Update DOM with data received. No need to reload page - defeats the purpose of ajax.
            //location.reload();
        },
        'error': function (textStatus) {
            alert("failure" + textStatus);
        },
        'url': '/clockoutjs',
        'cache': false
    });
});    
PeterKA
  • 21,234
  • 4
  • 21
  • 44
0

you want to click this button line<th style="text-align: center"><a id="forceclock" href="#" class="btn btn-danger">Clock out!</a>. In above line id is must be unique so remove #forceclock then insert your employee_id value in there...

$(document).ready(function () {
         $('body').on('click','.btn-danger',function(){
          var edit_id = $(this).attr('id'); //id must be your employee_id
          .........//it will be ok
         });
});
David Jaw Hpan
  • 4,423
  • 1
  • 22
  • 49