0

JQuery UI : Autocomplete widget

asked 2014-03-20 08:46:04 +0800

Weasley gravatar image Weasley
11 2

Hi everyone,

I want to add jquery ui widget into a zul file, but i always get the error : TypeError: $(...).autocomplete is not a function

Here is my code:

<vbox vflex="1" hflex="1"
apply="vng.up.me.stats.so.csm.controller.SidebarComposer">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <style src="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
<script type="text/javascript">

    jQuery.noConflict();
    $( document ).ready(function() {
        var availableTags = ["a","a2","a3"];
        console.log($(".searchfield" ));
        $( ".searchfield" ).autocomplete({
          source: availableTags
        });
    });
</script>
<div hflex="1">
    <textbox sclass="searchfield" hflex="1" placeholder="Please insert some text to search" >
    </textbox>
</div>
<div style="background-color:white;overflow:auto;" vflex="1"
    hflex="1">
    <tree id="tree">
    </tree>
</div>

</vbox>

Thanks,

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-03-20 11:03:39 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

updated 2014-03-20 11:56:40 +0800

Actually it's not that simple - although it is quite simple if you understand the logic. You cannot "make" a zk component to behave like a jQueryUI component directly. What you have to do is to declare a "native" jQueryUI component and then use the zAu.send() to post events to the server.

Here is a complete demo page using the jQuery UI auto complete component:

<?page title="Auto Complete Demo" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="winMain" title="Auto Complete Demo" border="normal" xmlns:n="native" width="100%">

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style src="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>

<!-- CLIENT SIDE SCRIPT. NOTICE THAT YOU HAVE TO INIT JQUERY COMPONETS AFTER ZK IS READY -->

<script type="text/javascript">

    zk.afterMount(function() {

        var availableTags = ["Java", "Javascript", "C", "C++", "C#"];

        $("#txtSearchField").autocomplete({
            source: availableTags
        });
    });

    /* 
     This function will be fired when you press CR in the search box.
     The zAu.Send will post an event to the server with the selected search value.   
    */  
    function doSearch() {
        var searchValue = $("#txtSearchField").val();       
        zAu.send(new zk.Event(zk.Widget.$('$winMain'), 'onSearch', searchValue));
    }

</script>

<!-- SERVER SIDE SCRIPT. NORMALLY THIS CODE SHOULD ME PLACED INSIDE A COMPOSER OR A VIEW MODEL -->

<zscript><![CDATA[

    /*
      Create a new event listener to catch the event posted from the client.
      Normally the event handling should be done inside a SelectorComposer or the ViewModel class.
      I am doing this here for simplicity.
    */
    org.zkoss.zk.ui.event.EventListener listener = new org.zkoss.zk.ui.event.EventListener() {
        public void onEvent(Event event) {
            if (event.getName().equals("onSearch")) {
                String searchValue = (String) event.getData();
                txtResult.setValue("Now searching for " + searchValue);
            }
        }
    };      
    winMain.addEventListener("onSearch", listener);

]]></zscript>

<vlayout hflex="true">

    <hlayout>
        <!-- Here is the native jquery component -->
        <n:input id="txtSearchField" placeholder="Search for..." 
                     onkeydown="if (event.keyCode == 13) doSearch()" />

        <label value="(Type something like 'Java', 'C' and press Enter)" />
    </hlayout>

    <label id="txtResult" style="font-weight: bold;" />

</vlayout>

</window>
</zk>

I have a question though... why do you need to do all this when you have zk componets like <combobox> and <listbox> which offer the auto completion functionality out-of-the box? Of course the intergration with native jQuery plugins is very useful if you whish to embed some more complex native widgets which are not offered directly as zk components.

Hope that helps, Costas.

link publish delete flag offensive edit

Comments

Thanks for your help. I don't want to use the combobox or listbox because it only match the pattern from beginning. Example: I have the list of 2 items: abc123 and def456, when i type 'ab' , the combobox suggest correct item. But when I type '23', it doesn't suggest anything.

Weasley ( 2014-03-27 07:27:44 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-03-20 08:46:04 +0800

Seen: 41 times

Last updated: Mar 20 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More