DevExpress MVC Gridview 把header caption 替换为 CheckBox (类似select all)
效果图:
Gridview. cshtml
DevExpressGridHelper gridHelper = new DevExpressGridHelper(settings);
gridHelper.AddCheckBoxColumnWithinHeader(Html, "AddToRpt", "Rpt", true, false, typeof(int), "1", "0", 50, false, true);
DevExpressGridHelper.cs(完整的类在另一篇博文)
public void CommonColumnSetting(MVCxGridViewColumn ao_Column, string as_FieldName, string as_Caption, int ai_Width, bool ab_Editable, bool ab_EditFormSettingsVisible, string as_DisplayFormatString = "", bool ab_IsRequired = false) { ao_Column.FieldName = as_FieldName; ao_Column.Caption = Utils.GetTranslation(as_Caption); ao_Column.HeaderStyle.Wrap = DefaultBoolean.True; if (!string.IsNullOrWhiteSpace(as_DisplayFormatString)) { ao_Column.PropertiesEdit.DisplayFormatString = as_DisplayFormatString; } if (ai_Width != 0) ao_Column.Width = ai_Width; if (ab_EditFormSettingsVisible) ao_Column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True; else ao_Column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False; ao_Column.CellStyle.BackColor = System.Drawing.Color.Transparent; if (ab_IsRequired) gs_MandatoryField += as_FieldName + ","; if (ab_Editable) { gs_EditField += as_FieldName + ","; ao_Column.CellStyle.BackColor = Utils.GetColor.EditableColor; } gs_Fields += as_FieldName + ","; } public void AddCheckBoxColumnWithinHeader(HtmlHelper helper, string as_FieldName, string as_Caption, object ao_ValueChecked, object ao_ValueUnchecked, Type ao_ValueType, string as_ValueCheckedString, string as_ValueUncheckedString, int ai_Width = 0, bool ab_Editable = false, bool ab_EditFormSettingsVisible = false, bool ab_IsRequired = false, string as_ValidationEvent = null) { this._Settings.Columns.Add(column => { this.CommonColumnSetting(column, as_FieldName, as_Caption, ai_Width, ab_Editable, ab_EditFormSettingsVisible, ab_IsRequired: ab_IsRequired); column.SetHeaderTemplateContent(c => { helper.DevExpress().CheckBox(chkSettings => { chkSettings.Text = as_Caption; chkSettings.Name = "chk" + this._Settings.Name + as_FieldName + "_SelectAll"; chkSettings.ControlStyle.Font.Underline = false; chkSettings.Width = Unit.Percentage(100); chkSettings.ControlStyle.HorizontalAlign = HorizontalAlign.Center; chkSettings.Properties.ClientSideEvents.CheckedChanged = "function (s,e){ On" + chkSettings.Name + "(s,e);}"; }).Render(); }); column.ColumnType = MVCxGridViewColumnType.CheckBox; var checkBoxProperties = column.PropertiesEdit as CheckBoxProperties; checkBoxProperties.ValueChecked = ao_ValueChecked; checkBoxProperties.ValueUnchecked = ao_ValueUnchecked; checkBoxProperties.ValueType = ao_ValueType; checkBoxProperties.ValueCheckedString = as_ValueCheckedString; checkBoxProperties.ValueUncheckedString = as_ValueUncheckedString; column.Settings.AllowSort = DefaultBoolean.False; }); }
JavaScript
function OnchkModalDetailGridViewAddToRpt_SelectAll(s,e) { let lb_SelectAll = s.GetValue(); let la_Indicies = ModalDetailGridView.batchEditHelper.GetDataItemVisibleIndices(); for( let i =0; i<la_Indicies.length; i++) { if(!ModalDetailGridView.batchEditApi.IsDeletedRow(la_Indicies[i]) && (ModalDetailGridView.batchEditApi.GetCellValue(la_Indicies[i], "Status")==1) ) { ModalDetailGridView.batchEditApi.SetCellValue(la_Indicies[i], "AddToRpt", (lb_SelectAll==true?1:0)); } } }
function OnModalDetailGridView_BatchStartEdit(s, e, as_Field) { gi_DetailRowIndex = e.visibleIndex; e.cancel = true; var ls_Status = ModalDetailGridView.batchEditApi.GetCellValue(e.visibleIndex, "Status"); if (ls_Status == "0" && e.focusedColumn.fieldName =="AddToRpt") { e.cancel = false; } }