0
我有以下HTML代碼,其中引用JavaScript。代碼不顯示任何錯誤,但數據不會顯示在屏幕上。動態行代使用KnockoutJS
我看到以下沒有工作
數據綁定線='選項:AssignResourceView.ResourceViewData
我指這段代碼jsfiddle
<table width='100%'>
<thead>
<tr>
<th width='25%'>Resource Type</th>
<th width='25%'>Resource</th>
<th width='10%'> </th>
</tr>
</thead>
<tbody data-bind='foreach: AssignResourceView.GetLines'>
<tr>
<td>
<select data-bind='options: AssignResourceView.ResourceViewData, optionsText: "name", optionsCaption: "Select...", value: "id"'> </select>
</td>
<td data-bind="with: resourceviews">
<select data-bind='options: resource, optionsText: "name", optionsCaption: "Select...", value: $parent.product'> </select>
</td>
<td>
<a href='#' data-bind='click: $parent.removeLine'>Remove</a>
</td>
</tr>
</tbody>
</table>
這個是我的JavaScript代碼
var AssignResourceView = function() {
var AssignResourceViewModel =
{
dataFromServer: sampleResourceViews,
lines: ko.observableArray(),
ResourceViewLine: {
resourceviews: ko.observable(),
resources: ko.observable(),
selectedresourceviewId :ko.observable(),
selectedresourceId : ko.observable(),
},
addLine: function()
{
AssignResourceViewModel.lines.push(AssignResourceViewModel.ResourceViewLine)
},
removeLine: function (line) { AssignResourceViewModel.remove(line) },
save : function() {
var dataToSave = $.map(lines(), function (line) {
return line.resourceviews() ? {
resourcename: line.resourceviews().name,
resourceid: line.selectedresourceviewId
} : undefined
});
alert("Could now send this to server: " + JSON.stringify(dataToSave));
},
LoadVM: {
Init: function() {
AssignResourceViewModel.ResourceViewLine.resourceviews.subscribe(function() {
AssignResourceViewModel.ResourceViewLine.resources(undefined);
}),
AssignResourceViewModel.lines = ko.observableArray([AssignResourceViewModel.ResourceViewLine])
},
}
}
return {
//main function to initiate the module
Init: function() {
debugger;
AssignResourceViewModel.LoadVM.Init();
var aR = document.getElementById("assignresourcediv");
ko.applyBindings(AssignResourceViewModel, aR);
},
GetLines:function(){
AssignResourceViewModel.lines();
},
AddLine: function() {
debugger;
AssignResourceViewModel.addLine();
},
RemoveLine: function() {
AssignResourceViewModel.removeLine();
},
Save:function(){
AssignResourceViewModel.save();
},
ResourceViewData:function(){
AssignResourceViewModel.dataFromServer;
},
Model: AssignResourceViewModel
};
}();
我的JSON樣品
var sampleResourceViews = [
{
"resource": [
{
"name": "deepak",
"id": 1
},
{
"name": "raju",
"id": 2
}
],
"name": "Vallet",
"id":1
},
{
"resource": [
{
"name": "deepak",
"id": 1
},
{
"name": "raju",
"id": 2
}
],
"name": "Service Specialist",
"id": 2
},
{
"resource": [
{
"name": "deepak",
"id": 1
},
{
"name": "raju",
"id": 2
}
],
"name": "Sales Specialist",
"id": 3
},
{
"resource": [
{
"name": "deepak",
"id": 1
},
{
"name": "raju",
"id": 2
}
],
"name": "Delivery Specialist",
"id": 4
}
];
謝謝,將嘗試它並更新此POST。但第二下拉不會加載在你的小提琴中。我的意思是,當我在第一個下拉列表中選擇一個項目時,它應該在第二個下拉列表中刷新項目。我已經使用「訂閱」功能。 – iShareHappyCode 2014-10-11 21:13:19
這裏有一個快速入門,讓第二個下拉工作 - http://jsfiddle.net/sifriday/eu95b2mz/5/ – sifriday 2014-10-11 21:55:21
我想你應該看看這個例子:http://knockoutjs.com/examples/ collections.html,並嘗試在不使用模塊模式的情況下重建代碼 - 只需簡單地使用視圖模型嘗試即可。感覺就像因爲你不需要的代碼而導致KO模式混亂。例如,第二個下拉列表並非真的需要來自訂閱,因爲數據已經在您的視圖模型中,通過您在第一個下拉列表中選擇的內容。 – sifriday 2014-10-11 21:58:04