setSelectionType(SelectionStyle.SIMPLE);
setSelectionAppearance(SelectionAppearance.CHECKBOX)
后会出现 “Can't select that many records at once”提示,并不能全选。
解决思路: 去掉提示,将选择grid中所有记录替换为选择所有可见的记录。
实现:
第一步: 在GridPager中为ListGrid的DataArrived事件添加去掉提示代码覆盖SmartClient的代码(因为SmartClient是在数据到达后就设置提示,并将checkBoxField设为不可用)。代码如下:
/** * 自定义ListGrid的CheckBoxField属性,覆盖SmartGWT的范围太大不能提示 * 需要在ListGrid的DataArrivedHandler事件处理中调用 * * @param grid 要更改的ListGrid对象 */ public native void changeCheckBoxFieldProperty(ListGrid grid)/*-{ var self = [email protected]::getOrCreateJsObj()(); if (self.getCurrentCheckboxField()) { var cbPos = self.getCheckboxFieldPosition(),falseImage = self.checkboxFieldFalseImage || self.booleanFalseImage; // remove selectionRangeNotLoadedMessage(Can't select that many records at once) prompt // checkbox is enable var props = { disabled: false, showHover: false, prompt: null, title: self.getValueIconHTML(falseImage, field) } self.setFieldProperties(cbPos, props); } }-*/; grid.addDataArrivedHandler(new DataArrivedHandler() { public void onDataArrived(DataArrivedEvent event) { changeCheckBoxFieldProperty(grid);//覆盖SmartGWT不能选择提示 updatePagerControls(getTotalPages()); } });
第二步: 在定义ListGrid时,自定义表单点击事件
//自定义表头单击事件 xxxGrid.addHeaderClickHandler(new HeaderClickHandler(){ public void onHeaderClick(HeaderClickEvent event) { Boolean re = changeSelectRange(xxxGrid,event.getFieldNum()); if(re != null){ event.cancel(); } } }); /** * javascript本地方法。 自定义checkBoxHeader选择范围为当前页面,解决默认选择所有导致出现提示"Can't select that many records at once"问题。 * @param grid 要更改CheckboxHeader选择的ListGrid对象 * @param fieldNum checkBoxField字段位置 * @return 如果该字段是checkBoxField则返回当前的状态,选中为true,未选为false,如果该字段不是checkBoxField,则返回null */ public native Boolean changeSelectRange(ListGrid grid ,int fieldNum)/*-{ var self = [email protected]::getOrCreateJsObj()(); var field = self.fields[fieldNum]; // check if the checkbox column header was clicked if (self.isCheckboxField(field) && self.selectionType != "single" && self.canSelectAll != false) { if (field._allSelected) { self.deselectAllRecords(); //更改checkboxHeader图标为不选中 var icon = self.checkboxFieldFalseImage || self.booleanFalseImage, title = self.getValueIconHTML(icon, field); self.setFieldTitle(fieldNum, title); field._allSelected=false; } else { var visibleRows = self.getVisibleRows();//return visibleRows start end end point ,if no visiable return [-1,-1] var dataRows = self.data.getRange(visibleRows[0], visibleRows[1]+1); self.selectRecords(dataRows, true); //更改checkboxHeader图标为选中 var icon = self.checkboxFieldTrueImage || self.booleanTrueImage, title = self.getValueIconHTML(icon, field); self.setFieldTitle(fieldNum, title); field._allSelected=true; } return @com.smartgwt.client.util.JSOHelper::toBoolean(Z)(field._allSelected); }else{ //if click other header clear all selected records self.deselectAllRecords(); field._allSelected=false; return null; } }-*/;
附: smartGWT GridPager分页组件