Skip to content

Commit a99d2b6

Browse files
committed
Added button htmlattributes
1 parent abf901a commit a99d2b6

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

Griddly.Mvc/GriddlyButton.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace Griddly.Mvc
66
{
77
public class GriddlyButton
88
{
9+
public GriddlyButton(string additionalClassName = null)
10+
{
11+
Buttons = new List<GriddlyButton>();
12+
13+
Enabled = true;
14+
Action = GriddlyButtonAction.Navigate;
15+
16+
ClassName = ((GriddlySettings.DefaultButtonClassName ?? "") + " " + (additionalClassName ?? "")).Trim();
17+
}
18+
919
public string Argument { get; set; }
1020
public Func<object, object> ArgumentTemplate { get; set; }
1121

@@ -20,26 +30,23 @@ public class GriddlyButton
2030
public string Target { get; set; }
2131
public string ConfirmMessage { get; set; }
2232
public bool AlignRight { get; set; }
33+
34+
/// <summary>
35+
/// The row ids to include in the button action (default uses grid default)
36+
/// </summary>
2337
public string[] RowIds { get; set; }
38+
2439
/// <summary>
2540
/// Append the selected row ids to the button href as comma separated query strings (only navigate and modal types supported)
2641
/// </summary>
2742
public bool AppendRowIdsToUrl { get; set; }
2843

44+
public IDictionary<string, object> HtmlAttributes { get; set; }
45+
2946
public GriddlyButtonAction Action { get; set; }
3047

3148
public List<GriddlyButton> Buttons { get; set; }
3249

33-
public GriddlyButton(string additionalClassName = null)
34-
{
35-
Buttons = new List<GriddlyButton>();
36-
37-
Enabled = true;
38-
Action = GriddlyButtonAction.Navigate;
39-
40-
ClassName = ((GriddlySettings.DefaultButtonClassName ?? "") + " " + (additionalClassName ?? "")).Trim();
41-
}
42-
4350
public GriddlyButton Add(GriddlyButton item)
4451
{
4552
Buttons.Add(item);

Griddly.Mvc/GriddlySettings.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public GriddlySettings Add(GriddlyButton button)
147147
});
148148
}*/
149149

150-
public GriddlySettings Button(Func<object, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null)
150+
public GriddlySettings Button(Func<object, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null, object htmlAttributes = null)
151151
{
152152
if (enableOnSelection == null)
153153
enableOnSelection = (action == GriddlyButtonAction.Ajax || action == GriddlyButtonAction.AjaxBulk || action == GriddlyButtonAction.Post);
@@ -163,10 +163,13 @@ public GriddlySettings Button(Func<object, object> argumentTemplate, string capt
163163
RowIds = rowIds
164164
};
165165

166+
if (htmlAttributes != null)
167+
button.HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
168+
166169
return Add(button);
167170
}
168171

169-
public GriddlySettings Button(string argument, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null)
172+
public GriddlySettings Button(string argument, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null, object htmlAttributes = null)
170173
{
171174
if (enableOnSelection == null)
172175
enableOnSelection = (action == GriddlyButtonAction.Ajax || action == GriddlyButtonAction.AjaxBulk || action == GriddlyButtonAction.Post);
@@ -182,6 +185,9 @@ public GriddlySettings Button(string argument, string caption, string icon = nul
182185
RowIds = rowIds
183186
};
184187

188+
if (htmlAttributes != null)
189+
button.HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
190+
185191
return Add(button);
186192
}
187193

@@ -409,16 +415,21 @@ public GriddlySettings<TRow> SelectColumn(Dictionary<string, Func<TRow, object>>
409415
return this;
410416
}
411417

412-
public GriddlySettings<TRow> Button<TModel>(Func<TModel, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, string[] rowIds = null)
418+
public GriddlySettings<TRow> Button<TModel>(Func<TModel, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, string[] rowIds = null, object htmlAttributes = null)
413419
{
414-
Add(new GriddlyButton()
420+
var button = new GriddlyButton()
415421
{
416422
ArgumentTemplate = (x) => argumentTemplate((TModel)x),
417423
Text = caption,
418424
Icon = icon,
419425
Action = action,
420426
RowIds = rowIds
421-
});
427+
};
428+
429+
if (htmlAttributes != null)
430+
button.HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
431+
432+
Add(button);
422433

423434
return this;
424435
}

Griddly/Views/Home/TestGrid.cshtml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,33 @@
1919
.Column(x => x.City, "City")
2020
.Column(x => x.State, "State")
2121
.Column(x => x.PostalCode, "Zip", template: x => Html.ActionLink("&" + x.PostalCode, "Profile"))
22+
2223
.Button("", "export", action: GriddlyButtonAction.Javascript, className: "export-xlsx")
2324
.Add(new GriddlyButton() { Text = "Confirm", Argument = "http://google.com", Action = GriddlyButtonAction.PostCriteria, ConfirmMessage = "Please confirm" })
24-
.Button("AlertSelectedIds", "Alert Selected", action: GriddlyButtonAction.Javascript, enableOnSelection: true)
25+
.Add(new GriddlyButton() { IsSeparator = true })
26+
.Add(new GriddlyButton() { IsSeparator = true })
27+
.Button("AlertSelectedIds", "Alert Selected", action: GriddlyButtonAction.Javascript, enableOnSelection: true, htmlAttributes: new { data_test = "hello world" })
28+
.Add(new GriddlyButton() { IsSeparator = true })
2529
.Button("TestPost", "Post Selected", action: GriddlyButtonAction.AjaxBulk, enableOnSelection: true)
30+
.Add(new GriddlyButton()
31+
{
32+
Text = "A dropdown in a group",
33+
Buttons = new List<GriddlyButton>()
34+
{
35+
new GriddlyButton() { Text = "1" },
36+
new GriddlyButton() { Text = "2" },
37+
new GriddlyButton() { Text = "3" }
38+
}
39+
})
40+
.Add(new GriddlyButton() { IsSeparator = true })
41+
.Add(new GriddlyButton()
42+
{
43+
Text = "A dropdown alone",
44+
Buttons = new List<GriddlyButton>()
45+
{
46+
new GriddlyButton() { Text = "1" },
47+
new GriddlyButton() { Text = "2" },
48+
new GriddlyButton() { Text = "3" }
49+
}
50+
})
2651
)

