How to implement filter search result with JSONP result?
I have seen this done in many places and I like to implement this feature in my search result. I have a page that shows all the result of a search term(s). On the same page, there are two tabs (All and Refine) and the by default all the search results will be displayed in the All tab. Once the user clicked on this "Refine" tab, put check marks on a bunch of checkbox (search result's parent) that he's interested in narrowing down his search. He clicked the submit/filter button and the search results in the "All" tab automatically shrink/increase base on how many of those checkbox he checked. The checkbox value's attribute contains the value of each search result's parent ID. So the number of results are based on if their parent field containd the same ID as the ones checked by the user. Sorry if I didn't explain this better. If you can point me to video tutorials or links that will be great. Below is the code for processing the search, shows the results, and display the result's parent name.
- var searchValue = urlParams[queryParamName];
- //make an API call to search third party API
- var myExp = new RegExp(searchValue, "i");
- $.getJSON('/customcf/example/ajaxProxy-KB.cfm', {
- proxyURL: 'https://app.example.com/api/head/suggest.json',
- // type: 'get',
- name: {
- $regex: searchValue,
- //$options: "i"
- }
- },
- function(data) {
- var output = '<div class="koSearchResult">';
- var technology = "";
- var refineSearch = "";
- var studentForms = "";
- var D2L = "";
- var arrayParent = [];
- $.each(data.data, function(i, article) {
- var txtSum = article.summary.toString();
- var txtTitle = article.name;
- var re = new RegExp('\\b(' + searchValue + ')\\b', 'gi');
- txtSum = txtSum.replace(re, '<span>$1</span>');
- txtTitle = txtTitle.replace(re, '<span>$1</span>');
- output += '<h6>';
- output += '<a href="../kb/article/'+ article.url_hash + '">' + txtTitle + '</a>';
- if(article.inherited_roles !== null || article.reader_roles != "")
- {
- output += " <img src='../images/Lock-icon.png' />";
- }
- output += '</h6>';
- output += '<p>' + txtSum + '</p>';
-
- //go through the parents fields array and store all unique parent IDs in an array
- $.each(article.parents, function(index, value){
- if($.inArray(value, arrayParent) < 0){
- arrayParent.push(value);
- }
- });
-
- });
-
- $.getJSON('https://app.example.com/api/head/category.jsonp?callback=?', {
- _authbykey: "572244a232131c887c11d11d",
- _fields: ["name"],
- id:{
- "$in": arrayParent
- },
- },
- function(dataParent) {
- //console.log("data length: ", dataParent.data.length);
- refineSearch = "<fieldset><legend>Refine your search</legend>";
- $.each(dataParent.data, function(i, parent) {
- refineSearch += '<input type="checkbox" name="' + parent.name.en + '" value="' + parent.id + '" />' + ' ' + parent.name.en + '<br />';
- });
-
- refineSearch += "</fieldset>";
- //this is putting the parents name and ID in the Refine search tab
- $("##refineYourSearch").replaceWith(refineSearch);
- }).fail(function(err){
- console.log("fail",err);
- });
-
- output += '</div>';
- $("##resultarea").replaceWith(output);
-
- }); //get JSON