首页 技术经验

EasyUI的combobox使用总结【combobox】 - combobox

combobox
一,combobox的属性,事件和方法
combobox可以配置属性,绑定事件监听,调用方法,理解这几个概念之后会对combobox有更清晰的了解。
 
1,配置属性
combobox可以配置自己的属性,他的属性可以定义在<input>或<select>标签的data-options参数中,比如:
 
<select id="type" name="type" class="easyui-combobox" 
data-options="url:'getType.do',valueField:'id',textField:'text' ">
也可以在JavaScript代码中指定:
 
$('#type').combobox({
 
         url:'getType.do',
 
         valueField:'id',
 
         textField:'text'
 
});
其中url,valueField,textField,都是combobox的属性,属性值应该用引号包裹。
 
2,绑定事件
combobox可以绑定事件监听,事件可以绑定在<input>或<select>标签的data-options参数中,比如用下面的代码绑定onSelect事件:
 
<select id="type" name="type" class="easyui-combobox" 
data-options="onSelect:changeType">
这个事件绑定表示选项更换之后会调用JavaScript中定义的changeType()函数
 
也可以在JavaScript代码中指定,比如用下面的代码绑定onSelect事件:
 
$('#type').combobox({
 
         onSelect: function (n,o) {
 
         }
 
});
3,调用方法
combobox可以调用一些方法,可调用的方法参见EasyUI的文档。
 
比如调用setValue方法:
 
$("#type").combobox('setValue','somevalue')
其中'setValue'是方法名,'somvalue'是参数。
 
有的方法没有参数,比如getValue方法:
 
var number=$("#number").combobox('getValue');
 
 
二,combobox的普通操作
如果有如下的combobox定义:
 
<input type="text" id="number" name="number" >
$("#number").combobox({
 
         onChange: function (n,o) {
 
                   changeNumber();
 
         }
 
});
1,获取选中项的value
var number=$("#number").combobox('getValue');
获取的是下拉框选中项的value值,不是显示的文字。
 
2,获取选中项的文字
var number=$("#number").combobox('getText');
3,指定选中某项
$("#number").combobox('setValue','somevalue')
 
$("#number").combobox('select','somevalue')
combobox('setValue')和combobox('select')的相同点:
 
1,都会触发onchange事件。
 
2,如果下拉框的value中存在'somevalue'这个值时,combobox下拉框会自动选中这个选项,否则会在文本框中显示该值,不选中任何选项。
 
combobox('setValue')和combobox('select')的不同:
 
combobox('setValue'),不会触发onselect事件。即使目标value存在且被下拉框选中也不会触发。
 
combobox('select'),会触发onselect事件,即使目标value不存在也会触发,此时onselect事件中的参数n为null。
 
4,清空选中项
$("#number").combobox('clear');
该方法只能清空选中项,不会影响下拉框选项列表。
 
5,清空下拉选项列表
$("#number").combobox('loadData', {});
该方法会清空下拉框的所有选项。loadData方法本身是用来加载静态下拉框选项的。
 
6,重新加载下拉选项
$("#number").combobox('reload');
还可以添加url参数,按照新的url参数来加载下拉选项:
 
$("#number").combobox('reload',url);
7,得到下拉框的下拉选项类
var opts = $("#type").combobox("options");
和combobox("getData")不同,combobox("options")得到的并不是下拉选项的列表,而是一个对象结构。
 
比如有这样的combobox:
 
<input id="type" class="easyui-combobox" name="type" 
    data-options="valueField:'id',textField:'text',url:'getType.do' ">
那么:
 
var valueField = $("#type").combobox("options").valueField;
得到的是字符串"id"。不含引号。
 
 
var url = $("#type").combobox("options").url;
得到的则是字符串'getType.do'。不含引号。
 
8,得到下拉框的全部数据
var allData = $("#type").combobox("getData");
该方法得到的是一个数组,可以用allData.length得到数组长度。也可以通过for循环来遍历。
 
数组的每个元素都代表一个下拉选项。
 
比如有这样的combobox:
 
<input id="type" class="easyui-combobox" name="type" 
    data-options="valueField:'id',textField:'text',url:'getType.do' ">
那么:
 
$("#type").combobox("getData")[0]['id'];
代表获取第一个下拉选项的value值。
 
注意:必须使用['id']的形式,笼统的使用[valueField]是错误的。
 
所以可以结合combobox("options")来达到统一的效果:
 
