How to Apply a Custom Filter

How to apply ag-grid custom filter when clicking a button

Replace this line

getStatusInstance.setModel({
value: "Ford",
});

with this

getStatusInstance.setModel({
filterType: "text",
type: "contains",
filter: "Ford"
});

Here is the params interface TextFilterModel, taking from Ag-Grid docs. You need to pass it like that or the result will not be applied at all.

// text filter uses this filter model
interface TextFilterModel {
// always 'text' for text filter
filterType: string;

// one of the filter options, e.g. 'equals'
type: string;

// the text value associated with the filter.
// it's optional as custom filters may not
// have a text value
filter?: string;
}

Live Demo

Edit AgGrid SetFilter

Creating the most efficient custom filter function (for my use case)

If the types to check are only String or Int create a custom protocol to constrain the value type to String or Int

protocol StringOrInt {}
extension Int : StringOrInt {}
extension String : StringOrInt {}

Then create a Filter struct with a keyPath, a value of StringOrInt and a ComparisonResult which is a common type for <, > and ==

struct Filter {
let value : StringOrInt
let keyPath : PartialKeyPath<Movie>
let ordered : ComparisonResult
}

To filter items create an array of Filter

let filters = [Filter(value: 10, keyPath: \Movie.price, ordered: .orderedAscending),
Filter(value: "Action", keyPath: \Movie.genre, ordered: .orderedSame)]

This will filter all movies with a price < 10 and genre == "Action"


Finally create a filterMovies function which applies the filters to the Movie items in the filter function of Standard Library. The Int values are converted to NSNumber to be able to use compare and compare against the given ComparisonResult

func filterMovies(_ movies : [Movie], by criteria : [Filter]) -> [Movie] {
return movies.filter { movie -> Bool in
for filter in criteria {
let value = filter.value
if let stringValue = value as? String {
if (movie[keyPath: filter.keyPath] as! String).compare(stringValue) != filter.ordered { return false }
} else if let intValue = value as? Int {
let numValue = NSNumber(value: intValue)
let movieInt = movie[keyPath: filter.keyPath] as! Int
if (NSNumber(value: movieInt)).compare(numValue) != filter.ordered { return false }
} else { return false }
}
return true
}
}

How to make a new filter and apply it on an image using cv2 in python2.7?

As far as applying a custom kernel to a given image you may simply use filter2D method to feed in a custom filter. You may also copy the following code to get you going. But The results with current filter seem a bit weird:

import cv2
import numpy as np

# Create a dummy input image.
canvas = np.zeros((100, 100), dtype=np.uint8)
canvas = cv2.circle(canvas, (50, 50), 20, (255,), -1)

kernel = np.array([[-1, -1, -1],
[-1, 4, -1],
[-1, -1, -1]])

dst = cv2.filter2D(canvas, -1, kernel)
cv2.imwrite("./filtered.png", dst)

Input image:

Sample Image

Output Image:

Sample Image

EDIT: As per the edits suggested by @Dolphin, using a kernel with center value of 8, would get the good results in case of circular binary disc.



Related Topics



Leave a reply



Submit