Skip to content

Commit e56bd0e

Browse files
committed
Merge pull request #7 from ithielnor/master
GriddlyButton enhancements
2 parents 8520b29 + 494c125 commit e56bd0e

File tree

6 files changed

+211
-111
lines changed

6 files changed

+211
-111
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.0.65.0")]
19-
[assembly: AssemblyFileVersion("1.0.65.0")]
18+
[assembly: AssemblyVersion("1.0.66.0")]
19+
[assembly: AssemblyFileVersion("1.0.66.0")]

Griddly.Mvc/GriddlyButton.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class GriddlyButton
1818
public string Icon { get; set; }
1919
public string ClassName { get; set; }
2020
public string Target { get; set; }
21+
public string ConfirmMessage { get; set; }
2122
public bool AlignRight { get; set; }
2223

2324
public GriddlyButtonAction Action { get; set; }
@@ -47,7 +48,6 @@ public HttpVerbs Verb
4748
switch (Action)
4849
{
4950
case GriddlyButtonAction.Navigate:
50-
case GriddlyButtonAction.Report:
5151
return HttpVerbs.Get;
5252
case GriddlyButtonAction.Post:
5353
case GriddlyButtonAction.PostCriteria:
@@ -70,7 +70,6 @@ public enum GriddlyButtonAction
7070
AjaxBulk,
7171
Post,
7272
Modal,
73-
Report,
7473
PostCriteria
7574
}
7675
}

Griddly/Content/griddly.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
color: #333;
4141
}
4242

