Skip to content

Commit e01c232

Browse files
committed
Harden up getting filters and none filters
1 parent 63410cd commit e01c232

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

Griddly/Scripts/griddly.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,35 @@
120120
}
121121
};
122122

123-
var serializeObject = function ($elements)
123+
var serializeObject = function ($elements, skipEmpty, multipleSelects)
124124
{
125125
// http://stackoverflow.com/a/1186309/8037
126126
var data = {};
127127

128128
$.each($elements.serializeArray(), function ()
129129
{
130-
if (data[this.name] !== undefined)
130+
if (this.value == '' && skipEmpty)
131+
this.value = null;
132+
else if (this.value == null && !skipEmpty)
133+
this.value = '';
134+
135+
var isMultipleSelect = $.inArray(this.name, multipleSelects) != -1;
136+
137+
if (this.value == null && !isMultipleSelect)
138+
return;
139+
140+
if (data[this.name] !== undefined || isMultipleSelect)
131141
{
132-
if (!data[this.name].push)
142+
if (data[this.name] == null)
143+
data[this.name] = [];
144+
else if (!data[this.name].push)
133145
data[this.name] = [data[this.name]];
134146

135-
data[this.name].push(this.value || '');
147+
data[this.name].push(this.value);
136148
}
137149
else
138150
{
139-
data[this.name] = this.value || '';
151+
data[this.name] = this.value;
140152
}
141153
});
142154

@@ -230,7 +242,11 @@
230242
{
231243
resetContext
232244
.find("[data-griddly-filter-isnoneall=true] [multiple] option[value='']")
233-
.prop("selected", false);
245+
.each(function ()
246+
{
247+
if (newFilters[$(this).closest("[data-filter-field]").data("filter-field")] == null)
248+
$(this).prop("selected", false);
249+
});
234250
}
235251
};
236252

@@ -1710,7 +1726,7 @@
17101726
$(".griddly-filter-cancel, button.close", this).off("click").on("click", function ()
17111727
{
17121728
if (self.$element.triggerHandler("beforeclear.griddly") !== false)
1713-
self.setFilterValues(values, null, true, true);
1729+
self.setFilterValues(values, false);
17141730
});
17151731
})
17161732
.on("shown.bs.modal", function ()
@@ -1737,6 +1753,16 @@
17371753
if (currencySymbol)
17381754
this.options.currencySymbol = currencySymbol;
17391755

1756+
var multipleSelects = [];
1757+
1758+
this.$element.find("[data-griddly-filter-ismultiple=true]").each(
1759+
function (el, i)
1760+
{
1761+
return multipleSelects.push($(this).data("filter-field"));
1762+
});
1763+
1764+
this.options.multipleSelects = multipleSelects;
1765+
17401766
$("form", this.$element).attr("onsubmit", "return false;");
17411767

17421768
$("form", this.$element).on("submit", $.proxy(function (event)
@@ -1801,7 +1827,7 @@
18011827
{
18021828
var allFilters = this.getAllFilterElements();
18031829

1804-
return serializeObject(allFilters);
1830+
return serializeObject(allFilters, true, this.options.multipleSelects);
18051831
},
18061832

18071833
setFilterValue: function (field, value)

0 commit comments

Comments
 (0)