jQuery.Autocomplete实现自动完成功能
html代码
<div class="conditions staff ue-clear">
<label>公司类型:</label>
<input type="text" id="companytypename"/>
</div>
js代码
$.ajax({
type: "Post",
url: "/company/companycheck/getacnames",
data: {table:"dictecontypes",code:"code",name:"enterregtype",page: 1, pagesize: 100000},
success: function (res) {
var r = $.parseJSON(res);
if (r != null && r.ret == 0) {
$("#companytypename").autocomplete(r.aaData, {
max: 12, //列表里的条目数
minChars: 0, //自动完成激活之前填入的最小字符
width: 200, //提示的宽度,溢出隐藏
scrollHeight: 300, //提示的高度,溢出显示滚动条
matchContains: true,//包含匹配,就是data参数的数据,是否只要包含文本框的内容就显示
autoFill: false,//自动填充
formatItem: function (row, i, max) {
return row.name;
},
formatMatch: function (row, i, max) {
return row.name;
},
formatResult: function (row) {
return row.name;
}
}).result(function (event, row, formatted) {
$("#companytypename").val(row.name);
});
}
else {
alert(r.msg);
}
}
});
});
php代码
public function getacnames()
{
if (!isset($_POST["table"])) {
$re["ret"] = 1;
$re["msg"] = "no table!";
echo json_encode($re);
exit;
}
if (!isset($_POST["name"])) {
$re["ret"] = 1;
$re["msg"] = "no name!";
echo json_encode($re);
exit;
}
$aColumns = array();
$table = $_POST["table"];
unset($_POST["table"]);
if (isset($_POST["code"])) {
$code = $_POST["code"];
unset($_POST["code"]);
$aColumns[] = $code;
}
$name = $_POST["name"] . " as name";
unset($_POST["name"]);
$aColumns[] = $name;
$arr = array();
if (isset($_POST["page"]) && isset($_POST["pagesize"])) {
$page = $_POST["page"];
$pagesize = $_POST["pagesize"];
unset($_POST["page"]);
unset($_POST["pagesize"]);
foreach ($_POST as $key => $value) {
if ("token" != $key) {
$arr["%" . $key] = "%" . $value;
}
}
$_POST["sEcho"] = $page;
$_POST["iDisplayLength"] = $pagesize;
$_POST["iDisplayStart"] = $pagesize * ($page - 1);
}
if (!is_numeric($_POST["sEcho"])) {
$result["ret"] = 1;
$result["msg"] = "page number is not number";
}
if (!is_numeric($_POST["iDisplayLength"])) {
$result["ret"] = 1;
$result["msg"] = "page size is not number";
}
if (!is_numeric($_POST["iDisplayStart"])) {
$result["ret"] = 1;
$result["msg"] = "start number is not number";
}
$sql = "select * from " . $table;
$sql = $this->_addWhere($sql, $arr);
$data = $this->mydb->pagingServer($aColumns, $sql);
echo $this->_ReturnMsg($this->_beforeMethod($data['obj']));
}