Griddly/Views/Shared/Griddly/BootstrapButton.cshtml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ else
3030
@RenderLink(Model, true, false)
3131
@if (Model.IsSplitDropdown)
3232
{
33-
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
33+
<button type="button" class="btn btn-default dropdown-toggle @GriddlySettings.DefaultButtonClassName" data-toggle="dropdown">
3434
<span class="caret"></span>
3535
</button>
3636
}
@@ -67,19 +67,26 @@ else
6767
}
6868

6969
<a data-role="griddly-button" class="@(!isMenuItem ? "btn btn-default" : null) @(isDropdown && !button.IsSplitDropdown ? "dropdown-toggle" : null) @(!button.Enabled || button.EnableOnSelection == true ? "disabled" : null) @button.ClassName @(!string.IsNullOrWhiteSpace(Model.Icon) ? "btn-with-icon" : null)"
70-
@*onclick="@(button.Action == GriddlyButtonAction.Javascript ? button.Argument : null)"*@
71-
title="@button.Title"
72-
@Html.AttributeIf("target", button.Action == GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
73-
@Html.AttributeIf("href", href != null, href)
74-
@Html.AttributeIf("data-toggle", button.Action != GriddlyButtonAction.Navigate && button.Action != GriddlyButtonAction.Javascript, button.Action.ToString().ToLower())
75-
@Html.AttributeIf("data-toggle", isDropdown && !button.IsSplitDropdown, "dropdown")
76-
@Html.AttributeIf("data-onclick", button.Action == GriddlyButtonAction.Javascript && button.Argument != null, button.Argument)
77-
@Html.AttributeIf("data-url", button.Action == GriddlyButtonAction.Ajax || button.Action == GriddlyButtonAction.AjaxBulk || button.Action == GriddlyButtonAction.Post || button.Action == GriddlyButtonAction.PostCriteria, button.Argument)
78-
@Html.AttributeIf("data-target", button.Action != GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
79-
@Html.AttributeIf("data-enable-on-selection", button.EnableOnSelection, button.EnableOnSelection.ToString().ToLower())
80-
@Html.AttributeIf("data-confirm-message", button.ConfirmMessage != null, button.ConfirmMessage)
81-
@Html.AttributeIf("data-rowids", button.RowIds != null, button.RowIds != null ? Html.AttributeEncode(Json.Encode(button.RowIds.Select(x => x.ToLower()).ToArray())) : null)
82-
@Html.AttributeIf("data-append-rowids-to-url", (button.Action == GriddlyButtonAction.Modal || button.Action == GriddlyButtonAction.Navigate) && button.AppendRowIdsToUrl, "")>
70+
@*onclick="@(button.Action == GriddlyButtonAction.Javascript ? button.Argument : null)"*@
71+
title="@button.Title"
72+
@Html.AttributeIf("target", button.Action == GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
73+
@Html.AttributeIf("href", href != null, href)
74+
@Html.AttributeIf("data-toggle", button.Action != GriddlyButtonAction.Navigate && button.Action != GriddlyButtonAction.Javascript, button.Action.ToString().ToLower())
75+
@Html.AttributeIf("data-toggle", isDropdown && !button.IsSplitDropdown, "dropdown")
76+
@Html.AttributeIf("data-onclick", button.Action == GriddlyButtonAction.Javascript && button.Argument != null, button.Argument)
77+
@Html.AttributeIf("data-url", button.Action == GriddlyButtonAction.Ajax || button.Action == GriddlyButtonAction.AjaxBulk || button.Action == GriddlyButtonAction.Post || button.Action == GriddlyButtonAction.PostCriteria, button.Argument)
78+
@Html.AttributeIf("data-target", button.Action != GriddlyButtonAction.Navigate && !string.IsNullOrWhiteSpace(button.Target), button.Target)
79+
@Html.AttributeIf("data-enable-on-selection", button.EnableOnSelection, button.EnableOnSelection.ToString().ToLower())
80+
@Html.AttributeIf("data-confirm-message", button.ConfirmMessage != null, button.ConfirmMessage)
81+
@Html.AttributeIf("data-rowids", button.RowIds != null, button.RowIds != null ? Html.AttributeEncode(Json.Encode(button.RowIds.Select(x => x.ToLower()).ToArray())) : null)
82+
@Html.AttributeIf("data-append-rowids-to-url", (button.Action == GriddlyButtonAction.Modal || button.Action == GriddlyButtonAction.Navigate) && button.AppendRowIdsToUrl, "")
83+
@if (button.HtmlAttributes != null)
84+
{
85+
foreach (var attr in button.HtmlAttributes)
86+
{
87+
<text>@attr.Key="@attr.Value"</text>
88+
}
89+
}>
8390

8491
@if (!string.IsNullOrWhiteSpace(Model.Icon) && GriddlySettings.IconTemplate != null)
8592
{

0 commit comments

Comments
 (0)