在使用easyui getSelections 与 getSelected中,经常如果不注意这两个的使用,用混的话,如果对这两个属性不了解,找起问题来,都很费事。走过的弯路,做一个记录。

DataGrid组件包括2个方法检索选择行数据:
· getSelected: 得到第一个选择行的数据,如果没有选择行则返回null否则返回该记录
· getSelections:得到全部的选择行的数据,如果元素是记录的话,返回数组数据
创建标记

<table ></table>

创建 datagrid

$('#tt').datagrid({

    title:'Load Data',

    iconCls:'icon-save',

    width:600,

    height:250,

    url:'datagrid_data.json',

    columns:[[

        {field:'itemid',title:'Item ID',width:80},

        {field:'productid',title:'Product ID',width:80},

        {field:'listprice',title:'List Price',width:80,align:'right'},

        {field:'unitcost',title:'Unit Cost',width:80,align:'right'},

        {field:'attr1',title:'Attribute',width:100},

        {field:'status',title:'Status',width:60}

    ]]

});

 

用法演示
得到选择行数据:

var row = $('#tt').datagrid('getSelected');

if (row){

    alert('Item ID:'+row.itemid+"\nPrice:"+row.listprice);

}

 

得到全部选择行的itemid:

var ids = [];

var rows = $('#tt').datagrid('getSelections');

for(var i=0; i<rows.length; i++){

    ids.push(rows[i].itemid);

}

alert(ids.join('\n'));

 注意:如果你其实应该用var rows = $('#tt').datagrid('getSelections');但是误使用的是var row = $('#tt').datagrid('getSelected');则取出来的row.length是空值。很郁闷的!

相关文章:

  • 2022-12-23
  • 2021-09-15
  • 2021-08-01
  • 2021-11-07
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案