Knockout を使い始めたのですが、クリック イベントが発生しないという問題があります。
上記は GetWaiters 関数を起動していません。私が間違っていることや欠けていることはわかりません。
ティア
私は次のhtmlを持っています:
<h2>Create</h2>
<table>
<thead>
<tr>
<th>WaiterId</th>
<th>RestId</th>
<th>Name</th>
</tr>
</thead>
<tbody data-bind="foreach: Waiters">
<tr>
<td data-bind="text: waiter_id"></td>
<td data-bind="text: rest_id"></td>
<td data-bind="text: name"></td>
</tr>
</tbody>
</table>
<br />
@Scripts.Render("~/bundles/myBundle")
<input type="button" id="btnGetWaiters" value="Get Waiters" data-bind="click: GetWaiters" />
And following in my js file:
var WaiterViewModel = function () {
//Make the self as 'this' reference
var self = this;
//Declare observable which will be bind with UI
self.waiter_id = ko.observable("0");
self.rest_id = ko.observable("0");
self.name = ko.observable("");
//The Object which stored data entered in the observables
var WaiterData = {
waiter_id: self.waiter_id,
rest_id: self.rest_id,
name: self.name
};
//Declare an ObservableArray for Storing the JSON Response
self.Waiters = ko.observableArray([]);
GetWaiters(); //Call the Function which gets all records using ajax call
//Function to Read All Employees
function GetWaiters() {
alert("fetching");
//Ajax Call Get All Employee Records
$.ajax({
type: "GET",
url: "/api/WaiterAPI",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
self.Waiters(data); //Put the response in ObservableArray
},
error: function (error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
//Ends Here
}
};
ko.applyBindings(new WaiterViewModel());