var field = $("#type").combobox("options").valueField;
 
var allData = $("#type").combobox("getData");
 
var value=allData[0][field];
以上代码中的value就是第一个下拉选项的value值。
 
9,onChange事件
onchange是combobox中的值发生变化时触发的事件。
 
这个发生变化包括在文本框中输入文字,还有点击下拉框中的其他选项。
 
当在文本框输入文字时,如果两次键入文字的时间间隔较小,该事件不会触发,稍一停顿则会触发,这个时间目测大概零点几秒。
 
onchange可以这样定义:
 
$("#type").combobox({
 
         onChange: function (n,o) {
 
         }
 
});
n代表新选择的下拉选项的value,或者在文本框中刚刚输入的文字。这取决于之前的值是选择下拉框选项而来还是在文本框中键入了文字。
 
o代表修改前的下拉选项的value,或者修改前在文本框中输入的文字。这取决于之前的值是选择下拉框选项而来还是在文本框中键入了文字。
 
 
 
10,onSelect事件
onselect是在combobox下拉框中更换下拉选项时触发的事件。
 
和onchange事件不同,在文本框中手动输入内容时不会触发onselect。
 
onselect可以这样定义:
 
$("#type").combobox({
 
         onSelect: function (n,o) {
 
 
         }
 
});
n代表新选择的下拉选项。
 
o代表选择前的下拉选项。
 
注意,和onChange不同,onSelect的n和o代表的是选择之前和选择之后的下拉选项,可以通过:
 
n.value
 
n.text
来获得选中项的value和文本内容
 
 
 
三,combobox的创建方式
EasyUI的combobox有以下几种创建方式:
 
1,<select>元素+预定义<option>
只需要在<select>元素中添加class="easyui-combobox"即可。
 
比如:
 
<select id="type" class="easyui-combobox" name="type" style="width: 100px">
 
         <option value=""></option>
 
         <option value="1">类型1</option>
 
         <option value="2">类型2</option>
 
         <option value="3">类型3</option>
 
</select>
 
 
2,<select>元素+动态加载下拉选项
在<select>标签中添加class="easyui-combobox"参数,另外在<select>标签中添加data-options参数,并在参数值中设定url属性。
 
比如:
 
<select id="type" class="easyui-combobox" name="type"
 
    data-options="url:'getType.do',valueField:'id',textField:'text' ">
 
         <option value="">请选择</option>
 
</select>
注意,动态下拉选项加载完成后,之前预加载的所有下拉选项会消失。
 
 
 
3,<input>元素+预定义JSON
在<input>标签中添加class="easyui-combobox"参数,另外在<input>标签中添加data-options参数,并在参数值中设置data属性,data的值是JSON格式。
 
比如:
 
<input class="easyui-combobox" id="type" name="type"
 
         data-options="
                   valueField: 'id',
                   textField: 'text',
                   data: [{
                            id: '1',
                            text: '类型1'
                   },{
                            id: '2',
                            text: 'Perl'
                   },{
                            id: '3',
                            text: 'Ruby'
                   }]"
 
/>
 
 
4,<input>元素+动态加载下拉选项
在<input>标签中添加class="easyui-combobox"参数,另外在<input>标签中添加data-options参数,并在参数值中设定url属性。
 
比如:
 
<input id="type" class="easyui-combobox" name="type" 
    data-options="valueField:'id',textField:'text',url:'getType.do '">
valueField:'id',表示url返回值中的id参数将被设置为下拉选项的value。
 
textField:'text',表示url返回值中的text参数将被设置为下拉选项的文本内容。
 
url,url是请求地址,可以返回JSON列表格式,也可以返回其他key-value形式的列表。
 
如果是java+spring的组合,可以返回List<Map<String, Object>>格式,List中的每个Map代表一个下拉选项,Map.get("id")的值就是下拉选项的value的值,Map.get("text")的值就是下拉选项的文本。
 
建议使用org.springframework.web.bind.annotation.ResponseBody注解。
 
 
 
5,由jquery代码创建。
首先需要一个<input>元素或<select>元素:
 
<input id="type" name="type" value="type">
然后用如下代码创建combobox:
 
$('#type').combobox({
 
    url:'getType.do',
 
    valueField:'id',
 
    textField:'text'
 
});
实际上只要执行:
 
