第三十三章: ComboBox(下拉列表框)组件
学习要点:
- 加载方式
- 属性列表
- 事件列表
- 方法列表
一、加载方式
1.class加载方式:
<!DOCTYPE html>
<html>
<head>
<title>JQuery Easy UI</title>
<meta charset="utf-8"/>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
<style>
.textbox{
height:200px;
margin:0;
padding:0 2px;
box-sizing:content-box;
}
</style>
</head>
<body>
<select class="easyui-combobox" style="width:200px;">
<option value="aaaa">aaaa</option>
<option value="bbbb">bbbb</option>
<option value="cccc">cccc</option>
<option value="dddd">dddd</option>
</select>
</body>
</html>
[
{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]
<?php
//记得是:user.php
require 'config.hph';
$query = mysql_query("SELECT id,user,email,data FROM think_user") or die('SQL 错误!');
$json ='';
while (!!$row = mysql_fetch_array($query,MYSQL_ASSOC)){
$json .= json_encode($row).',';
}
$json = substr($json,0,-1);
echo '['.$json.']';
mysql_close();
?>
2.JS调用加载:
<!DOCTYPE html>
<html>
<head>
<title>JQuery Easy UI</title>
<meta charset="utf-8"/>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
<style>
.textbox{
height:200px;
margin:0;
padding:0 2px;
box-sizing:content-box;
}
</style>
</head>
<body>
<input id="box" name="box"></input>
</body>
</html>
$(function(){
$('#box').combobox({
valueField:'id',
textField:'user',
//url:'context.json',
url:'user.php',
});
});
[
{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]
<?php
//记得是:user.php
require 'config.hph';
$query = mysql_query("SELECT id,user,email,data FROM think_user") or die('SQL 错误!');
$json ='';
while (!!$row = mysql_fetch_array($query,MYSQL_ASSOC)){
$json .= json_encode($row).',';
}
$json = substr($json,0,-1);
echo '['.$json.']';
mysql_close();
?>
二、属性列表
Combobox属性 | ||
---|---|---|
属性名 | 值 | 说明 |
valueField | string | 基础数据值名称绑定到该下拉列表框。默认为value。 |
textField | string | 基础数据字段名称绑定到该下拉列表框。默认值text。 |
groupField | string | 指定分组的字段名称,默认值null。 |
groupFormatter | function(group) | 返回格式化之后的分组标题文本,以显示分组项。 |
mode | string | 定义了当文本改变时如何读取列表数据。设置为‘remote’时,下拉列表框将会从服务器中加载数据。当设置为‘remote’模式时,用户输入将被发送到名为‘q’的HTTP请求参数服务器检索新数据。 |
url | string | 通过url加载远程列表数据。 |
method | string | HTTP方法索引数据。(POST/GET) |
data | array | 数据列表加载。 |
filter | function | 定义当‘mode’设置为‘load’时如何过滤本地数据。函数有2个参数:q:用户输入的文本。row:列表行数据。返回true的时候允许行显示。 |
formatter | function | 定义如何渲染行。该函数接受一个参数:row。 |
load | function(param,success,error) | 定义了如何从远程服务器加载数据。返回false可以忽略该动作。该函数具备如下参数:param:传递到远程服务器的参数对象。success(data):在检索数据成功的时候调用该回调函数。 |
loadFilter | function(data) | 返回过滤后的数据并显示。 |
<!DOCTYPE html>
<html>
<head>
<title>JQuery Easy UI</title>
<meta charset="utf-8"/>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
<style>
.textbox{
height:200px;
margin:0;
padding:0 2px;
box-sizing:content-box;
}
</style>
</head>
<body>
<input id="box" name="box"></input>
</body>
</html>
$(function(){
$('#box').combobox({
valueField:'id',
textField:'user',
//url:'context.json',
url:'user.php',
groupField:'gender',
groupFormatter:function(group){
return '['+group+']';
},
filter:function(q,row){
var opts = $(this).combobox('options');
return row[opts.textField].indexOf(q) >= 0;
},
formatter:function(row){
var opts = $(this).combobox('options');
return '[' + row[opts.textField] + ']';
},
// mode:'remote',
/*
data:[
{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]
*/
});
});
[
{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]
<?php
//记得是:user.php
require 'config.hph';
//$q = $_POST['q'] ? $_POST['q'] :'';
//$query = mysql_query("SELECT id,user,email,data FROM think_user WHERE id='$q'") or die('SQL 错误!');
$query = mysql_query("SELECT id,user,email,data FROM think_user") or die('SQL 错误!');
$json ='';
while (!!$row = mysql_fetch_array($query,MYSQL_ASSOC)){
$json .= json_encode($row).',';
}
$json = substr($json,0,-1);
echo '['.$json.']';
mysql_close();
?>
三、事件列表
ComboBox事件 | ||
---|---|---|
事件名 | 参数 | 说明 |
onBeforeLoad | param | 在请求加载数据之前触发,返回false取消该加载动作。 |
onLoadSuccess | none | 在加载远程数据成功的时候触发。 |
onLoadError | none | 在加载远程数据失败的时候触发。 |
onSelect | recode | 在用户选择列表项的时候触发。 |
onUnSelect | recode | 在用户取消选择列表项的时候触发。 |
四、方法列表
<!DOCTYPE html>
<html>
<head>
<title>JQuery Easy UI</title>
<meta charset="utf-8"/>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
<style>
.textbox{
height:200px;
margin:0;
padding:0 2px;
box-sizing:content-box;
}
</style>
</head>
<body>
<input id="box" name="box"></input>
<input type="button" value="按钮" onclick="abc"></input>
</body>
</html>
$(function(){
$('#box').combobox({
valueField:'id',
textField:'user',
//url:'context.json',
url:'user.php',
groupField:'gender',
groupFormatter:function(group){
return '['+group+']';
},
filter:function(q,row){
var opts = $(this).combobox('options');
return row[opts.textField].indexOf(q) >= 0;
},
formatter:function(row){
var opts = $(this).combobox('options');
return '[' + row[opts.textField] + ']';
},
/*
onSelect:function(record){
alert('选择一个项时触发!');
console(record);
}
*/
onUnselect:function(record){
alert('取消时触发!');
console(record);
}
// mode:'remote',
/*
data:[
{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]
*/
});
});
function abc(){
//$('#box').combobox('unselect',1);
//$('#box').combobox('setValue',1);
//$('#box').combobox('setValues',[1,2]);
//$('#box').combobox('relooad',1);
console.log($('#box').combobox('getData');
//$('#box').combobox('loadData',
[{
"id":1,
"user":"蜡笔小新",
"emial":"xiaoxin@163.com",
"date":"2014-10-01",
},
{
"id":2,
"user":"樱桃小丸子",
"emial":"xiaowanzi@163.com",
"date":"2014-10-02",
},
{
"id":3,
"user":"黑崎一护",
"emial":"yihu@163.com",
"date":"2014-10-03",
},
]);
}
作者:Roger_CoderLife
链接:https://blog.csdn.net/Roger_CoderLife/article/details/103593681
本文根据网易云课堂JQuery EasyUI视频教程翻译成文档,转载请注明原文出处,欢迎转载