43+
.griddly th.select
44+
{
45+
width: 22px;
46+
}
47+
4348
.griddly th.sortable
4449
{
4550
cursor: pointer;

Griddly/Scripts/griddly.js

Lines changed: 173 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
this.options.onRefresh = window[onRefresh];
9595

9696
// TODO: should we do this later on so we handle dynamically added buttons?
97-
this.$element.find("[data-toggle=modal][href*='_griddlyIds']").each(function ()
97+
this.$element.find("[data-griddly-toggle=modal][href*='_griddlyIds']").each(function ()
9898
{
9999
$(this).data("griddly-href-template", $(this).attr("href"));
100100
});
@@ -274,7 +274,7 @@
274274
else
275275
ids = "";
276276

277-
this.$element.find("[data-toggle=modal]").each(function ()
277+
this.$element.find("[data-griddly-toggle=modal]").each(function ()
278278
{
279279
var template = $(this).data("griddly-href-template");
280280

@@ -320,98 +320,6 @@
320320
onRowChange();
321321
}, this));
322322

323-
$(this.$element).on("click", "[data-toggle=ajaxbulk]", $.proxy(function (event)
324-
{
325-
var url = $(event.currentTarget).data("url");
326-
var ids = this.getSelected();
327-
328-
if (ids.length == 0 && $(event.currentTarget).data("enable-on-selection"))
329-
return;
330-
331-
$.ajax(url,
332-
{
333-
data: { ids : ids },
334-
traditional: true,
335-
type: "POST"
336-
}).done($.proxy(function (data, status, xhr)
337-
{
338-
// TODO: handle errors
339-
// TODO: go back to first page?
340-
this.refresh();
341-
}, this));
342-
}, this));
343-
344-
$(this.$element).on("click", "[data-toggle=post]", $.proxy(function (event)
345-
{
346-
var url = $(event.currentTarget).data("url");
347-
348-
if (!url)
349-
url = $(event.currentTarget).attr("href");
350-
351-
var ids = this.getSelected();
352-
var inputs = "";
353-
354-
if (ids.length == 0 && $(event.currentTarget).data("enable-on-selection"))
355-
return;
356-
357-
var token = $("input[name^=__RequestVerificationToken]").first();
358-
359-
if (token.length)
360-
inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />';
361-
362-
$.each(ids, function ()
363-
{
364-
inputs += "<input name=\"ids\" value=\"" + this + "\" />";
365-
});
366-
367-
$("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>")
368-
.appendTo("body").submit().remove();
369-
370-
return false;
371-
}, this));
372-
373-
$(this.$element).on("click", "[data-toggle=postcriteria]", $.proxy(function (event)
374-
{
375-
var request = this.buildRequest(false);
376-
var inputs = "";
377-
378-
var token = $("input[name^=__RequestVerificationToken]").first();
379-
380-
if (token.length)
381-
inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />';
382-
383-
for (var key in request)
384-
inputs += '<input name="' + key + '" value="' + request[key] + '" />';
385-
386-
var url = $(event.currentTarget).data("url");
387-
388-
$("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>")
389-
.appendTo("body").submit().remove();
390-
}, this));
391-
392-
$(this.$element).on("click", "[data-toggle=ajax]", $.proxy(function (event)
393-
{
394-
var url = $(event.currentTarget).data("url");
395-
var ids = this.getSelected();
396-
397-
if (ids.length == 0)
398-
return;
399-
400-
for (var i = 0; i < ids.length; i++)
401-
{
402-
$.ajax(url,
403-
{
404-
data: { id: ids[i] },
405-
type: "POST"
406-
}).done($.proxy(function (data, status, xhr)
407-
{
408-
// TODO: handle errors
409-
// TODO: go back to first page?
410-
this.refresh();
411-
}, this));
412-
}
413-
}, this));
414-
415323
$("a.export-xlsx", this.$element).on("click", $.proxy(function (e) {
416324
this.exportFile("xlsx");
417325
e.preventDefault();
@@ -922,9 +830,180 @@
922830
rowClickModal: null
923831
};
924832

833+
var GriddlyButton = function (element, options) {
834+
this.$element = $(element);
835+
this.options = options;
836+
837+
this.create();
838+
839+
if ($(this.$element).is("[data-enable-on-selection=true]")) {
840+
$(this.$element).addClass("disabled");
841+
}
842+
};
843+
844+
GriddlyButton.prototype = {
845+
constructor: GriddlyButton,
846+
847+
// create and bind
848+
create: function () {
849+
var griddly = this.$element.closest("[data-role=griddly]");
850+
var url = this.$element.data("url");
851+
var toggle = this.$element.data("toggle");
852+
var onclick = this.$element.data("onclick");
853+
var confirmMessage = this.$element.data("confirm-message");
854+
var enableOnSelection = this.$element.data("enable-on-selection");
855+
856+
$(this.$element).on("click", $.proxy(function (event) {
857+
if ((typeof confirmMessage === "undefined" || confirm(confirmMessage))) {
858+
if ($(this.$element).triggerHandler("beforeExecute") !== false) {
859+
if (toggle && ["ajaxbulk", "postcriteria", "ajax", "post"].indexOf(toggle) > -1) {
860+
if (!url)
861+
url = $(event.currentTarget).attr("href");
862+
863+
var ids = {};
864+
if (griddly)
865+
ids = $(griddly).griddly("getSelected");
866+
867+
switch (toggle) {
868+
case "ajaxbulk":
869+
if (ids.length == 0 && enableOnSelection)
870+
return;
871+
return this.ajaxBulk(url, ids);
872+
873+
case "post":
874+
if (ids.length == 0 && enableOnSelection)
875+
return;
876+
return this.post(url, ids);
877+
878+
case "postcriteria":
879+
if (!griddly)
880+
return;
881+
return this.postCriteria(url, $(griddly).griddly("buildRequest"));
882+
883+
case "ajax":
884+
if (ids.length == 0)
885+
return;
886+
return this.ajax(url, ids);
887+
}
888+
}
889+
890+
if (onclick) {
891+
var f = window[onclick];
892+
if ($.isFunction(f))
893+
return f.call(this.$element);
894+
throw "onclick must be a global function";
895+
// we do not support eval cause it's insecure
896+
}
897+
898+
return true;
899+
}
900+
}
901+
902+
return false;
903+
}, this));
904+
},
905+
906+
ajaxBulk: function (url, ids) {
907+
$.ajax(url,
908+
{
909+
data: { ids: ids },
910+
traditional: true,
911+
type: "POST"
912+
}).done($.proxy(function (data, status, xhr) {
913+
// TODO: handle errors
914+
// TODO: go back to first page?
915+
this.refresh();
916+
917+
$(this.$element).triggerHandler("afterExecute", [data, status, xhr]);
918+
}, this));
919+
},
920+
921+
post: function (url, ids) {
922+
var inputs = "";
923+
924+
var token = $("input[name^=__RequestVerificationToken]").first();
925+
926+
if (token.length)
927+
inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />';
928+
929+
$.each(ids, function () {
930+
inputs += "<input name=\"ids\" value=\"" + this + "\" />";
931+
});
932+
933+
$("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>")
934+
.appendTo("body").submit().remove();
935+
936+
return false;
937+
},
938+
939+
postCriteria: function (url, request) {
940+
var inputs = "";
941+
942+
var token = $("input[name^=__RequestVerificationToken]").first();
943+
944+
if (token.length)
945+
inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />';
946+
947+
for (var key in request)
948+
inputs += '<input name="' + key + '" value="' + request[key] + '" />';
949+
950+
$("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>")
951+
.appendTo("body").submit().remove();
952+
},
953+
954+
ajax: function (url, ids) {
955+
for (var i = 0; i < ids.length; i++) {
956+
$.ajax(url,
957+
{
958+
data: { id: ids[i] },
959+
type: "POST"
960+
}).done($.proxy(function (data, status, xhr) {
961+
// TODO: handle errors
962+
// TODO: go back to first page?
963+
this.refresh();
964+
965+
$(this.$element).triggerHandler("afterExecute", [data, status, xhr]);
966+
}, this));
967+
}
968+
},
969+
};
970+
971+
$.fn.griddlyButton = function (option, parameter) {
972+
var value;
973+
var args = arguments;
974+
975+
this.each(function () {
976+
var data = $(this).data('griddly'),
977+
options = typeof option == 'object' && option;
978+
979+
// initialize griddly button
980+
if (!data) {
981+
var instanceOptions = $.extend({}, $.fn.griddlyButton.defaults, options);
982+
983+
$(this).data('griddly', (data = new GriddlyButton(this, instanceOptions)));
984+
}
985+
986+
// call griddly method
987+
if (typeof option == 'string') {
988+
value = data[option].apply(data, Array.prototype.slice.call(args, 1));
989+
}
990+
991+
});
992+
993+
if (value !== undefined)
994+
return value;
995+
else
996+
return this;
997+
};
998+
999+
$.fn.griddlyButton.defaults =
1000+
{
1001+
};
1002+
9251003
$(function()
9261004
{
9271005
$("[data-role=griddly]").griddly();
1006+
$("[data-role=griddly-button]").griddlyButton();
9281007

9291008
// patch stupid bootstrap js so it doesn't .empty() our inline filter dropdowns
9301009
// remove once bs fixes: https://github.com/twbs/bootstrap/pull/14244

Griddly/Views/Home/TestGrid.cshtml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
PageSize = 5
99

1010
}
11+
.SelectColumn(x => x.ToString())
1112
.Column(x => x.FirstName, "First Name")
12-
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending)
13-
.Column(x => x.Company, "Company")
14-
.Column(x => x.Address, "Address")
15-
.Column(x => x.City, "City")
16-
.Column(x => x.State, "State")
17-
.TemplateColumn(x => Html.ActionLink("&" + x.PostalCode, "Profile"), "Zip")
18-
.Button("", "export", action: GriddlyButtonAction.Javascript, className: "export-xlsx")
13+
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending)
14+
.Column(x => x.Company, "Company")
15+
.Column(x => x.Address, "Address")
16+
.Column(x => x.City, "City")
17+
.Column(x => x.State, "State")
18+
.TemplateColumn(x => Html.ActionLink("&" + x.PostalCode, "Profile"), "Zip")
19+
.Button("", "export", action: GriddlyButtonAction.Javascript, className: "export-xlsx")
20+
.Add(new GriddlyButton() { Text = "Confirm", Argument = "http://google.com", Action = GriddlyButtonAction.PostCriteria, ConfirmMessage = "Please confirm" })
1921
)

0 commit comments

Comments
 (0)