【问题标题】:how to make advanceed search using Knockout javascript?如何使用 Knockout javascript 进行高级搜索?
【发布时间】:2014-04-30 13:27:48
【问题描述】:

我正在处理 MVVM 中的任务并使用 Knockout 和 Durandal 进行单页应用程序,但我是这个问题的新手,我需要进行高级搜索

问题是: 我有两个用 Infragistics 制作的组合框 首先是国家作为自动完成,它从数据库正确绑定(这是有效的)

<input id="iGCboxFromDest" data-bind="igCombo: {
dataSource: $root.countriesList,
text: countryID,
autoComplete: true,
showDropDownButton: false,
textKey: 'name',
valueKey: 'id',
width: 265,
mode: 'editable',
enableClearButton: false,
filteringType: 'local',
renderMatchItems: 'startsWith'
}" />

第二个城市,我需要根据国家/地区过滤它

,怎么做?

【问题讨论】:

    标签: javascript jquery mvvm knockout.js


    【解决方案1】:

    我已经修改了jsfiddle here,向它展示了如何使用您提到的组合框的两个选择输入来完成它的一个想法。

    基本上,您需要将要过滤的内容的选定值绑定到可观察对象。在我的示例中,我有一组 Country 和一组城镇(或城市!),它们通过 CountryId 与 Country 建立关系。

    countries = ko.observableArray([{"Id":1, "Name": England"}, //etc.]);
    towns = ko.observableArray([{"CountryId":1,"Name":"London"},{"CountryId":2,"Name":"Glasgow"},{"CountryId":3,"Name":"Cardiff"},{"CountryId":4,"Name":"Belfast"}]);
    

    并且选择的国家被绑定到一个 observable

    selectedCountry = ko.observable();
    

    然后,我将它们绑定到一个选择框

    <select id="countries" data-bind="value: selectedCountry, options: countries, optionsText: 'Name', optionsValue: 'Id',  optionsCaption: 'Countries'"></select>
    

    现在我们必须按 countryId 过滤城镇

    filteredTowns = ko.computed(function() {
       return ko.utils.arrayFilter(self.towns(), function(item) {
            return item.CountryId === self.selectedCountry();
        });  
    });
    

    第二个选择框,绑定到过滤后的城镇现在应该显示与该城市相关的城镇。

    <select id="towns" data-bind="value: selectedTown, options: filteredTowns, optionsText: 'Name', optionsValue: 'CountryId',  optionsCaption: 'towns'"></select>
    

    【讨论】:

    • 它在 html 标记中工作得很好,但是 igCombo 怎么样 - Infragistics 是否有任何细节要添加到中?
    • 我没有使用过 Infragistics,但下面的帖子提到它们在 v 13.1 中支持它。他们似乎也有一个级联的下拉菜单,这可能会使这更简单。 infragistics.com/community/blogs/jason_beres/archive/2013/03/25/…
    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 2016-07-13
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多