Skip to content

Commit

Permalink
#118 Persist search value of individual column with statesave
Browse files Browse the repository at this point in the history
  • Loading branch information
ekondur committed Mar 11, 2023
1 parent 19ca8a7 commit f7dd268
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 22 additions & 2 deletions DatatableJS.Net/JSHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,32 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> grid)
{
var tfoot = grid._columnSearching ?
$@"<tfoot>
<tr>
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Searchable ? $"<input type=\"{((a.Type == typeof(DateTime?) || a.Type == typeof(DateTime)) && grid._serverSide ? "date" : "text")}\" style=\"width:100%\" placeholder=\"{a.Title}\" class=\"{grid._columnSearchingCss}\" />" : "")))}
<tr class=""filters"">
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Searchable ? $"<input type=\"{((a.Type == typeof(DateTime?) || a.Type == typeof(DateTime)) && grid._serverSide ? "date" : "text")}\" style=\"width:100%\" placeholder=\"{a.Title}\" class=\"{grid._columnSearchingCss}\" />" : "<input style=\"display:none\" />")))}
</tr>
</tfoot>"
: string.Empty;

var tfootInit = string.Empty;

var initFootSearch = grid._stateSave ?
$@"var dtable = $('#{grid._name}').DataTable();
var dState = dtable.state.loaded();
var dCounter = -1;
if (dState) {{
dtable.columns().eq(0).each(function(colIdx) {{
var colSearch = dState.columns[colIdx].search;
if (dState.columns[colIdx].visible){{
dCounter++;
}}
if (colSearch.search) {{
$('input', $('.filters th')[dCounter]).val(colSearch.search);
}}
}});
dtable.draw();
}};"
: string.Empty;

if (!String.IsNullOrEmpty(grid._callBack.InitComplete) && grid._columnSearching == false)
{
tfootInit = "initComplete: function (settings, json) {";
Expand All @@ -102,6 +120,7 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> grid)
}}
}});
}});
{initFootSearch}
}},"
: string.Empty;
}
Expand All @@ -119,6 +138,7 @@ public static MvcHtmlString Render<T>(this GridBuilder<T> grid)
}}
}});
}});
{initFootSearch}
}},"
: string.Empty;
}
Expand Down
24 changes: 22 additions & 2 deletions DatatableJS/JSHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,32 @@ public static IHtmlContent Render<T>(this GridBuilder<T> grid)
{
var tfoot = grid._columnSearching ?
$@"<tfoot>
<tr>
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Searchable ? $"<input type=\"{((a.Type == typeof(DateTime?) || a.Type == typeof(DateTime)) && grid._serverSide ? "date" : "text")}\" style=\"width:100%\" placeholder=\"{a.Title}\" class=\"{grid._columnSearchingCss}\" />" : "")))}
<tr class=""filters"">
{string.Join(Environment.NewLine, grid._columns.Select(a => string.Format("<th>{0}</th>", a.Searchable ? $"<input type=\"{((a.Type == typeof(DateTime?) || a.Type == typeof(DateTime)) && grid._serverSide ? "date" : "text")}\" style=\"width:100%\" placeholder=\"{a.Title}\" class=\"{grid._columnSearchingCss}\" />" : "<input style=\"display:none\" />")))}
</tr>
</tfoot>"
: string.Empty;

var tfootInit = string.Empty;

var initFootSearch = grid._stateSave ?
$@"var dtable = $('#{grid._name}').DataTable();
var dState = dtable.state.loaded();
var dCounter = -1;
if (dState) {{
dtable.columns().eq(0).each(function(colIdx) {{
var colSearch = dState.columns[colIdx].search;
if (dState.columns[colIdx].visible){{
dCounter++;
}}
if (colSearch.search) {{
$('input', $('.filters th')[dCounter]).val(colSearch.search);
}}
}});
dtable.draw();
}};"
: string.Empty;

if (!String.IsNullOrEmpty(grid._callBack.InitComplete) && grid._columnSearching == false)
{
tfootInit = "initComplete: function (settings, json) {";
Expand All @@ -87,6 +105,7 @@ public static IHtmlContent Render<T>(this GridBuilder<T> grid)
}}
}});
}});
{initFootSearch}
}},"
: string.Empty;
}
Expand All @@ -104,6 +123,7 @@ public static IHtmlContent Render<T>(this GridBuilder<T> grid)
}}
}});
}});
{initFootSearch}
}},"
: string.Empty;
}
Expand Down

0 comments on commit f7dd268

Please sign in to comment.