diff --git a/GridBlazor.Tests/Client/ClientTests.cs b/GridBlazor.Tests/Client/ClientTests.cs
index 5d197a09..41293d77 100644
--- a/GridBlazor.Tests/Client/ClientTests.cs
+++ b/GridBlazor.Tests/Client/ClientTests.cs
@@ -64,6 +64,9 @@ public void TestMainMethods()
_client.Groupable(false);
Assert.IsFalse(_client.Grid.ExtSortingEnabled);
Assert.IsFalse(_client.Grid.GroupingEnabled);
+
+ _client.RearrangeableColumns(true);
+ Assert.IsTrue(_client.Grid.RearrangeColumnEnabled);
}
}
}
diff --git a/GridBlazor/CGrid.cs b/GridBlazor/CGrid.cs
index 0f6ba71b..0cd7ae5a 100644
--- a/GridBlazor/CGrid.cs
+++ b/GridBlazor/CGrid.cs
@@ -168,6 +168,8 @@ private CGrid(HttpClient httpClient, string url,
public bool GroupingEnabled { get; set; }
public bool ClearFiltersButtonEnabled { get; set; } = false;
+
+ public bool RearrangeColumnEnabled { get; set; }
///
/// Items, displaying in the grid view
@@ -595,6 +597,26 @@ public string[] GetPrimaryKeys()
return values.ToArray();
}
+ private static readonly Task InsertColumnSucceded = Task.FromResult(true);
+ private static readonly Task InsertColumnFailed = Task.FromResult(false);
+ ///
+ public Task InsertColumn(IGridColumn targetColumn, IGridColumn insertingColumn)
+ {
+
+ var currentPossition = _columnsCollection.IndexOf(insertingColumn);
+ var targetPossition = _columnsCollection.IndexOf(targetColumn);
+ if (currentPossition == -1 || targetPossition == -1 || currentPossition == targetPossition)
+ return InsertColumnFailed;
+
+ var index = currentPossition > targetPossition ? targetPossition : targetPossition - 1;
+ var removed = _columnsCollection.Remove(insertingColumn);
+ if (!removed)
+ return InsertColumnFailed;
+
+ _columnsCollection.Insert(index, insertingColumn);
+ return InsertColumnSucceded;
+ }
+
///
/// Fixed column values for the grid
///
diff --git a/GridBlazor/Client/GridClient.cs b/GridBlazor/Client/GridClient.cs
index 3b38de1a..1108e78c 100644
--- a/GridBlazor/Client/GridClient.cs
+++ b/GridBlazor/Client/GridClient.cs
@@ -187,6 +187,17 @@ public IGridClient Groupable(bool enable)
return this;
}
+ public IGridClient RearrangeableColumns()
+ {
+ return RearrangeableColumns(true);
+ }
+
+ public IGridClient RearrangeableColumns(bool enable)
+ {
+ _source.RearrangeColumnEnabled = enable;
+ return this;
+ }
+
public IGridClient ClearFiltersButton(bool enable)
{
_source.ClearFiltersButtonEnabled = enable;
diff --git a/GridBlazor/Client/IGridClient.cs b/GridBlazor/Client/IGridClient.cs
index f5fc25f1..d962e05a 100644
--- a/GridBlazor/Client/IGridClient.cs
+++ b/GridBlazor/Client/IGridClient.cs
@@ -100,6 +100,16 @@ public interface IGridClient
/// Enable or disable grouping
///
IGridClient Groupable(bool enable);
+
+ ///
+ /// Enable column rearrange
+ ///
+ IGridClient RearrangeableColumns();
+
+ ///
+ /// Enable or disable column rearrange
+ ///
+ IGridClient RearrangeableColumns(bool enable);
///
/// Enable or disable visibility of ClearFiltersButton
diff --git a/GridBlazor/ICGrid.cs b/GridBlazor/ICGrid.cs
index fa931697..0c8c4b7d 100644
--- a/GridBlazor/ICGrid.cs
+++ b/GridBlazor/ICGrid.cs
@@ -43,6 +43,11 @@ public interface ICGrid : IGrid, IGridOptions
///
bool SubGridsOpened { get; }
+ ///
+ /// Set or get default value of rearrange column
+ ///
+ public bool RearrangeColumnEnabled { get; set; }
+
Type Type { get; }
string Url { get; }
@@ -91,6 +96,14 @@ public interface ICGrid : IGrid, IGridOptions
Task DownloadExcel(IJSRuntime js, string filename);
+ ///
+ /// Changes postion of instertingColumn to appear before targetColumn
+ ///
+ /// Column which will be moved
+ /// Column before which it will be inserted
+ /// Retruns true if column was sucessfully inserted before target otherwise false
+ Task InsertColumn(IGridColumn targetColumn, IGridColumn insertingColumn);
+
///
/// Get and set export to an Excel file
///
diff --git a/GridBlazor/Pages/GridComponent.razor.cs b/GridBlazor/Pages/GridComponent.razor.cs
index 63e96765..179b9893 100644
--- a/GridBlazor/Pages/GridComponent.razor.cs
+++ b/GridBlazor/Pages/GridComponent.razor.cs
@@ -920,6 +920,28 @@ protected virtual async Task OnAfterDeleteForm()
await AfterDeleteForm.Invoke(this, _item);
}
}
+
+ public async Task HandleColumnRearranged(GridHeaderComponent gridHeaderComponent)
+ {
+ if (Payload == ColumnOrderValue.Null)
+ return;
+
+ var payload = Payload;
+ Payload = ColumnOrderValue.Null;
+ if (gridHeaderComponent.Column.Name == payload.ColumnName)
+ return;
+
+ var source = Grid.Columns.FirstOrDefault(c => c.Name == payload.ColumnName);
+ if (source is null)
+ return;
+
+ var updated = await Grid.InsertColumn(gridHeaderComponent.Column, source);
+ if (!updated)
+ return;
+
+ _shouldRender = true;
+ StateHasChanged();
+ }
public async Task ExcelHandler()
{
diff --git a/GridBlazor/Pages/GridHeaderComponent.razor b/GridBlazor/Pages/GridHeaderComponent.razor
index 6dea32bc..ddbf6323 100644
--- a/GridBlazor/Pages/GridHeaderComponent.razor
+++ b/GridBlazor/Pages/GridHeaderComponent.razor
@@ -3,8 +3,22 @@
@typeparam T
-
+ |
|