原文地址:http://my.oschina.net/meSpace/blog/41463
easyui api
数据表格组件包含两个方法来获取选中行的数据:
- getSelected: 获取第一个选中行的数据,如果没有选中的行则返回空,否则返回该行的记录
- getSelections: 获取所有选中行的数据,返回数组,其元素为行的记录
创建标签
创建数据表格
$('#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);}
获取所有选中行的项id:
var ids = [];var rows = $('#tt').datagrid('getSelections');for(var i=0; i
下载数据表格实例代码:
$('#tt').datagrid({
iconCls:'icon-save', width:1150, height:650, nowrap: false, striped: true, idField: 'g_id', collapsible:true, , remoteSort: false, idField:'g_id', pageSize:5, frozenColumns:[[ {field:'ck',checkbox:true} ]], columns:[[ {title:'游戏ID',field:'g_id',width:80}, {title:'游戏名称',field:'g_name',width:80}, {title:'版本',field:'g_version',width:80}, {title:'玩家数量',field:'g_players',width:80}, {title:'游戏简介',field:'g_desc',width:80}, {title:'星级',field:'g_star',width:80}, {title:'游戏图标路径',field:'g_imagepath',width:80}, {title:'游戏图标名称',field:'g_imagename',width:80}, {title:'可用状态',field:'g_status',width:80}, {title:'游戏上线时间',field:'g_onlinetime',width:80}, {title:'是否支持客户端',field:'g_isclient',width:80}, {title:'游戏储存路径',field:'g_gamepath',width:80}, {title:'游戏文件名',field:'g_gamename',width:80}, {title:'游戏密钥',field:'g_key',width:80}, {title:'游戏厂商ID',field:'gc_id',width:80}, {title:'游戏类型ID',field:'g_type_id',width:80}, {title:'文件大小',field:'g_filesize',width:80}, {title:'游戏文件路径',field:'g_packagepath',width:80} ]], pagination:true, rownumbers:true }); //分页 var p = $('#test').datagrid('getPager'); $(p).pagination({ onBeforeRefresh:function(){ alert('before refresh'); } });
//获取行数getSelections
//获取行数内容 getSelected
var ids = [];
var rows = $('#tt').datagrid('getSelections'); for(var i=0; i<rows.length; i++){ ids.push(rows[i].g_id); } alert(rows.length); alert(ids);
//只能在页面中删除行 不能对数据库操作...
function del(){
var rows = $('#tt').datagrid("getSelections"); //获取你选择的所有行 //循环所选的行 for(var i =0;i<rows.length;i++){ var index = $('#tt').datagrid('getRowIndex',rows[i]);//获取某行的行号 $('#tt').datagrid('deleteRow',index); //通过行号移除该行 } }
//获取页面中选择行的id和数量
function getSelectedArr() {
var ids = []; var rows = $('#tt').datagrid('getSelections'); for(var i=0; i<rows.length; i++){ ids.push(rows[i].g_id); } return ids; }
var arr = getSelectedArr();
var num = arr.length;
//日期格式的文本输入框
<input id="dd" class="easyui-datebox" ></input>