Devexpress ASPxPivotGrid 点击装置列排序
View Code
protected void Page_Load(object sender, EventArgs e)
{
ASPxPivotGrid1.FieldValueTemplate = new FieldValueTemplate(ASPxPivotGrid1);
}
protected void ASPxPivotGrid1_CustomCallback(object sender, PivotGridCustomCallbackEventArgs e)
{
string[] args = e.Parameters.Split('|');
if (args[0] == "SC")
DevCommon.HandleSortByColumnClick((ASPxPivotGrid)sender, args);
}
#region Dev ASPxPivotGrid转置列排序
#region 点击转置列排序
/// <summary>
/// 点击转置列排序
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="args"></param>
public static void HandleSortByColumnClick(ASPxPivotGrid pivotGrid, string[] args)
{
int fieldIndex = int.Parse(args[1]),
visibleIndex = int.Parse(args[3]),
dataIndex = int.Parse(args[4]);
bool isColumn = bool.Parse(args[2]);
PivotArea area = GetArea(isColumn),
crossArea = GetCrossArea(isColumn);
DevExpress.Web.ASPxPivotGrid.PivotGridField dataField;
List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields;
List<object> values;
GetFieldsAndValues(pivotGrid, fieldIndex, visibleIndex, dataIndex, area,out dataField, out fields, out values);
SetSortByColumn(pivotGrid, crossArea, dataField, fields, values);
}
#endregion
#region 设置字段排序
/// <summary>
/// 设置字段排序
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="crossArea"></param>
/// <param name="dataField"></param>
/// <param name="fields"></param>
/// <param name="values"></param>
public static void SetSortByColumn(ASPxPivotGrid pivotGrid, PivotArea crossArea, DevExpress.Web.ASPxPivotGrid.PivotGridField dataField, List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields, List<object> values)
{
pivotGrid.BeginUpdate();
List<DevExpress.Web.ASPxPivotGrid.PivotGridField> crossFields = pivotGrid.GetFieldsByArea(crossArea);
for (int i = 0; i < crossFields.Count; i++)
{
crossFields[i].SortBySummaryInfo.Field = dataField;
if (crossFields[i].SortOrder == PivotSortOrder.Descending)
{
crossFields[i].SortOrder = PivotSortOrder.Ascending;
}
else if (crossFields[i].SortOrder == PivotSortOrder.Ascending)
{
crossFields[i].SortOrder = PivotSortOrder.Descending;
}
crossFields[i].SortBySummaryInfo.Conditions.Clear();
for (int j = 0; j < values.Count; j++)
{
crossFields[i].SortBySummaryInfo.Conditions.Add(
new PivotGridFieldSortCondition(fields[j], values[j]));
}
}
pivotGrid.EndUpdate();
}
#endregion
#region 获取排序字段值
/// <summary>
/// 获取排序字段值
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="fieldIndex"></param>
/// <param name="visibleIndex"></param>
/// <param name="dataIndex"></param>
/// <param name="area"></param>
/// <param name="dataField"></param>
/// <param name="fields"></param>
/// <param name="values"></param>
public static void GetFieldsAndValues(ASPxPivotGrid pivotGrid, int fieldIndex, int visibleIndex, int dataIndex, PivotArea area,out DevExpress.Web.ASPxPivotGrid.PivotGridField dataField, out List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields, out List<object> values)
{
dataField = pivotGrid.GetFieldByArea(PivotArea.DataArea, dataIndex);
fields = pivotGrid.GetFieldsByArea(area);
values = new List<object>(fields.Count);
for (int i = 0; i < fields.Count; i++)
{
object value = pivotGrid.GetFieldValueByIndex(fields[i], visibleIndex);
values.Add(value);
if (fields[i].Index == fieldIndex) break;
}
}
#endregion
#region 获取转置行
public static PivotArea GetCrossArea(bool isColumn)
{
return isColumn ? PivotArea.RowArea : PivotArea.ColumnArea;
}
#endregion
#region 获取转置列
public static PivotArea GetArea(bool isColumn)
{
return isColumn ? PivotArea.ColumnArea : PivotArea.RowArea;
}
#endregion
#endregion
#region Dev ASPxPivotGrid转换列排序模板
public class FieldValueTemplate : ITemplate
{
public FieldValueTemplate(ASPxPivotGrid pivotGrid)
{
this.pivotGrid = pivotGrid;
}
ASPxPivotGrid pivotGrid;
protected ASPxPivotGrid PivotGrid { get { return pivotGrid; } }
#region ITemplate Members
public void InstantiateIn(Control container)
{
PivotGridFieldValueTemplateContainer c = (PivotGridFieldValueTemplateContainer)container;
HyperLink link = new HyperLink();
link.Text = (string)c.Text;
link.NavigateUrl = "javascript:void(0)";
link.Attributes["onclick"] = GetOnClickHandler(c);
c.Controls.Add(link);
bool isSortedByColumn = GetIsSortedByColumn(c);
if (isSortedByColumn)
{
c.Controls.Add(new LiteralControl(" *"));
}
}
bool GetIsSortedByColumn(PivotGridFieldValueTemplateContainer c)
{
List<PivotGridFieldPair> sortedFields = PivotGrid.Data.VisualItems.GetSortedBySummaryFields(c.ValueItem.IsColumn, c.ValueItem.Index);
bool isSortedByColumn = sortedFields != null && sortedFields.Count > 0;
return isSortedByColumn;
}
string GetOnClickHandler(PivotGridFieldValueTemplateContainer c)
{
StringBuilder res = new StringBuilder();
res.Append(pivotGrid.ClientInstanceName).Append(".PerformCallback('SC|");
res.Append(GetFieldIndex(c)).Append("|")
.Append(c.ValueItem.IsColumn).Append("|")
.Append(c.ValueItem.VisibleIndex).Append("|")
.Append(c.ValueItem.DataIndex);
res.Append("');");
return res.ToString();
}
int GetFieldIndex(PivotGridFieldValueTemplateContainer c)
{
return c.ValueItem.Field != null ? c.ValueItem.Field.Index : -1;
}
#endregion
}
#endregion
#region 点击转置列排序
/// <summary>
/// 点击转置列排序
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="args"></param>
public static void HandleSortByColumnClick(ASPxPivotGrid pivotGrid, string[] args)
{
int fieldIndex = int.Parse(args[1]),
visibleIndex = int.Parse(args[3]),
dataIndex = int.Parse(args[4]);
bool isColumn = bool.Parse(args[2]);
PivotArea area = GetArea(isColumn),
crossArea = GetCrossArea(isColumn);
DevExpress.Web.ASPxPivotGrid.PivotGridField dataField;
List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields;
List<object> values;
GetFieldsAndValues(pivotGrid, fieldIndex, visibleIndex, dataIndex, area,out dataField, out fields, out values);
SetSortByColumn(pivotGrid, crossArea, dataField, fields, values);
}
#endregion
#region 设置字段排序
/// <summary>
/// 设置字段排序
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="crossArea"></param>
/// <param name="dataField"></param>
/// <param name="fields"></param>
/// <param name="values"></param>
public static void SetSortByColumn(ASPxPivotGrid pivotGrid, PivotArea crossArea, DevExpress.Web.ASPxPivotGrid.PivotGridField dataField, List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields, List<object> values)
{
pivotGrid.BeginUpdate();
List<DevExpress.Web.ASPxPivotGrid.PivotGridField> crossFields = pivotGrid.GetFieldsByArea(crossArea);
for (int i = 0; i < crossFields.Count; i++)
{
crossFields[i].SortBySummaryInfo.Field = dataField;
if (crossFields[i].SortOrder == PivotSortOrder.Descending)
{
crossFields[i].SortOrder = PivotSortOrder.Ascending;
}
else if (crossFields[i].SortOrder == PivotSortOrder.Ascending)
{
crossFields[i].SortOrder = PivotSortOrder.Descending;
}
crossFields[i].SortBySummaryInfo.Conditions.Clear();
for (int j = 0; j < values.Count; j++)
{
crossFields[i].SortBySummaryInfo.Conditions.Add(
new PivotGridFieldSortCondition(fields[j], values[j]));
}
}
pivotGrid.EndUpdate();
}
#endregion
#region 获取排序字段值
/// <summary>
/// 获取排序字段值
/// </summary>
/// <param name="pivotGrid"></param>
/// <param name="fieldIndex"></param>
/// <param name="visibleIndex"></param>
/// <param name="dataIndex"></param>
/// <param name="area"></param>
/// <param name="dataField"></param>
/// <param name="fields"></param>
/// <param name="values"></param>
public static void GetFieldsAndValues(ASPxPivotGrid pivotGrid, int fieldIndex, int visibleIndex, int dataIndex, PivotArea area,out DevExpress.Web.ASPxPivotGrid.PivotGridField dataField, out List<DevExpress.Web.ASPxPivotGrid.PivotGridField> fields, out List<object> values)
{
dataField = pivotGrid.GetFieldByArea(PivotArea.DataArea, dataIndex);
fields = pivotGrid.GetFieldsByArea(area);
values = new List<object>(fields.Count);
for (int i = 0; i < fields.Count; i++)
{
object value = pivotGrid.GetFieldValueByIndex(fields[i], visibleIndex);
values.Add(value);
if (fields[i].Index == fieldIndex) break;
}
}
#endregion
#region 获取转置行
public static PivotArea GetCrossArea(bool isColumn)
{
return isColumn ? PivotArea.RowArea : PivotArea.ColumnArea;
}
#endregion
#region 获取转置列
public static PivotArea GetArea(bool isColumn)
{
return isColumn ? PivotArea.ColumnArea : PivotArea.RowArea;
}
#endregion
#endregion
#region Dev ASPxPivotGrid转换列排序模板
public class FieldValueTemplate : ITemplate
{
public FieldValueTemplate(ASPxPivotGrid pivotGrid)
{
this.pivotGrid = pivotGrid;
}
ASPxPivotGrid pivotGrid;
protected ASPxPivotGrid PivotGrid { get { return pivotGrid; } }
#region ITemplate Members
public void InstantiateIn(Control container)
{
PivotGridFieldValueTemplateContainer c = (PivotGridFieldValueTemplateContainer)container;
HyperLink link = new HyperLink();
link.Text = (string)c.Text;
link.NavigateUrl = "javascript:void(0)";
link.Attributes["onclick"] = GetOnClickHandler(c);
c.Controls.Add(link);
bool isSortedByColumn = GetIsSortedByColumn(c);
if (isSortedByColumn)
{
c.Controls.Add(new LiteralControl(" *"));
}
}
bool GetIsSortedByColumn(PivotGridFieldValueTemplateContainer c)
{
List<PivotGridFieldPair> sortedFields = PivotGrid.Data.VisualItems.GetSortedBySummaryFields(c.ValueItem.IsColumn, c.ValueItem.Index);
bool isSortedByColumn = sortedFields != null && sortedFields.Count > 0;
return isSortedByColumn;
}
string GetOnClickHandler(PivotGridFieldValueTemplateContainer c)
{
StringBuilder res = new StringBuilder();
res.Append(pivotGrid.ClientInstanceName).Append(".PerformCallback('SC|");
res.Append(GetFieldIndex(c)).Append("|")
.Append(c.ValueItem.IsColumn).Append("|")
.Append(c.ValueItem.VisibleIndex).Append("|")
.Append(c.ValueItem.DataIndex);
res.Append("');");
return res.ToString();
}
int GetFieldIndex(PivotGridFieldValueTemplateContainer c)
{
return c.ValueItem.Field != null ? c.ValueItem.Field.Index : -1;
}
#endregion
}
#endregion
注意:一定要添加ASPxPivotGrid的ClientInstanceName,和ID一样就可以了..不然点击无效----我死在这个上很久..
研究Dev有一段时间了,一直没时间来发表文章....以后会陆续跟上...