Php+jquery realization of automatic completion of the full-featured source code _php tutorial

Source: Internet
Author: User
The front hand write a pull-down automatic completion function, write simple, only to achieve the mouse selection function, does not support keyboard selection. Because this feature is used in many areas of the project, you need to do it carefully. Discover the functionality of the Select2 plugin to meet current requirements.

In the process of using the jquery plugin select2, there are some doubts, whether it is to wear JSON data or JSONP way to fetch data, can be returned correctly. However, the entries in the drop-down list cannot be selected, and the mouse and keyboard selections are not valid.

It was later found that the Select2 plug-in was selected with the ID field in the data as the standard. Therefore, the data returned by either JSON or JSONP,AJAX must have an ID field. If no such ID exists in the actual database, one can be constructed artificially, but the uniqueness of the ID is guaranteed.

the source code of the template file Try_diy.tpl is listed here
Where the Colum input box is the role of the plug-in, but its return value is the ID, when the page is submitted we need to re-present user-selected sections to the user, my practice is to query the form submitted by the ID of the corresponding layout name name, when the controller received the ID value and non-empty, The name value corresponding to the section ID is rendered on the page at the same time. Since the Select2 plug-in is placing name names in the span element of the constructed inner layer, I write the name value of the hidden field to the span element after the query results page is loaded.
Copy CodeThe code is as follows:














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 by ID
Pm_tpl::assign (' Frmid ', $fid);
Pm_tpl::assign (' Frmname ', $fname);
PM_TPL::d isplay ("Try_diy");
}else
{
PM_TPL::d isplay ("Try_diy");
}
}
Public Function Ajax_diy () {
$fid = $_get[' Q '];
$callback = $_get["Callback"]; The default function is named 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. ') '; /Construction 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, section Id:fid
$data = Array ();
if ($tpid && $fid) {//Constructs an ID field or can be constructed by processing the query result
$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;
}

To add, the actual use for the highlight effect, you can highlight the name contained in the query keyword, which can be select2 in the FormatResult function, can also be processed in PHP. This point of source I do not write here.
Supplementary reference http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html

http://www.bkjia.com/PHPjc/327146.html www.bkjia.com true http://www.bkjia.com/PHPjc/327146.html techarticle The front hand write a pull-down automatic completion function, write simple, only to achieve the mouse selection function, does not support keyboard selection. Because the project needs to use this function in many places, so ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.