$('#type').combobox({});
就可以把<input>元素或<select>元素变为combobox下拉框。这个过程貌似是不可逆的,除非把元素删除然后重新添加一个。
 
除了以上代码中定义的参数之外,还可以绑定EasyUI的其他参数或事件。
 
 
 
四,其他几个属性,事件,方法简介
1,groupField属性
指定要被分组的字段。
 
2,groupFormatter属性
返回要显示在分组项目上的分组文本。
 
用函数的方式定义,类似绑定事件时的写法。
 
3,onBeforeLoad事件
在发起加载请求前触发的事件,如果方法返回了false,则不会加载。
 
4,onLoadSuccess事件
远程数据加载成功后触发的事件。
 
5,onLoadError事件
远程数据加载失败后触发的事件。
 
6,setValues方法
给combobox批量赋值并选中。
 
$('#type').combobox('setValues', ['1','2']);
7,unselect方法
取消选择指定value。
阅读数
151449
话题:combobox

关于easyui combobox

关于easyui combobox

Combobox用法和方法参数: 1、 需要引入class=" easyui-combobox” 2、 参数设置需要在data-options中设置 3、 属性参数配置: valueField:基础数据值名称绑定到Combobox(提交值) textField:基础数据的字段名称绑定的Combobox(显示值) mode:定义当文本改变时如何加载列表数据,当设置为remote模式下,什么类型的用户将被发送http请求参数名为'q'的服务器,以获取新的数据。 url:从远程URL来加载列表数据 method:http方法检索列表数据 data:列表中被加载的数据 filter:定义如何过滤本地数据“模式”设置为'local' formatter:定义如何呈现行 loader:定义如何从远程服务器加载数据 4、 事件: onBeforeLoad(param): 操作之前将数据加载,返回false就取消这个荷载作用 onLoadSuccess():触发时,远程数据加载成功 onLoadError:触发时,远程数据加载错误 ONS www.hbbz08.com ELECT:触发,当用户选择一个列表项 onUnselect:触发,当用户取消选择一个列表 5、方法: options():返回选择对象 getData():返回加载的数据 loadData(data):加载列表数据 reload(url):重新加载远程数据列表 setValues(values):设置combobox的值数组 setValue(value):设置combobox的值 clear():清空combobox的值 select(value):选中指定的值 unselect(value):取消指定的值


easyui的combobox用法

easyui的combobox用法

  Combo Box (组合框)控件很简单,可以节省空间。从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的。用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输入文本。
  1、定义控件对应变量
  假定已经创建了一个Dialog,并且从控件工具箱将 Combo Box 控件拖放到上面。打开 Class Wizard,添加控件对应变量,
  如:CComboBox m_cbExamble;
  在后面的代码中会不断使用这个变量。
  
  2、向控件添加 Items
  1) 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行。换行用ctrl+回车。
  2)利用函数 AddString()向Combo Box 控件添加 Items,如:
  m_cbExample.AddString(“StringData1”);
  m_cbExample.AddString(“StringData2”);
  m_cbExample.AddString(“StringData3”);
  3)也可以调用函数 InsertString()将 Item插入指定位置 nIndex,如:
  m_cbExample.InsertString( nIndex, “StringData” );
  3、从控件得到选定的Item
  假设在控件列表中已经选定某项,现在要得到被选定项的内容,首先要得到该项的位置,然后得到对应位置的内容。这里会用到两个函数,如:
  int nIndex = m_cbExample.GetCurSel();
  CString strCBText;
  m_cbExample.GetLBText( nIndex, strCBText);
  这样,得到的内容就保存在 strCBText中。
  若要选取当前内容,可调用函数GetWindowText(strCBText)。
  
  4、在控件中查找给定Item
  这种操作一般用于在程序中动态修改控件中该项的值,可以用函数FindStringExact()精确匹配,如:
  int nIndex = m_cbExample.FindStringExact( nStartAfter, “value to be found”);
  nStartAfter指明从哪一行开始查找。如果查找成功,返回的是该项的位置;否则,返回CB_ERR。
  也可以选中包含指定字符串的项,如:
  int nIndex = m_cbExample.SelectString( nStartAfter, “value to be selected”);
  5、删除控件中的Item
  该操作可以利用函数DeleteString(),需要指定被删除项的位置,如:
  m_cbExample.DeleteString(nIndex);
  也可以使用函数ResetContent(),清除目前的所有项,如:
  m_cbExample.ResetContent();
  6、显示控件中的某项
  int nIndex = m_cbExample.GetCurSel(); //当前选中的项
  m_cbExample.SetCurSel(nIndex); //设置第nIndex项为显示的内容
  
  7、得到或设置输入框中被选中的字符位置
  DWORD GetEditSel( ) /BOOL SetEditSel( int nStartChar, int nEndChar );
  BOOL LimitText( int nMaxChars );设置输入框中可输入的最大字符数。
  
  8、列表框常用消息映射宏
  ON_CBN_DBLCLK 鼠标双击
  ON_CBN_DROPDOWN 列表框被弹出
  ON_CBN_KILLFOCUS / ON_CBN_SETFOCUS 在输入框失去/得到输入焦点时产生
  ON_CBN_SELCHANGE 列表框中选择的行发生改变
  ON_CBN_EDITUPDATE 输入框中内容被更新


