I have a table with items obtained from a knockout observable array, the user enter a search text and coincidences populate the observableArray, this table is shown on a modal. Each item has a button to open another modal with some functionality (ommited because is not relevant). The table must display the items if observableArray length is greater than 0, otherwise, must display one row to indicate there's no results to display.
<tr style="display: none" data-bind="visible: items().length == 0">
<td class="text-center alert alert-warning" colspan="4"><b>There's no coincidences</b></td>
</tr>
My view model:
var viewModel = function () {
self.items= ko.observableArray([]);
//Modal is already on html, but not visible, to show it I use this
$('#searchProduct').modal('show');
//When modal is closed, the table is cleaned, so the items in observableArray are removed
$('#searchProduct').on('hidden.bs.modal', function () {
self.items.removeAll();
});
}
The problem is that the first time, the visible binding works fine, but when the observableArray length changes (when call removeAll on hidden), the bind is not applied again. I know is because the binding is already apply, so when the observableArray changes, the length is updated but the condition cannot render html again.
How can this be solved with knockout?
(I tried to be very specific, but if more information is needed, I can update the information to be clearer)