Select Filtered Search Results

Select Filtered search results

I think that the issue is that you are tracking whether you are searching or not yourself and manipulating the source data array.

I have an example playground snippet I have used for some other answers which shows you how to do this more efficiently and has some handy helper functions to make it easier.


I think the key parts of this that would be beneficial to you are:

keep a seperate array just of search results, only used when search is active

var filteredNames = [String]()

Make sure that you adjust the count and rows used when searching or not

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredNames.count
} else {
return names.count
}
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell() // don't do this, i am for example.

var name: String
if isFiltering() {
name = filteredNames[indexPath.row]
} else {
name = names[indexPath.row]
}

cell.textLabel?.text = name
return cell
}

You can use the following helper functions to help you out.

func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
}

func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}

Full UISearchController Example (playground)

import UIKit
import PlaygroundSupport

class ViewController: UITableViewController {

let searchController = UISearchController(searchResultsController: nil)

var names = [
"John",
"Terry",
"Martin",
"Steven",
"Michael",
"Thomas",
"Jason",
"Matthew"
]
var filteredNames = [String]()

override func viewDidLoad() {
super.viewDidLoad()

self.title = "Search Example"

searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredNames.count
} else {
return names.count
}
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell() // don't do this, i am for example.

var name: String
if isFiltering() {
name = filteredNames[indexPath.row]
} else {
name = names[indexPath.row]
}

cell.textLabel?.text = name
return cell
}

func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
}

func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}

func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredNames = names.filter({( name : String) -> Bool in
return name.lowercased().contains(searchText.lowercased())
})

tableView.reloadData()
}
}

extension ViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}

let vc = ViewController()
let nav = UINavigationController()
nav.viewControllers = [vc]

PlaygroundPage.current.liveView = nav

How to filter the result of a SELECT query further?

Your 1st query returns only 1 column but in the 2nd you try to filter by 2 columns.

So include both columns is your 1st query:

SELECT t.* FROM (
SELECT student_payment, student_unlock
FROM student LEFT JOIN student_state
ON student.student_id = student_state.student_id_fk
WHERE student_surname LIKE '%XXX%' OR student_lastname LIKE '%XXX%'
) t
WHERE t.student_payment LIKE '%XXX%' OR t.student_unlock LIKE '%XXX%'

You can get the same results with this:

SELECT student_payment, student_unlock 
FROM student LEFT JOIN student_state
ON student.student_id = student_state.student_id_fk
WHERE
(student_surname LIKE '%XXX%' OR student_lastname LIKE '%XXX%')
AND
(t.student_payment LIKE '%XXX%' OR t.student_unlock LIKE '%XXX%')

How to select filtered results on a button click from datatable using jquery

you can get the filtered rows by using:

var rows = $(".datatable").dataTable().$('tr', {"filter":"applied"});

then you can iterate each row and apply what you need
check https://datatables.net/forums/discussion/18375/get-all-filtered-rows-across-all-pages
for more info

How can I get results of one search query filter only in Laravel

Did you try to filter the $value before stacking them in your where?

    if (!empty($value)) {

}

empty() will take care of filtering out the empty and null values. So you end up adding to your query filter values that actually exists.

EDIT

    foreach ($request->all() as $filterName => $value) {

if (!empty($value)) {
$decorator = static::createFilterDecorator($filterName);

if (static::isValidDecorator($decorator)) {
$query = $decorator::apply($query, $value);
}
}
}

EDIT 2

Your option tag has no value attribute defined and there for is not empty when posted to your controller because the browser defaults to the value inside the tag (Select Type, Select Category ect). You need to provide the value attribute as empty so that the POST does not use the inner text like this:

<option value="">Select Type</option>
[...]
<option value="">Select Category</option>
[...]
<option value="">Select Location</option>
[...]
<option value="">Select Budget</option>

Then if (!empty($value)) {} can do it's thing

Multiple Selection while searchController is filtering

I was able to do this once I set

tableView.allowsMultipleSelectionDuringEditing = true

along with

tableView.allowsMultipleSelection = true

Select2 choosing all options from filtered list for multiple selection

I have tried getting the filtered visible values from the select dropdown. select2 plugin stores all the plugin data in the data() object. Here you can the results from $("#select").data().

All you have to do is iterate over the filtered result elements (li tag) in the data object and trigger mouseup event for all those results elements.

Try below working example.

<html>
<head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css">
<style id="compiled-css" type="text/css">
</style>
<script> $(document).ready(function() { $('#state').select2(); $('#all_visible').click(function(event) { event.preventDefault(); var visible = null; // TODO: how do we grab the filtered list? // $('#state').val(visible).trigger('change');
// Get the data object of state dropdown var filteredValues = []; var select2Holder = $('#state').data();
if (select2Holder && select2Holder.select2) { // Find the $results object var results = select2Holder.select2.$results; if (results) { // var resultNode$ = $(results[0]); // console.log(resultNode$); if ($(results[0]) && $(results[0]).children().length > 0) { $(results[0]).children().each(function(index, node) { var selectedValue; // Get the option value from attribute var id = $(node).attr('id'); // id attribute will be like this 'select2-state-result-x3b0-NH' // split the id attribute using "-" and get last value var arr = id.split('-'); if (arr && arr.length > 0) { selectedValue = arr[arr.length -1 ]; filteredValues.push(selectedValue); }
$(node).trigger('mouseup'); }) } }
} }); }) </script>
</head>
<body > <select name='state' id='state' multiple='multiple'> <option value='null'> - Please Select - </option> <option value='AL'>Alabama</option> <option value='AK'>Alaska</option> <option value='AZ'>Arizona</option> <option value='AR'>Arkansas</option> <option value='CA'>California</option> <option value='CO'>Colorado</option> <option value='CT'>Connecticut</option> <option value='DE'>Delaware</option> <option value='DC'>District Of Columbia</option> <option value='FL'>Florida</option> <option value='GA'>Georgia</option> <option value='HI'>Hawaii</option> <option value='ID'>Idaho</option> <option value='IL'>Illinois</option> <option value='IN'>Indiana</option> <option value='IA'>Iowa</option> <option value='KS'>Kansas</option> <option value='KY'>Kentucky</option> <option value='LA'>Louisiana</option> <option value='ME'>Maine</option> <option value='MD'>Maryland</option> <option value='MA'>Massachusetts</option> <option value='MI'>Michigan</option> <option value='MN'>Minnesota</option> <option value='MS'>Mississippi</option> <option value='MO'>Missouri</option> <option value='MT'>Montana</option> <option value='NE'>Nebraska</option> <option value='NV'>Nevada</option> <option value='NH'>New Hampshire</option> <option value='NJ'>New Jersey</option> <option value='NM'>New Mexico</option> <option value='NY'>New York</option> <option value='NC'>North Carolina</option> <option value='ND'>North Dakota</option> <option value='OH'>Ohio</option> <option value='OK'>Oklahoma</option> <option value='OR'>Oregon</option> <option value='PA'>Pennsylvania</option> <option value='PR'>Puerto Rico</option> <option value='RI'>Rhode Island</option> <option value='SC'>South Carolina</option> <option value='SD'>South Dakota</option> <option value='TN'>Tennessee</option> <option value='TX'>Texas</option> <option value='UT'>Utah</option> <option value='VT'>Vermont</option> <option value='VA'>Virginia</option> <option value='WA'>Washington</option> <option value='WV'>West Virginia</option> <option value='WI'>Wisconsin</option> <option value='WY'>Wyoming</option></select><input type='button' id='all_visible' value='Select Visible' /></body>
</html>


Related Topics



Leave a reply



Submit