I am having trouble binding a click event to a button in a cell. The table is populated initially via razor code
enter code here<table class="table table-bordered" id="requestTable">
<thead>
<tr>
<th class="dynatable-header">
Request ID
</th>
<th class="dynatable-header">
Caller ID
</th>
<th class="dynatable-header">
Result Code
</th>
<th data-dynatable-column="address1And2" class="dynatable-header">
Address 1 & 2
</th>
<th class="dynatable-header">
City
</th>
<th class="dynatable-header">
State
</th>
<th class="dynatable-header">
Zip
</th>
<th class="dynatable-header">
Request Date
</th>
<th data-dynatable-column="requestTime" class="dynatable-header">
Request Time (ms)
</th>
<th data-dynatable-column="moreInfo" class="dynatable-header">
</th>
</tr>
</thead>
<tbody>
@foreach (AdminRequest request in this.Model)
{
<tr>
<td>
@request.RequestID
</td>
<td>
@request.CallerID
</td>
<td>
@request.ResultCD
</td>
<td>
@(request.AddressLine1 + " " + request.AddressLine2)
</td>
<td>
@request.City
</td>
<td>
@request.State
</td>
<td>
@request.ZipCode
</td>
<td>
@request.RequestDate
</td>
<td>
@(request.RequestMS.HasValue ? request.RequestMS.Value : 0)
</td>
<td>
<!--button goes here-->
</td>
</tr>
}
</tbody>
</table>
I would normally populate the cell with an action link to shove me over to another page, however I would like a button to show a popup instead. Therefore, I need dynamically bound buttons in the table which would have access to the row's data to call a web service on click to get the rest of the data. I am tired of using jQuery data tables, and thought I would try something new and use dynatables.
After it is populated, I call dynatables on the table. I have written a custom rowReader to strip out whitespace etc that comes along with the data.
$("#requestTable").dynatable({
features: {
pushState: false //stop firefox from erroring on the character limit
},
readers:{
_rowReader: function (index, tr, record) {
record.requestId = record.requestId.trim();
record.callerId = record.callerId.trim();
record.resultCode = record.resultCode.trim();
record.address1And2 = record.address1And2.trim();
record.city = record.city.trim();
record.state = record.state.trim();
record.zip = record.zip.trim();
record.requestDate = record.requestDate.trim();
record.requestTime = record.requestTime.trim();
record.moreInfo = "<button type='button' class='btn btn-primary'>More Info</button>";
}
},
writers: {
"moreInfo": function (record, tr) {
var button = record.moreInfo;
$(button).on("click", function () {
alert("hello");
});
return button;
}
}
});
This renders the button, but does not attach the event handler. Any ideas as to what I am doing wrong?
Thanks in advance!