简体   繁体   English

AngularJS下拉选项基于另一个下拉选择(自定义过滤器)

[英]AngularJS drop down options based on another drop down selection (custom filter)

I hope you guys can help me. 我希望你们能帮助我。

I am trying to filter the drop down selections of a drop down list, based on the value of another drop down list. 我正在尝试根据另一个下拉列表的值来过滤下拉列表的下拉选择。

I have one list that is my selection of reporting Frequency (Weekly, Monthly etc.), and another list that I would like filtered according to this selection. 我有一个列表是我选择的报告频率(每周,每月等),还有一个我希望根据此选择进行过滤的列表。

The only 2 real filter options must be Weekly and Monthly (the second drop down must be disabled when neither of these are selected). 仅有的2个实际过滤器选项必须是“每周”和“ 每月” (当两个均未选择时,必须禁用第二个下拉菜单)。

When the user select Weekly (ReportFrequencyType: 2), the data in the second drop down must be filtered to display those records matching ReportFrequencySelectionType: 2 (which are the days of the week) 当用户选择每周(ReportFrequencyType:2)时,必须过滤第二个下拉列表中的数据以显示与ReportFrequencySelectionType:2匹配的记录 (这是星期几)

Below my code with my attempt to create the filter: 在我的代码下方,尝试创建过滤器:

Note: I have tried variations of the filter, and amongst others, I get the following Error: Unable to get property 'ReportFrequencySelectionType' of undefined or null reference [object Object] 注意:我已经尝试使用过滤器的变体,并且除其他外,出现以下错误:无法获取未定义或空引用[object Object]的属性'ReportFrequencySelectionType'

Here is my controller: 这是我的控制器:

(function () {
    'use strict';

    angular
        .module('app.reportSettings')
        .controller('reportSettings', reportSettings);

    reportSettings.$inject = ['$q', 'dataservice', 'logger', 'customerFactory', '$scope', '$location', 'loginfactory', '$filter', '$interval', 'reportSettingsFactory'];

    function reportSettings($q, dataservice, logger, customerFactory, $scope, $location, loginfactory, $filter, $interval, reportSettingsFactory) {
        var vm = $scope;

        vm.saveSettings = saveSettings;
        vm.FrequencyOptions = [];
        vm.selectedReportNames = null;

        vm.customFilter = function (reportSettingsData) {
            if (reportSettingsData.SelectableReportFrequencySelectionNames.ReportFrequencySelectionType == vm.reportSettingsData.ReportFrequencyName.ReportFrequencyType) {
                return true;
            } else {
                return false;
            }
        };

        activate();

        function activate() {
            getData().then(getReportNames);
        }

        function getData() {
            return reportSettingsFactory.getSettings()
            .then(function (data) {
                if (logger.displayCommandResult(data)) {
                    vm.reportSettingsData = data.Records[0];
                    return vm.reportSettingsData;
                }
            });
        }
}
})();

Here are my 2 drop down lists in my view: 这是我视图中的2个下拉列表:

<!-- Frequency -->
<div class="col-md-3">
    <label>Frequency</label>
    <div class="input-dropdown">
        <cc-dropdown cc-placeholder="Report Frequency"
                        ng-model="reportSettingsData.ReportFrequencyName"
                        ng-options="reportSettingsData.SelectableReportFrequencyNames"
                        cc-fields="ReportFrequencyName"
                        cc-key-field="ReportFrequencyId"
                        cc-allow-search="reportSettingsData.SelectableReportFrequencyNames != null && reportSettingsData.SelectableReportFrequencyNames.length > 5"
                        name="iFrequencyName">
        </cc-dropdown>
    </div>
</div>

<div class="col-md-3">
    <label>Frequency Options</label>
    <div class="input-dropdown">
        <cc-dropdown cc-placeholder="Report Frequency Option"
                        ng-model="reportSettingsData.ReportFrequencySelectionName"
                        ng-options="reportSettingsData.SelectableReportFrequencySelectionNames | filter:customFilter"
                        ng-disabled="reportSettingsData.ReportFrequencyName.ReportFrequencyType != Enum.ReportFrequency.Monthly"
                        cc-fields="ReportFrequencySelectionName"
                        cc-key-field="ReportFrequencySelectionId"
                        cc-allow-search="true"
                        name="iFrequencySelections">
        </cc-dropdown>
    </div>
</div>

Here is a sample of the data pertaining to the filter and drop downs: 以下是与过滤条件和下拉列表相关的数据示例:

"SelectableReportFrequencyNames": [
    {
        "Id": "573ac13a8ac03497f7eef0e5",
        "ReportFrequencyId": 1,
        "ReportFrequencyName": "Daily",
        "ReportFrequencyType": 1
    },
    {
        "Id": "573ac1608ac03497f7eef0e6",
        "ReportFrequencyId": 2,
        "ReportFrequencyName": "Weekly",
        "ReportFrequencyType": 2
    },
    {
        "Id": "573ac1728ac03497f7eef0e7",
        "ReportFrequencyId": 3,
        "ReportFrequencyName": "Monthly",
        "ReportFrequencyType": 3
    },
    {
        "Id": "573ac1dc8ac03497f7eef0e8",
        "ReportFrequencyId": 4,
        "ReportFrequencyName": "Business Days",
        "ReportFrequencyType": 4
    },
    {
        "Id": "573ac1fb8ac03497f7eef0e9",
        "ReportFrequencyId": 5,
        "ReportFrequencyName": "Full Week",
        "ReportFrequencyType": 5
    }
],
"SelectableReportFrequencySelectionNames": [
    {
        "Id": null,
        "ReportFrequencySelectionId": 1,
        "ReportFrequencySelectionName": "Monday",
        "ReportFrequencySelectionType": 2
    },
    {
        "Id": null,
        "ReportFrequencySelectionId": 2,
        "ReportFrequencySelectionName": "Tuesday",
        "ReportFrequencySelectionType": 2
    },
    {
        "Id": null,
        "ReportFrequencySelectionId": 3,
        "ReportFrequencySelectionName": "Wednesday",
        "ReportFrequencySelectionType": 2
    }
]

Thank you greatly in advance! 提前非常感谢您!

After a lot of research and trail and error, herewith the answer and working solution to my question (I know this is one of many solutions): 经过大量研究和反复试验,得出我的问题的答案和可行的解决方案(我知道这是许多解决方案之一):

I made use of the ng-change directive, but instead of then applying different cases or filters in the AngularJS view side, I called a function in my controller to do a simple .forEach loop and gather my appropriate data. 我使用了ng-change指令,但是没有在AngularJS视图侧应用不同的大小写或过滤器,而是在控制器中调用了一个函数来执行简单的.forEach循环并收集我的适当数据。

Here is my view: 这是我的看法:

<!-- Frequency -->
<div class="col-md-3">
    <label>Frequency</label>
    <div class="input-dropdown">
        <cc-dropdown cc-placeholder="Report Frequency"
                        ng-model="reportSettingsData.ReportFrequencyName"
                        ng-options="reportSettingsData.SelectableReportFrequencyNames"
                        ng-change="frequencyChanged()"
                        cc-fields="ReportFrequencyName"
                        cc-key-field="ReportFrequencyId"
                        cc-allow-search="reportSettingsData.SelectableReportFrequencyNames != null && reportSettingsData.SelectableReportFrequencyNames.length > 5"
                        name="iFrequencyName">
        </cc-dropdown>
    </div>
</div>

Here is the function in my controller: 这是我控制器中的函数:

vm.frequencyChanged = function () {

    var allFrequencyOptions = vm.ReportFrequencySelections; // myArray
    var selectedfrequencyOptions = [];

    if (vm.reportSettingsData.ReportFrequencyName.ReportFrequencyId === 2) {
        allFrequencyOptions.forEach(function (a) {
            if (a.ReportFrequencySelectionType === 2) {
                selectedfrequencyOptions.push(a)
            }
        });
    }
    else if (vm.reportSettingsData.ReportFrequencyName.ReportFrequencyId === 3) {
        allFrequencyOptions.forEach(function (a) {
            if (a.ReportFrequencySelectionType === 3) {
                selectedfrequencyOptions.push(a)
            }
        });
    }
    else {
        selectedfrequencyOptions = [];
    }

    vm.reportSettingsData.SelectableReportFrequencySelectionNames = selectedfrequencyOptions;
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于Angularjs中的下拉选择(自定义指令)刷新div的内容 - Refreshing contents of div based on drop down selection (custom directive) in Angularjs 根据在另一个 Select 中选择的值过滤 Select(下拉)选项 - Filter Select (Drop Down) Options Based on Value Selected in another Select 基于下拉选择的AngularJs复杂模型 - AngularJs complex model based on drop down selection 选择下拉列表中的AngularJS自定义过滤器触发器 - AngularJS custom filter trigger on select drop down 下拉列表中的angularjs自定义过滤器 - angularjs custom filter in drop-down list 基于另一个下拉选择,如何将新选项附加到bootstrap-multiselect下拉菜单? - Based on another drop down selection how to append new options to bootstrap-multiselect dropdown? 从未来的下拉选项中删除先前的下拉选项 - Remove previous drop down selection from future drop down options 基于以前的下拉Angularjs的选择在下拉列表中混排项目的问题 - issue in shuffling items in drop down based on selection of previous dropdown angularjs angularjs中基于动态行添加+下拉选择 - Dynamic Row add + drop down based selection in angularjs 基于下拉选择的angularJS中的条件模型绑定 - Conditional model binding in angularJS based from the drop down selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM