在jqGrid中设置multiselect: true可以实现全选的 *** 作,但怎么设置被选中的checkbox里面的值呢,做法如下:
jQuery("#listTable")jqGrid({
url: 'queryListdo',
datatype: 'json',
colNames: ['','编号','姓名'],
colModel: [
{
name: 'MY_ID',
index: 'MY_ID',
sortable: false,
width: '0%',
hidden:true
},
{
name: 'MY_NO',
index:'MY_NO',
sortable: false,
align:'center',
width:'10%'
},
{
name: 'NAME',
index:'NAME',
sortable: false,
align:'center',
width:'10%'
}
],
page: 1,
rowNum: 10,
rowList: [10, 20, 30],
pager: '#listPage',
multiselect: true,
sortname: 'MY_NO',
viewrecords: true,
sortorder: "desc",
jsonReader: {
repeatitems: false
},
width: "100%",
height: '100%',
gridComplete: function() {
var rowIds = jQuery("#listTable")jqGrid('getDataIDs');
for(var k=0; k<rowIdslength; k++) {
var curRowData = jQuery("#listTable")jqGrid('getRowData', rowIds[k]);
var curChk = $("#"+rowIds[k]+"")find(":checkbox");
curChkattr('name', 'checkboxname'); //给每一个checkbox赋名字
curChkattr('value', curRowData['MY_ID']); //给checkbox赋值
curChkattr('title', curRowData['NAME'] ); //给checkbox赋予额外的属性值
//curChkattr('checked', 'true'); //设置所有checkbox被选中
}
//下面的代码顺序不能变(这是页面上所有行被真选中[所有行被**])
//$("#cb_listTable")attr("checked", true);
//$("#cb_listTable")click(); //input框
//$("#jqgh_listTable_cb")click(); //div标签
//$("#listTable_cb")click(); //th标签
}
});
加载完列表后获取被选中的checkbox的值及其属性值的做法如下:
var checkedVals = new Array();
var checkedTitles = new Array();
$(":checkbox[name=checkboxname][checked]")each(function(){
checkedValspush($(this)val());
checkedTitlespush($(this)attr("title"));
});
for(var p=0; p<checkedValslength; p++){
alert(checkedVals[p]);
alert(checkedTitles[p]);
}
--------------------------------------------------------------------------------------------------
让查询列表中按指定条件被默认选中或灰掉checkbox的做法如下:
jQuery("#listTable")jqGrid({
url: 'queryListdo',
datatype: 'json',
colNames: ['','编号','姓名'],
colModel: [
{
name: 'MY_ID',
index: 'MY_ID',
sortable: false,
width: '0%',
hidden:true
},
{
name: 'MY_NO',
index:'MY_NO',
sortable: false,
align:'center',
width:'10%'
},
{
name: 'NAME',
index:'NAME',
sortable: false,
align:'center',
width:'10%'
}
],
page: 1,
rowNum: 10,
rowList: [10, 20, 30],
pager: '#listPage',
multiselect: true,
multiboxonly: true,
sortname: 'MY_NO',
viewrecords: true,
sortorder: "desc",
jsonReader: {
repeatitems: false
},
width: "100%",
height: '100%',
gridComplete: function() {
var rowIds = jQuery("#listTable")jqGrid('getDataIDs');
for(var k=0; k<rowIdslength; k++) {
var curRowData = jQuery("#listTable")jqGrid('getRowData', rowIds[k]);
if(curRowDataMY_NO == '123456'){
$("#listTable")find("input[id='jqg_" + rowIds[k] +
"']")val(curRowDataMY_ID);
if(curRowDataNAME=='shihuan'){
$("#listTable")find("input[id='jqg_" + rowIds[k] +
"']")attr("checked", true);
}
}else{
$("#listTable")find("input[id='jqg_" + rowIds[k] +
"']")val(curRowDataMY_ID);
$("#listTable")find("input[id='jqg_" + rowIds[k] + "']")attr("checked",
true);
$("#listTable")find("input[id='jqg_" + rowIds[k] + "']")attr("disabled",
true);
}
}
}
onSelectAll:function(rowid, status) { //rowid 数组
var ids = jQuery("#listTable")jqGrid('getDataIDs');
for (var i=0; i<idslength; i++) {
var cl = ids[i];
var curRowData = jQuery("#listTable")jqGrid('getRowData',cl);
var ckt = $("#listTable")find("input[id='jqg_" + cl + "']")attr("disabled");
if(ckt){
$("#listTable")find("input[id='jqg_" + cl + "']")attr("checked", true);
}
}
},
beforeSelectRow:function(rowid, e){
//alert(rowid); //rowid的值是checkbox的value值
return false;
}
});
--------------------------------------------------------------------------------------------------
jqGrid被重新载入的做法如下:
function gridReload() {
jQuery("#listTable")jqGrid('setGridParam', {
//url:'///com/shihuan/searchdo',
//postData: transferFormElementsToObject('theForm'),
postData: {"message":msval},
page:1
})trigger("reloadGrid");
}
protected void Page_Load(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
chkText = "testall"; // 这里可以换成数据库的内容
chkCheckedChanged += new EventHandler(chk_CheckedChanged);
chkAutoPostBack = true;
PageFormControlsAdd(chk);
for (int i = 0; i < 10; i++)
{
CheckBox chk2 = new CheckBox();
chk2Text = "test" + iToString(); // 这里可以换成数据库的内容
chk2Checked = (i % 3 == 0); // 这里可以换成数据库的内容
PageFormControlsAdd(chk2);
}
}
void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach(Control ctl in PageFormControls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
chkChecked = allChecked;
}
}
}
<script>
$(document)ready(function()
{
initPlsfList();
});
//初始化grid列表
function initPlsfList(){
//缓存表列数组,key 为页面元素的name ,value 对应数据库中的字段
var cellArray = new Array();
cellArray["zoneID"] ="ZONE_ID";
cellArray["factorPG"] ="FACTOR_PG";
cellArray["factorQG"] ="FACTOR_QG";
cellArray["factorPL"] ="FACTOR_PL";
cellArray["factorQL"] ="FACTOR_QL";
cellArray["valid"] ="VALID";
var caseID = '${caseID}';
$("#plsfList")jqGrid({
url:"<c:url value='/lfc/powerlsfactor/queryDatacaseID="+caseID+"' />",
datatype: "json",
mtype:"POST",
height: "auto",
width: "auto",
colNames:[
'id',
"<fmt:message key='casevalid'/>",
"<fmt:message key='zone'/>",
"<fmt:message key='pasf'/>",
"<fmt:message key='pisf'/>",
"<fmt:message key='lasf'/>",
"<fmt:message key='lisf'/>",
'modeID'
],
colModel:[
{name:'id',index:'id', width:100,hidden:true},
{name:'valid',index:'valid', width:100,editable:true,
formatter:formatValid,
editable:true,edittype:'checkbox',
editoptions:{value:'1:0',defaultValue:'1'}
},
{name:'zoneName',index:'zoneName', width:150,editable:true},
{name:'factorPG',index:'factorPG', width:100,editable:true},
{name:'factorQG',index:'factorQG', width:100,editable:true},
{name:'factorPL',index:'factorPL', width:100,editable:true},
{name:'factorQL',index:'factorQL', width:100,editable:true},
{name:'caseID',index:'caseID', width:100,hidden:true},
],
rowNum:10,
rowList:[10,20,30],
pager: '',
cellEdit:true,
viewrecords: true,
jsonReader: { repeatitems : false, id: "id" },
viewsortcols:[false,'horizontal',false],
sortable:false,
sortorder:"asc",
sortname:"id",
multiselect: true,
cellurl:"<c:url value='/lfc/powerlsfactor/save'/>",
cellsubmit: 'remote',
gridComplete: function() {
var $selecAll = $("#cb_plsfList");
var cb_title = "<fmt:message key='selectall' bundle='${commonResources}'/>" ;
if($selecAll){
$selecAllattr("title",cb_title);
}
//设置全选checkbox title
var rowIds = jQuery("#plsfList")jqGrid('getDataIDs');
for(var k=0; k<rowIdslength; k++) {
var curRowData = jQuery("#plsfList")jqGrid('getRowData', rowIds[k]);
var curChk = $("#"+rowIds[k]+"")find(":checkbox");
//curChkattr('title', curRowDatamodeName); //给checkbox赋予额外的属性值
}
},
onSortCol:function(index,iCol,sortorder){
return false ;
},
ondblClickRow: function (rowid,iRow,iCol,e) {
/var $plsfList = $("#plsfList");
if (isRowNeedSave($plsfList)){
showMessage("请先保存");
}else{
$("#operate")val("update");
newrowid = rowid ;
$plsfListsetGridParam({cellEdit:false});
$plsfListjqGrid('editRow', rowid, true);
//确定按钮可用
$("#confirm_btn")attr("disabled",false);
}/
},
beforeSubmitCell:function(rowid, cellname, value, iRow, iCol){
//列提交前的拦截方法
var $plsfList = $("#plsfList") ;
var $editUrl = '<c:url value='/lfc/powerlsfactor/save'/>' ;
//设置列提交的url。updateCellName:要编辑的列名 ;updateCellValue :是编辑的值
$editUrl = addParamToUrl($editUrl,'updateCellName',iCol == 3 cellArray['zoneID'] :cellArray[cellname]);
$editUrl = addParamToUrl($editUrl,'updateCellValue',iCol == 3 $("#zone_id")val():value);
//给jqgrid 从新设置cellurl 值
$plsfListsetGridParam({cellurl:$editUrl});
return false ;
},
afterEditCell:function(rowid, cellname, value, iRow, iCol){
//动态修改lie时,当列 变为可修改状态时,给列add一个button,且列中元素不可编辑,点击button d出一个模态窗口,可以选择元素 ,赋值给grid当前编辑列中单行表单域中
$("#"+rowid+" input[type='checkbox']")attr("checked",value == "<fmt:message key='casevalid'/>" true:false);
if(iCol==3){
$("#irowNum")val(rowid);
var $data = $("#"+rowid +">td"); //获取这个行里所有的td元素,即:获取所有子元素
$zoneInput = $datafind("input")eq("1") ;
$zoneInputcss("width","100px");
$zoneInputattr("disabled",true);
$zoneInputafter("<input type='button' value='选择' onclick='fnCallDialogForEidt()' />");
}
}
});
//grid添加新的一行
var newrowid ;
function addRow()
{
$("#operate")val("");
var selectedId = $("#plsfList")jqGrid("getGridParam", "selrow");
var ids = jQuery("#plsfList")jqGrid('getDataIDs');
//获得当前最大行号(数据编号)
var rowid = Mathmaxapply(Math,ids);
//获得新添加行的行号(数据编号)
newrowid = rowid+1;
var dataRow = {
id: "",
valid:"",
zoneID:'',
factorPG:'',
factorQG:'',
factorPL:'',
factorQL:'',
caseID:''
};
//将新添加的行插入到第一列
$("#plsfList")jqGrid("addRowData", newrowid, dataRow, "first");
//设置grid单元格不可编辑
$("#plsfList")setGridParam({cellEdit:false});
//设置grid单元格可编辑
$('#plsfList')jqGrid('editRow', newrowid, false);
//确定按钮可用
$("#confirm_btn")attr("disabled",false);
//给添加的列加选择按钮
var $zoneInput = $("#"+newrowid+"_zoneName");
$zoneInputattr("disabled",true)css("width",100);
$zoneInputafter("<input type='button' value='选择' onclick='fnCallDialogForEidt()' />");
}
function insertPlsf(){
var $plsfList = $("#plsfList") ;
var $operate = $("#operate")val();
//设置grid单元格可编辑
$plsfListsetGridParam({cellEdit:true});
//设置grid行不可编辑
//$plsfListjqGrid('editRow', newrowid, false);
//拼接请求的url
var url = '<%=basePath%>'+"/lfc/powerlsfactor/save" ;
var $params = $plsfListfind("input[id^="+newrowid+"]");
var $check_val = $paramseq(0)is(':checked') 1:0;
url = addParamToUrl(url,'valid',$check_val);
url = addParamToUrl(url,'zoneID',$("#zone_id")val());
url = addParamToUrl(url,'factorPG',$paramseq(2)val());
url = addParamToUrl(url,'factorQG',$paramseq(3)val());
url = addParamToUrl(url,'factorPL',$paramseq(4)val());
url = addParamToUrl(url,'factorQL',$paramseq(5)val());
var $caseID = $("#caseID")val();
url = addParamToUrl(url,'caseID',$caseID);
$ajax({url:url,type:"post",timeout:5000,
success:function(data){
showMessage(data);
reloadGrid();
}
});
//将新添加行号 初始为空
newrowid = '' ;
//确定按钮不可用
$("#confirm_btn")attr("disabled",true);
}
//格式zone列输出内容
function formatZone(cellvalue, options, rowObject){
if(cellvalue == 0){
return 0;
}else if(cellvalue == 1){
return 1;
}else if(cellvalue == 2){
return 2;
}else{
return 3;
}
}
function isRowNeedSave($jqgrid){
var $editTr = $jqgridfind("tr[editable=1]") ;
var flag = false ;
if ( $editTr && $editTrlength > 0){
flag = true ;
}
return flag ;
}
function cancel(){
reloadGrid();
//确定按钮不可用
$("#confirm_btn")attr("disabled",true);
//设置grid单元格可编辑
$("#plsfList")setGridParam({cellEdit:true});
//设置grid单元格可编辑
$('#plsfList')jqGrid('editRow', newrowid, true);
}
function fnCallDialogForEidt(){
//获得当前行号(数据编号)
var returnValue = "";
returnValue = windowshowModalDialog("<c:url value='/element/zone/querycaseID="+$("#caseID")val()+"' />",window,"");
if(returnValue==""||returnValue==null)
return;
var $plsfList = $("#plsfList");
var $params = $plsfListfind("input[id$='zoneName']");
var names = returnValuesplit(",");
$paramseq(0)val(names[1]);
$("#zone_id")val(names[0]);
}
<script>
$(document)ready(function()
{
initPlsfList();
});
//初始化grid列表
function initPlsfList(){
//缓存表列数组,key 为页面元素的name ,value 对应数据库中的字段
var cellArray = new Array();
cellArray["zoneID"] ="ZONE_ID";
cellArray["factorPG"] ="FACTOR_PG";
cellArray["factorQG"] ="FACTOR_QG";
cellArray["factorPL"] ="FACTOR_PL";
cellArray["factorQL"] ="FACTOR_QL";
cellArray["valid"] ="VALID";
var caseID = '${caseID}';
$("#plsfList")jqGrid({
url:"<c:url value='/lfc/powerlsfactor/queryDatacaseID="+caseID+"' />",
datatype: "json",
mtype:"POST",
height: "auto",
width: "auto",
colNames:[
'id',
"<fmt:message key='casevalid'/>",
"<fmt:message key='zone'/>",
"<fmt:message key='pasf'/>",
"<fmt:message key='pisf'/>",
"<fmt:message key='lasf'/>",
"<fmt:message key='lisf'/>",
'modeID'
],
colModel:[
{name:'id',index:'id', width:100,hidden:true},
{name:'valid',index:'valid', width:100,editable:true,
formatter:formatValid,
editable:true,edittype:'checkbox',
editoptions:{value:'1:0',defaultValue:'1'}
},
{name:'zoneName',index:'zoneName', width:150,editable:true},
{name:'factorPG',index:'factorPG', width:100,editable:true},
{name:'factorQG',index:'factorQG', width:100,editable:true},
{name:'factorPL',index:'factorPL', width:100,editable:true},
{name:'factorQL',index:'factorQL', width:100,editable:true},
{name:'caseID',index:'caseID', width:100,hidden:true},
],
rowNum:10,
rowList:[10,20,30],
pager: '',
cellEdit:true,
viewrecords: true,
jsonReader: { repeatitems : false, id: "id" },
viewsortcols:[false,'horizontal',false],
sortable:false,
sortorder:"asc",
sortname:"id",
multiselect: true,
cellurl:"<c:url value='/lfc/powerlsfactor/save'/>",
cellsubmit: 'remote',
gridComplete: function() {
var $selecAll = $("#cb_plsfList");
var cb_title = "<fmt:message key='selectall' bundle='${commonResources}'/>" ; if($selecAll){
$selecAllattr("title",cb_title);
}
//设置全选checkbox title
var rowIds = jQuery("#plsfList")jqGrid('getDataIDs');
for(var k=0; k<rowIdslength; k++) {
var curRowData = jQuery("#plsfList")jqGrid('getRowData', rowIds[k]);
var curChk = $("#"+rowIds[k]+"")find(":checkbox");
//curChkattr('title', curRowDatamodeName); //给checkbox赋予额外的属性值 }
},
onSortCol:function(index,iCol,sortorder){
return false ;
},
ondblClickRow: function (rowid,iRow,iCol,e) {
/var $plsfList = $("#plsfList");
if (isRowNeedSave($plsfList)){
showMessage("请先保存");
}else{
$("#operate")val("update");
newrowid = rowid ;
$plsfListsetGridParam({cellEdit:false});
$plsfListjqGrid('editRow', rowid, true);
//确定按钮可用
$("#confirm_btn")attr("disabled",false);
}/
},
beforeSubmitCell:function(rowid, cellname, value, iRow, iCol){
//列提交前的拦截方法
var $plsfList = $("#plsfList") ;
var $editUrl = '<c:url value='/lfc/powerlsfactor/save'/>' ;
//设置列提交的url。updateCellName:要编辑的列名 ;updateCellValue :是编辑的值
$editUrl = addParamToUrl($editUrl,'updateCellName',iCol == 3 cellArray['zoneID'] :cellArray[cellname]);
$editUrl = addParamToUrl($editUrl,'updateCellValue',iCol == 3 $("#zone_id")val():value);
//给jqgrid 从新设置cellurl 值
$plsfListsetGridParam({cellurl:$editUrl});
return false ;
},
afterEditCell:function(rowid, cellname, value, iRow, iCol){
//动态修改lie时,当列 变为可修改状态时,给列add一个button,且列中元素不可编辑,点击button d出一个模态窗口,可以选择元素 ,赋值给grid当前编辑列中单行表单域中 $("#"+rowid+" input[type='checkbox']")attr("checked",value == "<fmt:message key='casevalid'/>" true:false);
if(iCol==3){
$("#irowNum")val(rowid);
var $data = $("#"+rowid +">td"); //获取这个行里所有的td元素,即:获取所有子元素
$zoneInput = $datafind("input")eq("1") ;
$zoneInputcss("width","100px");
$zoneInputattr("disabled",true);
$zoneInputafter("<input type='button' value='选择' onclick='fnCallDialogForEidt()' />");
}
}
});
//grid添加新的一行
var newrowid ;
function addRow()
{
$("#operate")val("");
var selectedId = $("#plsfList")jqGrid("getGridParam", "selrow");
var ids = jQuery("#plsfList")jqGrid('getDataIDs');
//获得当前最大行号(数据编号)
var rowid = Mathmaxapply(Math,ids);
//获得新添加行的行号(数据编号)
newrowid = rowid+1;
var dataRow = {
id: "",
valid:"",
zoneID:'',
factorPG:'',
factorQG:'',
factorPL:'',
factorQL:'',
caseID:''
};
//将新添加的行插入到第一列
$("#plsfList")jqGrid("addRowData", newrowid, dataRow, "first");
//设置grid单元格不可编辑
$("#plsfList")setGridParam({cellEdit:false});
//设置grid单元格可编辑
$('#plsfList')jqGrid('editRow', newrowid, false);
//确定按钮可用
$("#confirm_btn")attr("disabled",false);
//给添加的列加选择按钮
var $zoneInput = $("#"+newrowid+"_zoneName");
$zoneInputattr("disabled",true)css("width",100);
$zoneInputafter("<input type='button' value='选择' onclick='fnCallDialogForEidt()' />");
}
function insertPlsf(){
var $plsfList = $("#plsfList") ;
var $operate = $("#operate")val();
//设置grid单元格可编辑
$plsfListsetGridParam({cellEdit:true});
//设置grid行不可编辑
//$plsfListjqGrid('editRow', newrowid, false);
//拼接请求的url
var url = '<%=basePath%>'+"/lfc/powerlsfactor/save" ;
var $params = $plsfListfind("input[id^="+newrowid+"]");
var $check_val = $paramseq(0)is(':checked') 1:0;
url = addParamToUrl(url,'valid',$check_val);
url = addParamToUrl(url,'zoneID',$("#zone_id")val());
url = addParamToUrl(url,'factorPG',$paramseq(2)val());
url = addParamToUrl(url,'factorQG',$paramseq(3)val());
url = addParamToUrl(url,'factorPL',$paramseq(4)val());
url = addParamToUrl(url,'factorQL',$paramseq(5)val());
var $caseID = $("#caseID")val();
url = addParamToUrl(url,'caseID',$caseID);
$ajax({url:url,type:"post",timeout:5000,
success:function(data){
showMessage(data);
reloadGrid();
}
});
//将新添加行号 初始为空
newrowid = '' ;
//确定按钮不可用
$("#confirm_btn")attr("disabled",true);
}
//格式zone列输出内容
function formatZone(cellvalue, options, rowObject){
if(cellvalue == 0){
return 0;
}else if(cellvalue == 1){
return 1;
}else if(cellvalue == 2){
return 2;
}else{
return 3;
}
}
function isRowNeedSave($jqgrid){
var $editTr = $jqgridfind("tr[editable=1]") ;
var flag = false ;
if ( $editTr && $editTrlength > 0){
flag = true ;
}
return flag ;
}
function cancel(){
reloadGrid();
//确定按钮不可用
$("#confirm_btn")attr("disabled",true);
//设置grid单元格可编辑
$("#plsfList")setGridParam({cellEdit:true});
//设置grid单元格可编辑
$('#plsfList')jqGrid('editRow', newrowid, true);
}
function fnCallDialogForEidt(){
//获得当前行号(数据编号)
var returnValue = "";
returnValue = windowshowModalDialog("<c:url value='/element/zone/querycaseID="+$("#caseID")val()+"' />",window,"");
if(returnValue==""||returnValue==null)
return;
var $plsfList = $("#plsfList");
var $params = $plsfListfind("input[id$='zoneName']");
var names = returnValuesplit(",");
$paramseq(0)val(names[1]);
$("#zone_id")val(names[0]);
}
添加checkboxprotectedvoidPage_Load(objectsender,EventArgse){CheckBoxchk=newCheckBox();chkText="testall";//这里可以换成数据库的内容chkCheckedChanged+=newEventHandler(chk_CheckedChanged);chkAutoPostBack=true;PageFormControlsAdd(chk);for(inti=0;i<10;i++){CheckBoxchk2=newCheckBox();chk2Text="test"+iToString();//这里可以换成数据库的内容chk2Checked=(i%3==0);//这里可以换成数据库的内容PageFormControlsAdd(chk2);}}voidchk_CheckedChanged(objectsender,EventArgse){CheckBoxall=senderasCheckBox;foreach(ControlctlinPageFormControls){if(ctlisCheckBox){CheckBoxchk=ctlasCheckBox;chkChecked=allChecked;}}}
jgGrid定义增加面 multiselect: true, onSelectRow: function (rowId, status, e) { var rowIds = jQuery("#table1")jqGrid('getGridParam', 'selarrrow');
以上就是关于jqGrid 怎么动态控制 multiselect 属性全部的内容,包括:jqGrid 怎么动态控制 multiselect 属性、jqGrid加checkbox,怎样实现动态显示和隐藏列,求大神指教或给个例子,谢谢谢谢。、jqgrid 怎么把行插入到指定位置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)