Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The example below uses the Autocomplete element of jQueryUI and this sample code下面的例子使用jQueryUI的  Autocomplete元素的 示例代码。 

 

Figure 1 : Adding a Text Field to the Form

First, add a Text Field element to the form and put "city" as field ID.

图1:向表单添加文本字段

首先,添加一个文本字段元素的形式,并把“城市”作为字段ID。

接下来,将自定义HTML元素添加到窗体,并将以下脚本复制到自定义HTML  属性中。 Next, add a Custom HTML element to the form and copy the following script into the Custom HTML property. 

Code Block
<script>
    $(function() {
        $( "[name$=city]" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2
        });
    });
</script>


Figure 2 : Adding a Custom HTML Script to the Form 图2:向表单添加一个自定义的HTML脚本 

Tip

1. By default, the js library file of jQuery and jQueryUI is included in the Joget form.默认情况下,jQuery和jQueryUI的js库文件包含在Joget表单中。
2. The "city" in "[ 代码中“[ name$=city]" in the code must be the same with that in your Field ID.

After adding the script, click on Preview in Form Builder. Type "kua"; the text field will appear like this:

Image Removed
Figure 3 : Preview and Check Autocomplete Field 

 ]”中的“ city ”必须与您的Field ID中的“city” 相同。

添加脚本后,单击 在窗体生成器中的预览。键入“kua”; 文本字段将如下所示:

Image Added

图3:预览和检查自动填充字段 

自动完成文本字段的预览和检查表明它正在工作,虽然看起来有点不漂亮。将下面的CSS添加到自定义HTML改进了自动填充文本字段的显示方式。A preview and check on the Autocomplete text field showed that it is working, although it looked a little unpleasant.  Adding the following CSS to the Custom HTML improved the way the Autocomplete text field looked.

Code Block
<style>
    .ui-autocomplete {
        background:#fff;
        border:#000 1px solid;
        list-style:none;
        padding: 5px;
        width: 250px;
    }
    .ui-menu-item:hover{
        background:#9CE9FF;
    }
</style>


Figure 3 : Completed Auto Complete Field图3:完成的自动完成字段