ตัวอย่างโค้ด พร้อมคำอธิบายต่อไปนี้ เป็นแนวทางการใช้งาน jQuery UI สำหรับสร้าง autocomplete โดยเรียกใช้ jQuery UI และ theme จาก google และเรียกใช้งาน jQuery จาก jquery.com
autocomplete ตามโค้ดตัวอย่างด้านล่าง เป็นการเรียกใช้งานจากค่าที่กำหนดตายตัวด้วย javascript เหมาะกับการใช้งานกับข้อมูลจำนวนไม่มากนัก หรือข้อมูลที่ไม่ต้องติดต่อกับฐานข้อมูล
ตัวอย่าง
คำอธิบายแสดงไว้ในโค้ดตัวอย่าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title>jquery ui autocomplete</title> <style type= "text/css" > body { font-family:tahoma, "Microsoft Sans Serif" , sans-serif, Verdana; font-size:12px; } </style> <?php $jquery_ui_v = "1.8.5" ; $theme = array ( "0" => "base" , "1" => "black-tie" , "2" => "blitzer" , "3" => "cupertino" , "4" => "dark-hive" , "5" => "dot-luv" , "6" => "eggplant" , "7" => "excite-bike" , "8" => "flick" , "9" => "hot-sneaks" , "10" => "humanity" , "11" => "le-frog" , "12" => "mint-choc" , "13" => "overcast" , "14" => "pepper-grinder" , "15" => "redmond" , "16" => "smoothness" , "17" => "south-street" , "18" => "start" , "19" => "sunny" , "20" => "swanky-purse" , "21" => "trontastic" , "22" => "ui-darkness" , "23" => "ui-lightness" , "24" => "vader" ); $jquery_ui_theme = $theme [11]; ?> <link type= "text/css" rel= "stylesheet" href= "//ajax.googleapis.com/ajax/libs/jqueryui/<?=$jquery_ui_v?>/themes/<?=$jquery_ui_theme?>/jquery-ui.css" /> <style> /* css ส่วนของรายการที่แสดง */ .ui-autocomplete { /* width:230px;*/ /* max-height:200px; overflow:scroll;*/ } .ui-button { margin-left: -1px; } .ui-button-icon-only .ui-button-text { padding: 0.35em; } /* css ส่วน textbox */ .ui-autocomplete-input { font-family:tahoma, "Microsoft Sans Serif" , sans-serif, Verdana; font-size:12px; height:18px; width:200px; } /* css ส่วนปุ่มคลิกเลือกแสดงรายการทั้งหมด*/ .myselect{ /* border:0px solid;*/ width:20px; height:25px; } </style> </head> <body> <label for = "tags" >Tags: </label> <input id= "tags" /> <script class = "jsbin" src= "//ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" ></script> <script type= "text/javascript" > $( function () { // กำหนดค่าแบบตายตัวสำหรับ ใช้งาน autocomplete var availableTags = [ "ActionScript" , "AppleScript" , "Asp" , "BASIC" , "C" , "C++" , "Clojure" , "COBOL" , "ColdFusion" , "Erlang" , "Fortran" , "Groovy" , "Haskell" , "Java" , "JavaScript" , "Lisp" , "Perl" , "PHP" , "Python" , "Ruby" , "Scala" , "ทดสอบข้อความภาษาไทย" ]; $( "#tags" ).autocomplete({ // ใช้งาน autocomplete กับ input text id=tags minLength: 0, // กำหนดค่าสำหรับค้นหาอย่างน้อยเป็น 0 สำหรับใช้กับปุ่ใแสดงทั้งหมด source: availableTags, // กำหนดให้ใช้ค่าจากตัวแปรที่กำหนดข้างต้น open: function (){ // เมื่อมีการแสดงรายการ autocomplete var valInput=$(this).val(); // ดึงค่าจาก text box id=tags มาเก็บที่ตัวแปร if (valInput!= "" ){ // ถ้าไม่ใช่ค่าว่าง $( ".ui-menu-item a" ).each( function (){ // วนลูปเรียกดูค่าทั้งหมดใน รายการ autocomplete var matcher = new RegExp( "(" +valInput+ ")" , "ig" ); // ตรวจสอบค่าที่ตรงกันในแต่ละรายการ กับคำค้นหา var s=$(this).text(); var newText=s.replace(matcher, "<b>$1</b>" ); // แทนค่าที่ตรงกันเป็นตัวหนา $(this).html(newText); // แสดงรายการ autocomplete หลังจากปรับรูปแบบแล้ว }); } } }); // สร้างปุ่ม สำหรับคลิกแสดงรายการทั้งหมด $( "<button> </button>" ) .attr( "tabIndex" , -1 ) // กำหนด tabindex .ให้กับปุ่ม .attr( "title" , "Show All Items" ) // กำหนด title เมื่อเลื่อนเมาส์มาอยู่เหนือปุ่ม .addClass( "myselect" ) // ให้ปุ่มนี้มี class=myselect .insertAfter( "#tags" ) // แทรกปุ่มนี้ต่อจาก input text id=tags .button({ // กำหนด รูปแบบของปุ่ม icons: { primary: "ui-icon-triangle-1-s" }, text: false // กำหนดไม่แสดงข้อความใดๆ ในปุ่ม }) .removeClass( "ui-corner-all" ) .addClass( "ui-corner-right ui-button-icon" ) .click( function () { // เงื่อนไขเมื่อคลิกที่ปุ่มนี้ // ตรวจสอบถ้ามีการแสดงรายการทั้งหมดอยู่แล้ว if ($( "#tags" ).autocomplete( "widget" ).is( ":visible" ) ) { $( "#tags" ).autocomplete( "close" ); // ปิดการแสดงรายการทั้งหมด return ; } // ส่งค่าว่างปล่าวไปทำการค้นหา จะได้ผลลัพธ์เป็นรายการทั้งหมด $( "#tags" ).autocomplete( "search" , "" ); $( "#tags" ).focus(); //ให้ cursor ไปอยู่ที่ input text id=tags }); }); </script> </body> </html> |