【发布时间】:2014-06-28 10:01:48
【问题描述】:
我是 Knockout JS 的新手,我正在努力解决这个问题,需要您的指导。一切正常,我很可能得到ProductID,它是ProductOffers VIA Ajax,但是当我第二次使用dropdown 时不会自动填充。
<table>
<thead>
<tr>
<th></th>
<th>Product</th>
<th>Product Offers</th>
<th>Price</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select data-bind="options: Products, optionsText: 'Name',optionsValue: 'ID', value: ProductID, optionsCaption: '-'" />
</td>
<td data-bind="if: ProductID">
<select data-bind="options: ProductOffers, optionsText: 'Name',optionsValue: 'ID', value: ProductOfferID, optionsCaption: '-'" />
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function Product(id, name) {
this.ID = id;
this.Name = name;
}
function Offer(id, name) {
this.ID = id;
this.Name = name;
}
var viewModel = {
Products: ko.observableArray(<%= LoadProducts() %>),
ProductID: ko.observable('0'),
ProductOfferID: ko.observable('0'),
ProductOffers: ko.observable("")
};
viewModel.ProductID.subscribe(function (newValue) {
if (typeof newValue != "undefined") {
//alert("Selected product is : " + newValue);
viewModel.ProductOffers = GetProductOffers(newValue);
//alert(viewModel.ProductOffers);
}
});
ko.extenders.numeric = function (target, precision) {
//create a writeable computed observable to intercept writes to our observable
var result = ko.computed({
read: target, //always return the original observables value
write: function (newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValue !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
});
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
ko.applyBindings(viewModel);
function GetProductOffers(ProductID) {
alert("Fetching offers for Product : " + ProductID)
var Val = "";
jQuery.ajax({
type: "POST",
url: "testing.aspx/GetProductOffers",
data: "{ProductID: '" + ProductID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (msg) {
Val = msg.d;
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.' + jqXHR.responseText);
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]' + jqXHR.responseText);
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].' + jqXHR.responseText);
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.' + jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.' + jqXHR.responseText);
} else if (exception === 'abort') {
alert('Ajax request aborted.' + jqXHR.responseText);
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
return Val;
}
</script>
**EDIT : ** 这是 tim 输入后正在发生的事情。
JSFiddle : http://jsfiddle.net/neodescorpio/sPrVq/1/
编辑:
这里是 web 方法,我根据 JSLint 对其进行了更改以生成有效的JSON。现在第二个下拉列表已填满,但问题是每当我更改产品时它的值永远不会改变,获取正确的值但下拉列表不显示它们。
[WebMethod]
public static string GetProductOffers(long ProductID)
{
StringBuilder sbScript = new StringBuilder();
string json = "[{\"ID\": 0,\"Name\": \"Sorry ! No data found\"}]";
bool first = true;
List<DMS.ProductOfferDO> offers = ProductOffers.Get(ProductID);
if (offers != null && offers.Count > 0)
{
sbScript.Append("[");
foreach (var x in offers.OrderBy(d => d.IsCashOffer))
{
if (first)
{
sbScript.Append(string.Format("{{\"ID\": {0},\"Name\": \"{1}\"}}", x.ID, x.Name));
first = false;
}
else
{
sbScript.Append(string.Format(",{{\"ID\": {0},\"Name\": \"{1}\"}}", x.ID, x.Name));
}
}
sbScript.Append("]");
json = sbScript.ToString();
}
return json;
}
【问题讨论】:
-
您的示例代码中没有任何内容可以实际获取任何数据或执行任何操作来填充第一个列表选项中的第二个列表,因此我认为您要么需要包含更多代码,要么缺少一个大逻辑块。
-
@TimHobbs 感谢您的即时回复,我没有遗漏任何东西,因为我说除了问题之外它工作正常。所有我试图完成他的第二个下拉列表的事情都在第一个更改时填充。并获得一些详细信息,例如价格和库存。并显示在表格上。
-
我的错误 - 我想我的大脑放屁了,没有向下滚动查看其余部分。 :)
-
它有时会发生兄弟:D
-
用 JSFiddle 更新
标签: javascript jquery asp.net knockout.js