OpenLayers 4.6.4中如何程序化定义ol.format.filter.and过滤条件?
ol.format.filter.and Conditions in OpenLayers 4.6.4 Great question! Moving from hardcoded filters to dynamic, programmatic ones gives you way more flexibility—here's how to do it cleanly with OpenLayers 4.6.4:
Core Approach
Instead of writing filter conditions directly inside ol.format.filter.and(), you can build an array of filter objects dynamically (based on variables, user input, or other logic) and then pass that array to the and() function (using the spread operator ... since and() accepts multiple arguments).
Example 1: Basic Dynamic Filter Variables
Let’s start by replacing your hardcoded values with reusable variables, then assemble the filters:
// Define filter parameters as variables (could come from user input, config, etc.) const filterParams = { startDate: '2015-11-27T05:00:00Z', endDate: '2015-12-31T05:00:00Z', category: 'Church' }; // Build an array of filter conditions const filterConditions = [ ol.format.filter.during('DATE', filterParams.startDate, filterParams.endDate), ol.format.filter.equalTo('Category', filterParams.category) // Note: Fixed typo—OL 4 uses `equalTo` instead of `exactTo` ]; // Create the feature request with the dynamic AND filter const featureRequest = new ol.format.WFS().writeGetFeature({ srsName: 'EPSG:4326', featureNS: 'http://myserver', featurePrefix: 'locations', featureTypes: ['photos'], outputFormat: 'application/json', filter: ol.format.filter.and(...filterConditions) // Spread the array into individual arguments });
Example 2: Conditionally Adding Filters
If you need to include filters only when certain criteria are met (e.g., a user selects a category or date range), build the condition array incrementally:
// Start with an empty array of conditions const filterConditions = []; // Add date range filter only if start and end dates are provided const startDate = getUserSelectedStartDate(); // Imagine this fetches user input const endDate = getUserSelectedEndDate(); if (startDate && endDate) { filterConditions.push(ol.format.filter.during('DATE', startDate, endDate)); } // Add category filter only if a category is selected const selectedCategory = getUserSelectedCategory(); if (selectedCategory) { filterConditions.push(ol.format.filter.equalTo('Category', selectedCategory)); } // Handle case where no filters are applied (optional) let filter; if (filterConditions.length > 0) { filter = ol.format.filter.and(...filterConditions); } else { // Optional: Use a wildcard filter to match all features, or omit the filter parameter filter = ol.format.filter.like('Category', '*'); } // Create the feature request const featureRequest = new ol.format.WFS().writeGetFeature({ srsName: 'EPSG:4326', featureNS: 'http://myserver', featurePrefix: 'locations', featureTypes: ['photos'], outputFormat: 'application/json', filter: filter });
Key Notes
- Typo Fix: In your original code,
exactToisn’t a valid OpenLayers 4 method—useequalTofor exact matches. - Flexibility: This approach lets you add/remove conditions on the fly without rewriting the entire filter block.
- Empty Filters: If your condition array is empty, you can either omit the
filterparameter entirely to retrieve all features, or use a wildcard filter as shown.
内容的提问来源于stack exchange,提问作者Apocalypse911

