datagrid实现单行的选择、取消

该博客介绍了如何使用jQuery EasyUI库实现DataGrid组件中单行的选择和取消选择功能。通过提供的JavaScript函数,可以获取选中的行、所有选中的行、上移行以及显示所有行的数据。在DataGrid初始化时,通过onBeforeSelect事件监听选择行为,确保每次只能选择一行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="keywords" content="jquery,ui,easy,easyui,web">
	<meta name="description" content="easyui help you build your web page easily!">
	<title>jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="http://www.jeasyui.net/Public/js/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="http://www.jeasyui.net/Public/js/easyui/themes/icon.css">
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="http://www.jeasyui.net/Public/js/easyui/jquery.easyui.min.js"></script>
	<script>
		function getSelected(){
			var row = $('#tt').datagrid('getSelected');
			if (row){
				alert('Item ID:'+row.itemid+"\nPrice:"+row.listprice);
			}
		}
		function getSelections(){
			var ids = [];
			var rows = $('#tt').datagrid('getSelections');
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].itemid);
			}
			alert(ids.join('\n'));
		}
        
        function upRow(){
            var row = $('#tt').datagrid('getSelected');
            var rowindex = $('#tt').datagrid('getRowIndex',row);
            //alert(rowindex);
            if(rowindex === 0) {return; }
            $('#tt').datagrid('insertRow',{index:rowindex-1,row});
            $('#tt').datagrid('deleteRow',rowindex+1);
        }
        function allRows(){
            var rows = $('#tt').datagrid('getRows');
            alert(rows.length);
            for(var i=0; i<rows.length; i++){
                alert(rows[i].itemid);
            }
        }
        
        //实现选择单行,取消单行
        $(function(){
            $("#tt").datagrid({
                onBeforeSelect: function(index,row){
                    var row =$('#tt').datagrid('getSelected');
                    var curRowindex = $('#tt').datagrid('getRowIndex',row);
                    alert("当前selected row =" + curRowindex);
                    if(curRowindex != index) {return true;}
                    else{ $('#tt').datagrid('unselectRow',index); return false;}
                }
            });
        });
	</script>
</head>
<body>
	<h1>DataGrid</h1>
	<div style="margin-bottom:20px">
		<a href="#" onclick="getSelected()">GetSelected</a>
		<a href="#" onclick="getSelections()">GetSelections</a>
        <a href="#" onclick="upRow()">upRow</a>
        <a href="#" onclick="allRows()">all rows</a>
	</div>
	<table id="tt" class="easyui-datagrid" style="width:600px;height:250px"
			url="data/datagrid_data.json"
            data-options="singleSelect:true"
			title="Load Data" iconCls="icon-save" fitColumns="true">
		<thead>
			<tr>
				<th field="itemid" width="80">Item ID</th>
				<th field="productid" width="80">Product ID</th>
				<th field="listprice" width="80" align="right">List Price</th>
				<th field="unitcost" width="80" align="right">Unit Cost</th>
				<th field="attr1" width="150">Attribute</th>
				<th field="status" width="60" align="center">Stauts</th>
			</tr>
		</thead>
	</table>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值