{"id":1095,"date":"2011-04-24T16:17:31","date_gmt":"2011-04-24T14:17:31","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1095"},"modified":"2011-04-24T16:19:32","modified_gmt":"2011-04-24T14:19:32","slug":"search-refiners-part-4-user-selection-based","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-4-user-selection-based\/","title":{"rendered":"Search Refiners (part 4) \u2013 User selection based"},"content":{"rendered":"<p>In this series:\u00a0\u00a0<\/p>\n<ol>\n<li>Search Refiners part 1 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-1-expanding-the-ootb-search-refinement-panel\/\">Expanding the OOTB search Refinement Panel <\/a><\/li>\n<li>Search Refiners part 2 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-2-use-of-customfilters\/\">Use of CustomFilters <\/a><\/li>\n<li>Search Refiners part 3 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-3-chart-based\/\">Chart based<\/a><\/li>\n<li>Search Refiners part 4 &#8211; User selection based (this post)<\/li>\n<\/ol>\n<p>After building a chart based search refiner, now it&#8217;s time to get some interaction in the selection of search terms. In this post I will show you how to use jQuery in combination with the RefinementManager.<br \/>\nWith interaction I mean the user can drag a search term to a place on the screen to refine the results. Unfortunately the OOTB Refinement Panel and the results webpart &#8216;talk&#8217; to each other by the parameters in the url. Therefor a postback must occur to actually refine the results with the selected term. But a little animation just looks great.\u00a0<\/p>\n<p>For the drag and drop functionality I used the standard jQuery library and a custom jQuery UI selection. With this functionality in hand I created my own drag and drop implementation for this specific solution.\u00a0<\/p>\n<h3>Script part<\/h3>\n<p>Because I already have a little experience with programming against the RefinementManager class in the chart based example I first focused on the drag drop functionality.<br \/>\nTwo divs are placed on the screen:<br \/>\n1. to list all the terms the user can choose from to select<br \/>\n2. the already selected terms by the user\u00a0<\/p>\n<p>The items in both divs has to have the ability to drag items from and to drop items to. The user has to have the possibility to drag items from the &#8216;terms to select&#8217; part to the &#8216;selected terms&#8217; part and vice versa.<br \/>\nBoth the divs contain an unordered list with list items.<br \/>\nSo a single div with items in it looks in plain HTML like:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div&gt;\r\n&lt;ul&gt;\r\n&lt;li&gt;item01&lt;\/li&gt;\r\n&lt;li&gt;item02&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>A single item has to be draggable to move it from the &#8216;terms to select&#8217; to the &#8216;selected terms&#8217; part(and vice versa). So the li element has to be draggable.<br \/>\nOn the other hand when a li element a dragged, the list to which the single item has to be added has to be droppable, the ul element.\u00a0<\/p>\n<p>To accomplish this just a few lines of jQuery are neccesary, the rest is alredy build in the jQuery libraries.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0\u00a0 var $currentSelectedItems = $(&quot;#selectedTerms&quot;),\r\n\u00a0$baseItemList = $(&quot;#termsToSelect&quot;);&lt;\/pre&gt;\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0 \/\/make the li's in the selectedTerms div draggable\r\n\u00a0\u00a0\u00a0 $(&quot;li&quot;, $currentSelectedItems).draggable({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 appendTo: $baseItemList,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 helper: &quot;clone&quot;\r\n\u00a0\u00a0\u00a0 });\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0 \/\/make the ul in the selectedTerms div droppable\r\n\u00a0\u00a0\u00a0 $(&quot;ul&quot;, $currentSelectedItems).droppable({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 accept: &quot;#termsToSelect li&quot;,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hoverClass: &quot;ui-state-hover&quot;,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 drop: function (event, ui) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 moveTerm(ui.draggable, $currentSelectedItems);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 });\r\n<\/pre>\n<p>The draggable functionality is nothing special. The element passed to appendTo is the container during dragging. Second the clone helper is used here, which means a clone of the actual item is dragged around. Another option here is original, which drags the original item around.\u00a0<\/p>\n<p>The droppable functionality uses different options. First I&#8217;m telling the element what items to accept: an li element within &#8216;termsToSelect&#8217;. Second a nice hover class and finally a function is specified what to do when the actual drop occurs: move the term to from &#8216;terms to select&#8217; to &#8216;selected terms&#8217; and navigate to the url which the item received from the RefinementPanel (getting there soon).\u00a0<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0\u00a0 \/\/move term\r\n\u00a0\u00a0\u00a0 function moveTerm($item, $listToAdjust) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $item.fadeOut(function () {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var $list = $(&quot;ul&quot;, $listToAdjust);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $item.appendTo($list).fadeIn();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 window.location.href = $item.find('a').attr(&quot;href&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\u00a0\u00a0\u00a0 }\r\n<\/pre>\n<p>\u00a0\u00a0<\/p>\n<p>The script above is the script which act on the &#8216;selected terms&#8217; part ($currentSelectedItems or $(&#8220;#selectedTerms&#8221;)). The same functionality have to be added to the &#8216;terms to select&#8217; part. Because it&#8217;s almost the same this code it&#8217;s left out here for readability.\u00a0<\/p>\n<p>That&#8217;s is with all the script, let&#8217;s move the code behind all this.\u00a0<\/p>\n<h3>The webpart<\/h3>\n<p>The code behind this (just a regular webpart) works with the RefinmentManager class of the page as in the chart based example, the previous post in this series. The extra functionality here is that the selected term is needed from the RefinmentManager&#8217;s filter. If a term is selected or not is just another node in the refinement xml, just as Value and Url as shown in the following code listing:<br \/>\n\u00a0<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring filterValue = filter.SelectSingleNode(&quot;Value&quot;).InnerText;\r\nstring filterUrl = filter.SelectSingleNode(&quot;Url&quot;).InnerText;\r\n\u00a0\r\n\r\nif (filter.SelectSingleNode(&quot;Selection&quot;).InnerText.Equals(&quot;Selected&quot;))\r\n{\r\n\u00a0\u00a0\u00a0 dateSelectedList.Add(new ListItem(filterValue, filterUrl));\r\n}\r\nelse\r\n{\r\n\u00a0\u00a0\u00a0 dateToSelectList.Add(new ListItem(filterValue, filterUrl));\r\n}\r\n<\/pre>\n<p>dateSelectedList and dateToSelectList are both generic lists of ListItems and in the RenderContents method the items in the lists are added to the appropriate containers.\u00a0<\/p>\n<p>After putting this all together it looks like the folowing image when no selection are made by the user:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4NoSelection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1099\" title=\"SearchrefinerPart4NoSelection\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4NoSelection.png\" alt=\"\" width=\"205\" height=\"178\" \/><\/a><\/p>\n<p>When dragging starts from &#8216;terms to select&#8217; the location where to drag to is highlighted:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4StartDragging1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1101\" title=\"SearchrefinerPart4StartDragging\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4StartDragging1.png\" alt=\"\" width=\"289\" height=\"185\" \/><\/a><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4StartDragging.png\"><\/a>\u00a0<\/p>\n<p>Because &#8216;helper&#8217; was set to &#8216;clone&#8217; earlier the item that&#8217;s dragged, &#8216;Past Week&#8217;, is still in the &#8216;terms to select&#8217; container and also moving around the screen.\u00a0<\/p>\n<p>When dropping the item for a moment the screen looks like (because of the &#8216;appendto&#8217; option):<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4AfterDragging.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1102\" title=\"SearchrefinerPart4AfterDragging\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4AfterDragging.png\" alt=\"\" width=\"250\" height=\"177\" \/><\/a>\u00a0\u00a0<\/p>\n<p>And then a postback occurs and it looks like:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4DoneDragging.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1103\" title=\"SearchrefinerPart4DoneDragging\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/04\/SearchrefinerPart4DoneDragging.png\" alt=\"\" width=\"285\" height=\"159\" \/><\/a>\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>And the terms displayed behave just like the OOTB refiners.\u00a0\u00a0\u00a0\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>The OOTB refinement functionality is great, but it can be made much more attractive with just a little script as shown in this post. What I&#8217;ve shown you here is just a little example with one of the possibilities to make it more attractive. There are a lot of jQuery libraries with excellent functionality which you can use to do all kinds of stuff and don&#8217;t forget the design. This example looks nothing like a nice production ready refiner, but go wild and make it look awesome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this series:\u00a0\u00a0 Search Refiners part 1 &#8211; Expanding the OOTB search Refinement Panel Search Refiners part 2 &#8211; Use of CustomFilters Search Refiners part 3 &#8211; Chart based Search Refiners part 4 &#8211; User selection based (this post) After &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-4-user-selection-based\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,11,42],"class_list":["post-1095","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-jquery","tag-sharepoint-2010"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1095"}],"version-history":[{"count":13,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1095\/revisions"}],"predecessor-version":[{"id":1113,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1095\/revisions\/1113"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}