easyui combobox中的值是从数据库拿过来的,编辑easyui gridveiw时combobox定位gridveiw对应值

easyui combobox中的值是从数据库拿过来的,编辑easyui gridveiw时combobox定位gridveiw对应值

先定义一个性别的选择框,如下:
{name:"gender",id:"gendere",fieldLabel:"用户性别",
mode: 'local',
triggerAction: 'all',
typeAhead: true,
lazyRender:true,
xtype:"combo",
emptyText:"--请选择--",
store: new Ext.data.ArrayStore({
id:2,
fields:['key','values'],
data:[[0,'男'],[1,'女']]
}),
displayField:'values',
valueField:'key',
selectOnFocus:true
}
再定义点击gridview时的触发事件,如下,将点击那行的性别赋值给性别选择框:
userGrid.on('rowclick',function(grid,rowIndex,event){
var record = userGrid.getStore().getAt(rowIndex);
Ext.getCmp('gendere').setValue(record.get("gender"));
});
这样应该就可以显示出来了。


easyui combobox 绑定Json里指定的一串数据

easyui combobox 绑定Json里指定的一串数据

修改数据源 id text 修改成id name


$('#selGlade').combobox({
url: Handler(GladeHandler, [["t", "CobmoList"]]),
valueField: 'id',
textField: 'text',
panelHeight:'auto',//面板自动高度
panelWidth: 142,//面板宽度
width:142//select宽度
});


json:



[{
"id":1,
"text":"text1"
},{
"id":2,
"text":"text2"
},{
"id":3,
"text":"text3",
"selected":true
},{
"id":4,
"text":"text4"
},{
"id":5,
"text":"text5"
}]


我想要让easyui的combox动态加载数据库的数据,怎么做啊?请教大神!!

我想要让easyui的combox动态加载数据库的数据,怎么做啊?请教大神!!

$('#com').combobox({
url: url,//ajax后台取数据路径,返回的是json格式的数据
valueField: 'id',
textField: 'text',//json格式如下示 这里的valueField和textField值要与json中一致
onSelect: function (r) {
alert(r.id);
},
onLoadSuccess: function () {
$(this).combobox('setText', '--请选择--');
}
});


json:格式

[{
"id":1,
"text":"text1"
},{
"id":2,
"text":"text2"
}]

详见easyui api


jquery easyui 中combobox的onselect事件怎么做下拉框的级联

jquery easyui 中combobox的onselect事件怎么做下拉框的级联

1:准备2个input设置不同的id


2:注册easyui的combobox控件

$('#box1').combobox({
data:[{id:0,text:'广州'},{id:1,text:'上海'}]
valueField:'id',
textField:'text'
});
$('#box2').combobox({
valueField:'id',
textField:'text'
});
3:由box1联动显示box2的数据,设置box1的onSelect事件
$('#box1').combobox({
onSelect:function(record){
var region=[];
if(record.id==0){//如果城市是广州
region.push={id:0101,text:"黄埔区"};
region.push={id:0102,text:"天河区"};
}else if(record.id==1){//如果城市是上海
region.push={id:0101,text:"浦东区"};
region.push={id:0102,text:"松江区"};
}
//给box2赋值
$('#box2').combobox({
data: region
});
}
});


easyui怎么把select定义成combobox

easyui怎么把select定义成combobox

