Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skokasik/7069670 to your computer and use it in GitHub Desktop.
Save skokasik/7069670 to your computer and use it in GitHub Desktop.
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
};
}();
@ameliapond
Copy link

« attachViewFilter » is undefined

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