Click handlers disappear, when updating cell data

Click handlers disappear, when updating cell data

jdegejdege Posts: 4Questions: 2Answers: 0

I have an ajax-populated datatable with one column containing three buttons. In the "createdRow" callback, I am finding these buttons using jQuery, and attaching event handlers to their 'click' events.

```js
module.createTable = function()
{
return $('#attachmentList').DataTable({
"createdRow": function(row, data, dataIndex)
{
var $row = $(row);

            $row.data("rowname", data.name);

            // fire an edit event for the edit anchor
            var $a = $row.find("a.attachment-edit");
            $a.click(function (event)
            {
                var $this = $(this);
                var name = $this.data("rowname");

                sb.notify({
                    type: 'edit-attachment',
                    data: name
                });
            });
        },
        [...]

```js

Firing the 'edit-attachment' event brings up a pop-out dialog, with a textbox and a save button. If the user types in some text, and clicks "save", we send some stuff on the server, and raise a notes updated event that changes the data in the appropriate cell of the datatable:

```js
module.notesUpdated = function(dto)
{
var rows = table.rows().eq(0);

    for (var i = 0; i < rows.length; i++)
    {
        var row = rows[i];
        var node = table.row(row).node();
        var name = $(node).data("rowname");

        if (name === dto.name)
        {
            table.cell(row, 5).data(dto.notes);
            break;
        }
    }

    table.draw();
};

```js

And when we do this, the click event handlers disappear.

You can see this with inspect element. Prior to clicking "save", the <a>'s have "click" mapped to jquery's y.handle, after clicking save, they do not. If I comment-out table.cell().data(), the event handlers do not disappear.

Is this a bug? Perhaps. It's certainly not what I expected to happen. But before I start making claims, I thought I'd ask if I was simply approaching the problem in the wrong way. Am I doing something in a way other than what datatables would expect me to be doing?

Is there a preferred way of wiring up event-handlers to elements within a datatables cell, or is there another way of updating the data in the cell, that would avoid this problem?

Or do I simply have to rewire my event handlers, every time I update the contents of a cell?

This discussion has been closed.