Skip to content

Commit

Permalink
#24 - added support for cascading parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjuden committed Mar 26, 2017
1 parent 6a743e2 commit f92a2d8
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script type="text/javascript" src="~/js/jquery.highlight-5.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('select').select2();
_initializeReportViewerControls();
$('.FirstPage, .ViewReport, .Refresh').click(function () {
if (!$(this).attr('disabled')) {
Expand Down Expand Up @@ -81,6 +81,33 @@
});
});
function _initializeReportViewerControls() {
$('select').select2();
try {
ReportViewer_Register_OnChanges();
} catch (e) { }
}
function reloadParameters() {
var params = $('.ParametersContainer :input').serializeArray();
var urlParams = $.param(params);
showLoadingProgress("Updating Parameters...");
$.get("/Report/ReloadParameters/[email protected]()&" + urlParams).done(function (data) {
if (data != null) {
$('.Parameters').html(data);
_initializeReportViewerControls();
if ($('.ReportViewerContent').find('div').length != 1) {
$('.ReportViewerContent').html('<div class="ReportViewerInformation">Please fill parameters and run the report...</div>');
}
}
hideLoadingProgress();
});
}
function showLoadingProgress(message) {
hideLoadingProgress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script type="text/javascript" src="~/Scripts/jquery.highlight-5.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('select').select2();
_initializeReportViewerControls();
$('.FirstPage, .ViewReport, .Refresh').click(function () {
if (!$(this).attr('disabled')) {
Expand Down Expand Up @@ -67,7 +67,7 @@
$('#ReportViewerSearchText').on("keypress", function (e) {
if (e.keyCode == 13) {
// Cancel the default action on keypress event
e.preventDefault();
e.preventDefault();
findText();
}
});
Expand All @@ -81,6 +81,33 @@
});
});
function _initializeReportViewerControls() {
$('select').select2();
try {
ReportViewer_Register_OnChanges();
} catch (e) { }
}
function reloadParameters() {
var params = $('.ParametersContainer :input').serializeArray();
var urlParams = $.param(params);
showLoadingProgress("Updating Parameters...");
$.get("/Report/ReloadParameters/[email protected]()&" + urlParams).done(function (data) {
if (data != null) {
$('.Parameters').html(data);
_initializeReportViewerControls();
if ($('.ReportViewerContent').find('div').length != 1) {
$('.ReportViewerContent').html('<div class="ReportViewerInformation">Please fill parameters and run the report...</div>');
}
}
hideLoadingProgress();
});
}
function showLoadingProgress(message) {
hideLoadingProgress();
Expand Down
144 changes: 94 additions & 50 deletions AlanJuden.MvcReportViewer.NetCore/CoreHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,8 @@ public static HtmlString RenderReportViewer(this IHtmlHelper helper, ReportViewe
sb.AppendLine(" <div class='ReportViewerHeader row'>");
sb.AppendLine(" <div class='ParametersContainer col-sm-12'>");
sb.AppendLine(" <div class='Parameters col-sm-10'>");
//Parameters start
foreach (var reportParameter in contentData.Parameters)
{
sb.AppendLine(" <div class='Parameter col-md-6 col-sm-12'>");
if (reportParameter.PromptUser || model.ShowHiddenParameters)
{
sb.AppendLine($" <div class='col-sm-4'><label for='{reportParameter.Name}'>{reportParameter.Prompt}</label></div>");

sb.AppendLine(" <div class='col-sm-8'>");
if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any())
{
sb.AppendLine($" <select id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(reportParameter.MultiValue == true ? "multiple='multiple'" : "")}>");
foreach (var value in reportParameter.ValidValues)
{
sb.AppendLine($" <option value='{value.Value}' {(reportParameter.SelectedValues.Contains(value.Value) ? "selected='selected'" : "")}>{value.Label}</option>");
}
sb.AppendLine($" </select>");
}
else
{
var selectedValue = reportParameter.SelectedValues.FirstOrDefault();

if (reportParameter.Type == ReportService.ParameterTypeEnum.Boolean)
{
sb.AppendLine($" <input type='checkbox' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(selectedValue.ToBoolean() ? "checked='checked'" : "")} />");
}
else if (reportParameter.Type == ReportService.ParameterTypeEnum.DateTime)
{
sb.AppendLine($" <input type='datetime' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{selectedValue}' />");
}
else
{
sb.AppendLine($" <input type='text' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{selectedValue}' />");
}
}

sb.AppendLine(" </div>");
}
else
{
if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any())
{
var values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray();
sb.AppendLine($" <input type='hidden' id='{reportParameter.Name}' name='{reportParameter.Name}' value='{String.Join(",", values)}' />");
}
}

sb.AppendLine($" <input type='hidden' id='ReportViewerEnablePaging' name='ReportViewerEnablePaging' value='{model.EnablePaging}' />");

sb.AppendLine(" </div>");
}
sb.AppendLine(ParametersToHtmlString(contentData.Parameters, model));

