Home > Backend Development > PHP Tutorial > PHP+jQuery implements automatic completion function source code_PHP tutorial

PHP+jQuery implements automatic completion function source code_PHP tutorial

WBOY
Release: 2016-07-21 15:10:02
Original
1097 people have browsed it

I manually wrote a drop-down auto-completion function earlier. It is simple to write and only implements the mouse selection function and does not support keyboard selection. Since this function is used in many places in the project, it needs to be done carefully. It is found that the function of the select2 plug-in can meet the current needs.

I encountered some doubts when using the jquery plug-in select2. Whether it is passing through json data or fetching data through jsonp, it can be returned correctly. However, the items in the drop-down list cannot be selected, and neither mouse nor keyboard selection is valid.

Later I discovered that the select2 plug-in implements selection based on the id field in the data. So whether it is 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 also construct one manually, but the uniqueness of the ID must be ensured.

Here is the source code of the template file try_diy.tpl:
The column input box is where the plug-in works, but the value returned is the id, which we need after the page is submitted Re-present the section selected by the user to the user. My approach is to query the corresponding section name based on the id submitted in the form. When the controller receives the id value and it is not empty, the name value corresponding to the section id is presented to the user at the same time. displayed on the page. Since the select2 plug-in puts the name in the inner span element of the constructed

, I will write the name value of the hidden field into span element.

Copy code The code is as follows:















Section ID: <{$frmid}>




<script> <br>$(document).ready(function( ) { <br>$('#colum').select2({ <br>minimumInputLength: 0, <br>placeholder: 'Select section', <br>ajax: { <br>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 address of the jsonp request</SPAN> <br> dataType: 'jsonp', <br> jsonp: "callback", // passed to the request handler or page, Used to get the jsonp callback function name (usually the default is: callback, so it can be omitted) <br>                                                                                       use using using               ’ ’ ’ ’ ’ ’ ’ s ’ through ’ s ’ ‐ ‐ ‐ ‐‐‐‐ ​ ​ ​ ​ ​ ​ ​ ​ ​Random function name, you can also write "?", jQuery will automatically process the data <br>quietMillis: 100, <br>data: function(name, page) { <br>return { <br>types: ["exercise"] , <br>limit: -1, <br>q: name <br>}; <br>}, <br>results: function(data, page) { <br>return { results: data.items } <br>} <br>}, <br>formatResult: function(exercise) { <br>return "<div class='select2-user-result'>" + exercise.name + "</div>"; <br>}, <br>formatSelection: function(exercise) { <br>return exercise.name; <br>} <br>}); <br><br>$('#colum').change(function (){ <br>frm.submit(); <br>}); <br>var name = $("#columname").val(); <br>if(name){ <br>$(" #s2id_colum").find("span").text(name); <br>} <br>}); <br></script>


The following is an example of a controller:

Copy the code The 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);//根据id取name
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"]; //默认函数名为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.')';//构造jonsp
exit();
}
}
?>

model方法:
复制代码 代码如下:

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)
{
//产品ID:tpid,版块ID:fid
$data = array();
if($tpid && $fid){//构造一个id字段,也可以通过对查询结果加工构造
$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;
}

补充一下,实际使用为突出提示效果,可以高亮显示name中包含的查询关键字,这一点可以再select2的formatResult函数中处理,也可以在php中进行加工处理。这一点 的源码我这里就不写了。
补充参考http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327146.htmlTechArticle前面手工写了一个下拉自动补全功能,写的简单,只实现了鼠标选择的功能,不支持键盘选择。由于项目很多地方要用到这个功能,所以需...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template