JavaScript search bar with Kendo UI Panelbar

663 Views Asked by At

Can someone please help me with search bar for Kendo UI Panelbar.

  1. When search I want it display on type='program'. Example if I search account the programs that have account words should appear.
  2. Currently it not working at all. It just expand all my panelbar program. :(

Here i provide dojo demo

My javascript

function myFunction() {

var panelbar = $("#panelbar").data("kendoPanelBar");
panelbar.expand($("li", panelbar.element));
//panelbar.collapse($("li", panelbar.element));

var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("panelbar");
li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("li")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }     
}
}

basically my nested list looks like this

<ul id="panelbar">
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search..."> 
<li type="module">Agencies &amp; Groups
    <ul>
    <li type="category">Agency &amp; Booker
    <ul>
    <li type="program">Agency &amp; Booker</li>
    <li type="program">Region</li>
    <li type="program">Sub Region</li>
    </ul>
    </li>
    <li type="program">Agency Category</li>
    <li type="program">Agency Contract</li>
    <li type="program">Agency Overview</li>
    </ul>
</li>   
<li type="module">Call Charge &amp; Billing
    <ul>
    <li type="category">JDS
    <ul>
    <li type="program">JDS Room Mapping</li>
    <li type="program">JDS Room Status</li>
    <li type="program">DS Interface</li>
    </ul>
    </li>
    <li type="category">Operator Panel
    <ul>
    <li type="program">Wake Up Call</li>
    <li type="program">Operator Panel</li>
    </ul>
    </li>       
    <li type="program">Call Code</li>
    <li type="program">Charge Rate</li>
    <li type="program">Property PABX</li>
    <li type="program">Call Transaction</li>
    </ul>
</li>

Here i provide dojo demo

1

There are 1 best solutions below

7
Richard On BEST ANSWER

Type <LI> type attribute is for bullet types. Place your types values (i.e. hierarchy level categorization values) in the class attribute. Change

<li type="category" label=""><span class="k-icon ehors-subfolder-icon"></span>Account's Ledger … 

to

<li class="category" label=""><span class="k-icon ehors-subfolder-icon"></span>Account's Ledger ….

An id value should be unique among all the DOM elements (except maybe radio buttons). The leafs all have spans with the same id value, id="spanpanelbar". Place those in a class attribute as well. Change

<li type="program"><span id="spanpanelbar" class="k-icon ehors-folderopen-icon"></span>General Ledger</li>

to

<li class="program"><span class="spanpanelbar k-icon ehors-folderopen-icon"></span>General Ledger</li>

The search match result processing is changing only the style display property of the leafs. Change the processing to add a class to the <li>, corresponding to the search result condition.

style
.program.nomatch {display:block; color: lightgray } /* or simply display: none */
.program.match   {display:block;  }

javascript
match = $(this).text().match(searchRegEx);
$li = $(this);
$li.toggleClass("match", (match!=null)).toggleClass("nomatch",(match==null));

See this dojo, an update of your original one. It has bonus code for:

  • waiting for typing to stop
  • highlighting the matched fragment

enter image description here

The PanelBar widget is a hierarchical viewer. The items in the path to a program must be displayed in order to see the program. In order to display only paths to found programs you would:

  • At search start set all items to have nomatch class (hide everything)
  • When a match is made, set the program and it's parent items to have match class (unhide path to program)

Example:

    // hide top and middle tier so they wont show if there are no
    // subordinate programs that match

    $("li.category").toggleClass("nomatch", true).toggleClass("match",false);
    $("li.module")  .toggleClass("nomatch", true).toggleClass("match",false);

    // evaluate each program for matching the search term

    $("#panelbar li.program").each(function() { 
      var match, $li, $span, textnode, element;

      match = $(this).text().match(searchRegEx);
      $li = $(this);

      // hide/display programs according to match result

      $li.toggleClass("match", (match!=null))
         .toggleClass("nomatch",(match==null));

      // display items in path when match made

      if (match!=null) {
        $li.parentsUntil("#panelbar", "li")
          .toggleClass("match",true)
          .toggleClass("nomatch",false);
      }

dojo