sb.AppendLine(" </div>");

Expand Down Expand Up @@ -168,7 +119,100 @@ public static HtmlString RenderReportViewer(this IHtmlHelper helper, ReportViewe
sb.AppendLine(" </div>");
sb.AppendLine("</form>");

sb.AppendLine("<script type='text/javascript'>");
sb.AppendLine(" function ReportViewer_Register_OnChanges() {");

var dependencyFieldKeys = new List<string>();
foreach (var parameter in contentData.Parameters.Where(x => x.Dependencies != null && x.Dependencies.Any()))
{
foreach (var key in parameter.Dependencies)
{
if (!dependencyFieldKeys.Contains(key))
{
dependencyFieldKeys.Add(key);
}
}
}

foreach (var queryParameter in contentData.Parameters.Where(x => dependencyFieldKeys.Contains(x.Name)))
{
sb.AppendLine(" $('#" + queryParameter.Name + "').change(function () {");
sb.AppendLine(" reloadParameters();");
sb.AppendLine(" });");
}

sb.AppendLine(" }");

sb.AppendLine("</script>");

return new HtmlString(sb.ToString());
}

public static string ParametersToHtmlString(System.Collections.Generic.List<ReportParameterInfo> parameters, ReportViewerModel model)
{
StringBuilder sb = new StringBuilder();

if (parameters == null)
{
var contentData = new ReportExportResult();
var definedParameters = ReportServiceHelpers.GetReportParameters(model, true);
contentData.SetParameters(definedParameters, model.Parameters);
parameters = contentData.Parameters;
}

//Parameters start
foreach (var reportParameter in parameters)
{
sb.AppendLine(" <div class='Parameter col-md-6 col-sm-12'>");
if (reportParameter.PromptUser || model.ShowHiddenParameters)
{
sb.AppendLine($" <div class='col-sm-4'><label for='{reportParameter.Name}'>{reportParameter.Prompt}</label></div>");

sb.AppendLine(" <div class='col-sm-8'>");
if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any())
{
sb.AppendLine($" <select id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(reportParameter.MultiValue == true ? "multiple='multiple'" : "")}>");
foreach (var value in reportParameter.ValidValues)
{
sb.AppendLine($" <option value='{value.Value}' {(reportParameter.SelectedValues.Contains(value.Value) ? "selected='selected'" : "")}>{value.Label}</option>");
}
sb.AppendLine($" </select>");
}
else
{
var selectedValue = reportParameter.SelectedValues.FirstOrDefault();

if (reportParameter.Type == ReportService.ParameterTypeEnum.Boolean)
{
sb.AppendLine($" <input type='checkbox' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' {(selectedValue.ToBoolean() ? "checked='checked'" : "")} />");
}
else if (reportParameter.Type == ReportService.ParameterTypeEnum.DateTime)
{
sb.AppendLine($" <input type='datetime' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{selectedValue}' />");
}
else
{
sb.AppendLine($" <input type='text' id='{reportParameter.Name}' name='{reportParameter.Name}' class='form-control' value='{selectedValue}' />");
}
}

sb.AppendLine(" </div>");
}
else
{
if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any())
{
var values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray();
sb.AppendLine($" <input type='hidden' id='{reportParameter.Name}' name='{reportParameter.Name}' value='{String.Join(",", values)}' />");
}
}

