Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Javascript: Using query string to filter list view in sharepoint.
/*
object to save filters and apply to URL
*/
var QueryStringManager = function(window) {
this.window = window;
};
QueryStringManager.prototype = function() {
var filters = [],
viewFilters = [],
multifilter = {"IsSet":false},
url = function() {
return this.window.location.protocol + "//" + this.window.location.host + this.window.location.pathname;
},
regexIndexOf = function(phrase, regex, startpos) {
// thanks StackOverflow: http://stackoverflow.com/questions/273789/is-there-a-version-of-javascripts-string-indexof-that-allows-for-regular-expr
var indexOf = phrase.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
},
getExistingFilter = function(filterName) {
var urlSearch = this.window.location.search;
var queryStrings = {};
urlSearch.replace( new RegExp("([^?=&]+)(=([^&]*))?", "g"), function($0, $1, $2, $3) { queryStrings[$1] = $3; } );
return queryStrings;
},
attachFilter = function(fieldName, fieldValue) {
if ((fieldValue !== -1) && (fieldValue !== -2)) {
filters.push( {"FilterField":fieldName, "FilterValue":fieldValue} );
}
return this;
},
attachMultiFilter = function(fieldName, fieldValues) {
multifilter = $.extend(multifilter, { "IsSet":true, "FilterField": fieldName, "FilterValues": fieldValues });
return this;
},
applyFilter = function() {
var furl = url() + "?";
for(var i=0;i<viewFilters.length;i++)
{
furl = furl + "View=" + viewFilters[i].View + "&";
}
if (multifilter.IsSet) {
furl = furl + "FilterName=" + multifilter.FilterField + "&FilterMultiValue=" + multifilter.FilterValues + "&";
}
for (var i = 0; i<filters.length; i++) {
furl = furl + "FilterField" + (i + 1) + "=" + filters[i].FilterField + "&FilterValue" + (i + 1) + "=" + filters[i].FilterValue + "&";
}
this.window.location = furl;
},
clearFilters = function() {
filters = [];
multifilter = {"IsSet":false};
return this;
};
return { getExistingFilter : getExistingFilter,
attachFilter : attachFilter,
applyFilter : applyFilter,
attachMultiFilter : attachMultiFilter,
clearFilters : clearFilters,
attachViewFilter : attachViewFilter
};
}();

« attachViewFilter » is undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment