Reputation: 111
I've got a Knockout js foreach loop arount a table which returns multiple users and a search button for each returned user to search on that record. The button and data is drawn as it should but when I click any of the search buttons it only returns the first user. I believe this is because the id for the search button is always the same to need to interact with the DOM to add an each function around? Would this be correct and if so how is this done?
<!-- ko foreach: $data -->
<table class="table table-striped, table-bordered" id="customer-results-policy">
<thead>
<tr>
<th>
Title
</th>
<th>
First Name
</th>
<th>
Surname
</th>
<th>
Date of birth
</th>
<th>
Email
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span data-bind="text: Customer.Title"></span>
</td>
<td>
<span data-bind="text: Customer.Forename"></span>
</td>
<td>
<span data-bind="text: Customer.Surname"></span>
</td>
<td>
<span data-bind="dateString: Customer.DateOfBirth"></span>
</td>
<td>
<div><input type="text" id="email-search-box-customer" class="span3 search-query" readonly="readonly" data-bind="value: Customer.Email" /> <br/>
<button type="submit" data-bind="click: $parent." class="btn btn-primary submit-email-search-customer">Search for this customer</button> </div>
</td>
</tr>
</tbody>
</table>
<!-- /ko -->
JS
$('#page-parent').on('click', '.submit-email-search-customer', function () {
$('.submit-email-search-customer').each(function() {
});
var email = $('#email-search-box-customer').val();
var dataExists;
{
dataExists = viewModel.refreshViewByEmail(email);
}
if (!dataExists) {
$('#error-message').html("<strong>The email address wasn't recognised -- please check and try again</strong>");
$('#error-display').show('slow');
return false;
} else {
$('#search-results-many').hide();
$('#customer-results-email').show();
$("#search-results").show('slow');
$("#modify-button").show('slow');
$("#customer-modify").show();
$("#account-results").show();
$("#address-results").show();
}
Upvotes: 0
Views: 544
Reputation: 709
First of all you cant use Constant values as id inside a foreach as this will make your html having multiple elements with the same id(which is not allowed)
And because you are using knockout to manage your template i should use knockout click bindings to manage the click events. See the documentation for knockout http://knockoutjs.com/documentation/click-binding.html
Upvotes: 1