sb.AppendLine($" <input type='hidden' id='ReportViewerEnablePaging' name='ReportViewerEnablePaging' value='{model.EnablePaging}' />");

sb.AppendLine(" </div>");
}

return sb.ToString();
}
}
}
9 changes: 9 additions & 0 deletions AlanJuden.MvcReportViewer.NetCore/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public JsonResult FindStringInReport(string reportPath, string searchText, int?
return Json(ReportServiceHelpers.FindStringInReport(model, searchText, page).ToInt32());
}

public JsonResult ReloadParameters(string reportPath)
{
var model = this.GetReportViewerModel(Request);
model.ViewMode = ReportViewModes.View;
model.ReportPath = reportPath;

return Json(AlanJuden.MvcReportViewer.CoreHtmlHelpers.ParametersToHtmlString(null, model));
}

public ActionResult PrintReport(string reportPath)
{
var model = this.GetReportViewerModel(Request);
Expand Down
1 change: 1 addition & 0 deletions AlanJuden.MvcReportViewer.NetCore/ReportExportResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal void SetParameters(ReportService.ReportParameter[] definedReportParamet
{
var reportParameter = new ReportParameterInfo();
reportParameter.AllowBlank = definedReportParameter.AllowBlank;
reportParameter.Dependencies = definedReportParameter.Dependencies;
reportParameter.MultiValue = definedReportParameter.MultiValue;
reportParameter.Name = definedReportParameter.Name;
reportParameter.Prompt = definedReportParameter.Prompt;
Expand Down
3 changes: 2 additions & 1 deletion AlanJuden.MvcReportViewer.NetCore/ReportParameterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class ReportParameterInfo
public List<string> SelectedValues { get; set; }
public ReportService.ParameterTypeEnum Type { get; set; }
public bool PromptUser { get; set; }
public bool AllowBlank { get; internal set; }
public bool AllowBlank { get; set; }
public string[] Dependencies { get; set; }

public ReportParameterInfo()
{
Expand Down
19 changes: 19 additions & 0 deletions AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ public static ReportService.ReportParameter[] GetReportParameters(ReportViewerMo
ReportService.ParameterValue[] values = null;
ReportService.DataSourceCredentials[] rsCredentials = null;

if (model != null && model.Parameters != null && model.Parameters.Any())
{
var tempParameters = new List<ReportService.ParameterValue>();
foreach (var parameter in model.Parameters)
{
foreach (var value in parameter.Value.Where(x => !String.IsNullOrEmpty(x)))
{
tempParameters.Add(new ReportService.ParameterValue()
{
Label = parameter.Key,
Name = parameter.Key,
Value = value
});
}
}

values = tempParameters.ToArray();
}

var parameters = service.GetReportParametersAsync(model.ReportPath, historyID, forRendering, values, rsCredentials).Result;

return parameters;
Expand Down
5 changes: 3 additions & 2 deletions AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ public void AddParameter(string name, string[] values)
}
}

private static List<string> KEYS_TO_IGNORE = new List<string>() { "ReportViewerEnablePaging", "reportPath", "startPage", "searchText", "page" };
private static string[] VALUE_SEPARATORS = new string[] { "," };

public void BuildParameters(HttpRequest request)
{
if (request.Query != null && request.Query.Keys != null)
{
foreach (var key in request.Query.Keys)
foreach (var key in request.Query.Keys.Where(x => !KEYS_TO_IGNORE.Contains(x)))
{
this.AddParameter(key, request.Query[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray());
}
Expand All @@ -97,7 +98,7 @@ public void BuildParameters(HttpRequest request)
{
if (request.Form != null && request.Form.Keys != null)
{
foreach (var key in request.Form?.Keys)
foreach (var key in request.Form?.Keys.Where(x => !KEYS_TO_IGNORE.Contains(x)))
{
this.AddParameter(key, request.Form[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray());
}
Expand Down
Loading

0 comments on commit f92a2d8

Please sign in to comment.