ccravenbartle
asked on
Knockout JS CSS binding
I am trying knockout css binding for the first time... In a table, I want to set the Status element label to the Bootstrap class 'label-success' or 'label-important' based on the string value returned by the observable Status()
However, at runtime I get a: JavaScript runtime error: Function expected
Is it even possible to do css binding on strings or does it expect to evaluate boolean or numeric values?
<tbody data-bind="foreach: PurchaseRequests" >
<tr>
<td data-bind="text: Date"></td>
<td data-bind="text: Reference"></td>
<td>
<span data-bind="text: Status,
css: {'label' : true,
'label-success' : Status() == 'Draft',
'label-important': Status() == 'Approved'}">
</span>
</td>
</tr>
</tbody>
However, at runtime I get a: JavaScript runtime error: Function expected
Is it even possible to do css binding on strings or does it expect to evaluate boolean or numeric values?
ASKER
Leakim: thank you for your response. However, I'm not sure how to get this to work as Status() is a string and can be 'Draft', 'Sent For Approval', 'Approved' or 'Posted'. I imagine I would need a new observable for each. The PurchaseRequests observable array is being created via the Ajax call below. Would I need loop through each element of the observable array to set up these new observables for each Purchase Request?
vm.PurchaseRequests.getPurchaseRequests = function () {
$.ajax({
url: '@Url.Action("GetOutstandingRequest", "ControlPanel")',
type: 'GET',
contentType: 'application/json',
dataType: 'json',
success: function (result) { vm.PurchaseRequests(result); }
});
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Leakim: Thank you - your approach worked
Open in new window
And :Open in new window