使用knockout重置下拉列表

时间:2016-02-16 09:56:40

标签: c# asp.net knockout.js

我对Knockout很陌生,在选择默认选项时,重置我的级联下拉列表时遇到问题。

这是我目前的代码

JS

/// <reference path="../knockout/knockout-3.4.0.debug.js" />
/// <reference path="../jquery/jquery.min.js" />

var deal = function () {
var self = this;

self.ManufacturerOptions = ko.observableArray();
self.VehicleManufacturerId = ko.observable();
self.RangeOptions = ko.observableArray();
self.VehicleRangeId = ko.observable();

var Deals = {

    ManufacturerOptions: self.ManufacturerOptions,
    VehicleManufacturerId: self.VehicleManufacturerId,
    RangeOptions: self.RangeOptions,
    VehicleRangeId: ko.observable(self.VehicleRangeId),

};

self.Deal = ko.observable();
self.Deals = ko.observableArray();
GetManufacturers();

function GetManufacturers() {
    $.ajax({
        url: 'http://localhost:47633/api/Vehicle/GetManufacturers',
        type: 'get',
        crossDomain: true,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (dataReturned) {
            self.ManufacturerOptions(dataReturned);
        }
    });
}

self.manufacturerChanged = function (obj, event) {
    var manufacturerId = $("#Manufacturer").val();
    GetRanges(manufacturerId);
}

function GetRanges(manufacturerId) {
    return manufacturerId ? $.ajax({
        url: 'http://localhost:47633/api/Vehicle/GetRanges?manufacturerCode=' + manufacturerId,
        type: 'get',
        crossDomain: true,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (dataReturned) {
            self.RangeOptions(dataReturned);
        }
    }) : null;
}
};

$(document).ready(function () {
    ko.applyBindings(new deal());
});

HTML

<h1>DealBook</h1>
<div data-bind="if: Deal">
<div>
    <h2>Update Deal</h2>
</div>
<div>
    <p>Manufacturer: <select id="Manufacturer" data-bind="options: ManufacturerOptions, optionsCaption: 'Select Manufacturer', optionsValue: 'cman_code', optionsText: 'cman_name', value: Deal().VehicleManufacturerId, event: { change: manufacturerChanged}"></select></p>
    <p>Range: <select id="Range" data-bind="options: RangeOptions, optionsCaption: 'Select Range', optionsValue: 'cran_code', optionsText: 'cran_name', value: Deal().VehicleRangeId, event: { change: rangeChanged }"></select></p>
</div>
</div>
<div id="dealCreateContainer" data-bind="ifnot: Deal()">
<div>
    <p>Manufacturer: <select id="Manufacturer" data-bind="options: ManufacturerOptions, optionsCaption: 'Select Manufacturer', optionsValue: 'cman_code', optionsText: 'cman_name', value: VehicleManufacturerId, event: { change: manufacturerChanged}"></select></p>
    <p>Range: <select id="Range" data-bind="options: RangeOptions, optionsCaption: 'Select Range', optionsValue: 'cran_code', optionsText: 'cran_name', value: VehicleRangeId, event: { change: rangeChanged }"></select></p>
</div>

我可以按self.VehicleRangeId(null)设置范围的默认值,但我似乎无法清除下拉列表。我想我可以设置选项 - self.RangeOptions(null)但是会​​清除包含默认值的下拉列表。有人可以给我一个指针或解释我需要做什么吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

如果您有默认值,则可以覆盖RangeOptions

self.RangeOptions(['default value'])

否则,您可以清除所有范围选项:

self.RangeOptions([])