$("#cbx_quale").combobox({
url: '@(Url.Action("BaseDataCombobox", "System"))?type=@(CMM.RedStrings.Model.Const.BaseDataCategory.EnterpriseProperty)',
valueField: "ID",
textField: "Text",
panelHeight: "auto",
editable: false,
onSelect: function (record) {
$("#hide_quale").val(record.ID);
},
onLoadSuccess: function () { //加载完成后,设置选中第一项
var val = $(this).combobox("getData");
for (var item in val[0]) {
if (item == "ID") {
$(this).combobox("select", val[0][item]);
}
}
}
});

只是有一定combobox不好的是后台返回的json数据不能直接绑定,网上的很多解决方法都是先通过ajax请求获取json数据,再通过回调函数将数据绑定到combobox中。



$.ajax({
url:"${ctx}/carpark-type/combobox.json",
type:"post",
datatype:"json",
success:function(data){
$('#cc').combobox({
data:data.rows,
valueField:'id',
textField:'text',
onLoadSuccess: function () { //加载完成后,设置选中第一项
var val = $(this).combobox("getData");
for (var item in val[0]) {
if (item == "id") {
$(this).combobox("select", val[0][item]);
}
}
}
});
}
});


easyUI中datagrid里面放置combobox,如何写onSelect事件,

easyUI中datagrid里面放置combobox,如何写onSelect事件,

第一个combobox的options中加 onSelect: function () { //获取选中的值 var varSelect = $(this).combobox('getText'); if (varSelect == '中国') { //获取第二个combobox所在的datagrid Editor var varEditor = $('#DataGridId').datagrid('getEditor', { index: index, field: '' }); $(varEditor.target).combobox('disable'); } else { //获取第二个combobox所在的datagrid Editor var varEditor = $('#DataGridId').datagrid('getEditor', { index: index, field: '' }); $(varEditor.target).combobox('enable'); } }

怎样获取form表单中的easyui combobox复选取值相对应的id

EasyUi的Combobox如何选中某项值?

1.添加combobox后做如下设置

2.根据自己的需要在类中重写如下方法

// ==========================================================
// Combo box data source methods
// ==========================================================
- (NSInteger) numberOfItemsInComboBox: (NSComboBox *) aComboBox {
#pragma unused (aComboBox)
return ([dataSource count]);
}
- (id) comboBox: (NSComboBox *) aComboBox objectValueForItemAtIndex: (NSInteger) index {
#pragma unused (aComboBox)
return [dataSource objectAtIndex:index];
}
- (NSUInteger) comboBox: (NSComboBox *) aComboBox indexOfItemWithStringValue: (NSString *) string {
#pragma unused (aComboBox)
return [dataSource indexOfObject:string];
}

- (NSString *) firstGenreMatchingPrefix: (NSString *) prefix {
NSString *string = nil;
NSString *lowercasePrefix = [prefix lowercaseString];
NSEnumerator *stringEnum = [dataSource objectEnumerator];

while ( string = [stringEnum nextObject] )
if ([[string lowercaseString] hasPrefix:lowercasePrefix] ) {
return (string) ;
}

return (nil);
} // firstGenreMatchingPrefix

- (NSString *) comboBox: (NSComboBox *) aComboBox completedString: (NSString *) inputString {
#pragma unused (aComboBox)
// This method is received after each character typed by the user, because we have checked the "completes" flag for
// genreComboBox in IB.
// Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete
// string.
NSString *candidate = [self firstGenreMatchingPrefix:inputString];
return (candidate ? candidate : inputString);
}
这些方法实现combobox在选择选项、自动完成时的处理方式。其中dataSource为NSArray。
3.设置combobox的datasource为实现上述方法的对象即可。


easyui的datagrid列的editor值为combobox时怎么动态加载数据

直接在option里面写即可 { title: '企业', field: 'Ent', align: 'center', width: 100, editor: { type: 'combobox', options: { data: GetEnt(), valueField: "value", textField: "text", editable: false, required: true } } }其中GetEnt方法中可以写ajax 返回一个对象数组 如[{ value: 0, text:'企业1' },{ value: 2, text:'企业1' }]

怎样获取form表单中的easyui combobox复选取值相对应的id

var width = $(window).width()-80; var height = $(window).height()-120; stView_layout = $('#stView_layout').layout({ width: width, height: height }); station_view = $('#stationView').window({ title: '测站导航', left:50, top:80, width: width, modal: false, shadow: false, closed: true, height: height, onResize:function(w,h){ if(stView_treegrid){ stView_treegrid.treegrid({ width:w-20, height:h-260 }); } } });