Previously, I manually wrote a drop-down auto-completion function, which is simple to write. It only supports the mouse selection function and does not support keyboard selection. This function is used in many projects, so you need to do it carefully. It is found that the function of the select2 plug-in can meet the current needs.
When using the jquery plug-in select2, I encountered some questions. Both json data and jsonp data retrieval can be returned correctly. However, the entries in the drop-down list cannot be selected, and the selection of the mouse and keyboard is invalid.
Later, it was found that the select2 plug-in was selected based on the id field in the data. Therefore, no matter json or jsonp, the data returned by ajax must have an id field. If such an id does not exist in the actual database, you can create one manually, but ensure the uniqueness of the id.
The source code of the template file try_diy.tpl is listed here.:
The colum input box serves as the plug-in, but the returned value is id. After the page is submitted, we need to re-present the selected forum to the user, my approach is to query the corresponding layout name based on the id submitted by the form. When the Controller receives the id value and is not empty, the name value corresponding to the Forum id is displayed on the page at the same time. Because the select2 plug-in places the name in the span element of the <div id = "s2id_colum"> </div> constructed, therefore, I will write the name value of the hidden domain to the span element after loading the query result page.Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript" src = "http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"> </script>
<Link href = "./static/js/select2-3.3.2/select2.css" rel = "stylesheet"/>
<Script src = "./static/js/select2-3.3.2/select2.js"> </script>
</Head>
<Body>
<Form name = "frm" id = "frm" autocomplete = "off" action = "" method = "post">
<Input name = "c" type = "hidden" value = "try"/>
<Input name = "a" type = "hidden" value = "diy"/>
<Label> Apsara stack product section (Ajax call remote data): </label>
<Input type = "hidden" class = "bigdrop" id = "colum" name = "colum" style = "width: 600px" value = ""/>
<Input type = "hidden" id = "columname" value = "<{$ frmname}>" name = "columname"/>
<P> Forum ID: <{$ frmid}> </p>
</Form>
<Script>
$ (Document). ready (function (){
$ ('# Colum'). select2 ({
MinimumInputLength: 0,
Placeholder: 'select a forum ',
Ajax :{
Url: "http://pm.feiliu.com /? C = try & a = ajax_diy ", <SPAN style =" BACKGROUND-COLOR: # f5f5f5; COLOR: #008000 "> // </SPAN> <SPAN style =" BACKGROUND-COLOR: # f5f5f5; COLOR: #008000 "> provide the url of the jsonp request </SPAN>
DataType: 'jsonp ',
Jsonp: "callback", // The name passed to the request handler or page to obtain the jsonp callback function (generally: callback by default, so it can be omitted)
// JsonpCallback: "testback ",
// JsonpCallback is the custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. It can also be written "? ", JQuery automatically processes the data
QuietMillis: 100,
Data: function (name, page ){
Return {
Types: ["exercise"],
Limit:-1,
Q: name
};
},
Results: function (data, page ){
Return {results: data. items}
}
},
FormatResult: function (exercise ){
Return "<div class = 'select2-user-result'>" + exercise. name + "</div> ";
},
FormatSelection: function (exercise ){
Return exercise. name;
}
});
$ ('# Colum'). change (function (){
Frm. submit ();
});
Var name = $ ("# columname"). val ();
If (name ){
$ ("# S2id_colum"). find ("span"). text (name );
}
});
</Script>
</Body>
</Html>
The following is an example of a controller:Copy codeThe Code is as follows: class pmc_try
{
Public function diy (){
If ($ _ POST ['colum '])
{
$ Fid = $ _ POST ['colum '];
$ Fname = mod_forum: get_forum_name_by_fid ($ fid); // name based on id
Pm_tpl: assign ('frmid ', $ fid );
Pm_tpl: assign ('frmname', $ fname );
Pm_tpl: display ("try_diy ");
} Else
{
Pm_tpl: display ("try_diy ");
}
}
Public function ajax_diy (){
$ Fid = $ _ GET ['q'];
$ Callback = $ _ GET ["callback"]; // The default function name is callback.
$ Forums = mod_forum: get_ajx_forum_by_tpid ($ fid );
$ Total = count ($ forums );
$ Result = array (
'Total' => $ total,
'Items '=> $ forums
);
$ Output = json_encode ($ result );
Echo $ callback. '('. $ output. ')'; // construct jonsp
Exit ();
}
}
?>
Model Method:Copy codeThe Code is as follows: public static function get_forum_name_by_fid ($ fid)
{
$ SQL = "SELECT name FROM sq_forums WHERE fid = '$ fid' ORDER BY threads_counter DESC ";
$ Data = pm_db11: result_first ($ SQL );
Return $ data;
}
Public static function get_ajx_forum_by_tpid ($ tpid, $ fid)
{
// Product ID: tpid, Forum ID: fid
$ Data = array ();
If ($ tpid & $ fid) {// construct an id field, which can also be constructed by processing the query results
$ SQL = "SELECT fid AS id, fid, name FROM sq_forums WHERE tpid = '$ tpid' AND name LIKE n' % $ fid % 'order BY threads_counter DESC ";
$ Query = pm_db11: query ($ SQL );
$ Data = pm_db11: fetch_all ($ query );
}
Return $ data;
}
In addition, in actual use, the keyword in name can be highlighted for the highlighted effect, which can be processed in the formatResult function of select2 or in php. I will not write the source code here.
Additional reference http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html