简体   繁体   English

如何一起应用过滤器?

[英]How to apply filters together?

I have markers on a Google Map with dropdown filters, and these filters work perfectly well individually.我在带有下拉过滤器的谷歌地图上有标记,这些过滤器单独工作得很好。 However, I want the filter to consider all three drop-down lists at once.但是,我希望过滤器一次考虑所有三个下拉列表。

Here is my code: https://jsfiddle.net/h4cv57tb/这是我的代码: https ://jsfiddle.net/h4cv57tb/

Somehow the 3 filter functions should be merged, but unfortunately I have no idea how to ...应该以某种方式合并 3 个过滤器功能,但不幸的是我不知道如何...

Here are the filter functions which should be merged:以下是应该合并的过滤器功能:

//Filter by type
filterMarkers = function(category) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    // If is same category or category not picked
                    if (marker.category == category || category.length === 0) {
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }
    
            //FILTER by ripening time
    
            filterMarkersEres = function(eres) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    var ereshonap = marker.eres;
    
                    // If is same category or category not picked
                    if (ereshonap.includes(parseInt(eres)) || eres.length === 0) { //
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }
    
    
            //FILTER by place
            filterMarkersHely_tipus = function(hely_tipus) {
                for (i = 0; i < gmarkers1.length; i++) {
                    marker = gmarkers1[i];
                    // If is same category or category not picked
                    if (marker.hely_tipus == hely_tipus || hely_tipus.length === 0) {
                        marker.setVisible(true);
                    }
                    // Categories don't match
                    else {
                        marker.setVisible(false);
                    }
                }
            }

Thanks in advance for your help!在此先感谢您的帮助!

Make sure you look at where there is duplicate code and simply combine areas that are different, if feasible.确保查看有重复代码的位置,并在可行的情况下简单地组合不同的区域。 To combine these functions into a single one just combine the if conditions together.要将这些函数组合成一个函数,只需将 if 条件组合在一起即可。

filterMarkers = function(category = null, eres = null, hely_tipus = null) {
    for (i = 0; i < gmarkers1.length; i++) {
        marker = gmarkers1[i];
        ereshonap = marker.eres;
        visible = false;

        if (category){
            if (marker.category == category || category.length === 0) {
            visible = true;
            }
        }

        if (eres) {
            if (ereshonap.includes(parseInt(eres)) || eres.length === 0) {
            visible = true;
            }
        }

        if (hely_tipus) {
            if(marker.hely_tipus == hely_tipus) {
                visible = true;
            }
        }

        if (visible) {
            marker.setVisible(true);
        } else {
            marker.setVisible(false);
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM