Created
March 20, 2012 10:17
-
-
Save cajones/2133858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SharepointSearchForm = (function () { | |
var uniqueArray = function (arr) { | |
var uniqueValues = []; | |
$.each(arr, function (index, item) { | |
if ($.inArray(item, uniqueValues) === -1) { | |
uniqueValues.push(item); | |
} | |
}); | |
return uniqueValues; | |
}; | |
var viewFieldsAsCaml = function (fields) { | |
var caml = "<ViewFields Properties='True'>"; | |
$.each(fields, function (i, e) { | |
caml += "<FieldRef Name='" + e + "' />"; | |
}); | |
caml += "</ViewFields>"; | |
return caml; | |
}; | |
var queryAsCaml = function (queryValues) { | |
if (queryValues.length == 0) return ''; | |
var caml = "<Query><Where>"; | |
if (queryValues.length > 1) { | |
caml += "<And>"; | |
} | |
$.each(queryValues, function (i, e) { | |
caml += '<Eq><FieldRef Name="' + e.id + '" /><Value Type="Text">' + e.value + '</Value></Eq>'; | |
}); | |
if (queryValues.length > 1) { | |
caml += "</And>"; | |
} | |
caml += "</Where></Query>"; | |
return caml; | |
}; | |
return { | |
loadUniqueValues: function (field, callback) { | |
$().SPServices({ | |
operation: "GetListItems", | |
async: false, | |
listName: "BestPracticeUploadList", | |
CAMLViewFields: viewFieldsAsCaml([field]), | |
ignoreCase: true, | |
CAMLQuery: "", | |
CAMLQueryOptions: "<QueryOptions />", | |
completefunc: function (xData, Status) { | |
var rows = $(xData.responseXML).SPFilterNode("z:row"); | |
var fieldlist = $.map(rows, function (item, index) { | |
var val = $(item).attr('ows_' + field); | |
return val === undefined ? '' : val; | |
}); | |
if (callback) { | |
callback(uniqueArray(fieldlist)); | |
} | |
} | |
}); | |
}, | |
search: function (fields, queryValues, callback) { | |
$().SPServices({ | |
operation: "GetListItems", | |
async: false, | |
listName: "BestPracticeUploadList", | |
CAMLViewFields: viewFieldsAsCaml(fields), | |
ignoreCase: true, | |
CAMLQuery: queryAsCaml(queryValues), | |
CAMLQueryOptions: "<QueryOptions />", | |
completefunc: function (xData, Status) { | |
if (callback) { | |
callback($(xData.responseXML).SPFilterNode("z:row")); | |
} | |
} | |
}); | |
} | |
}; | |
})(); | |
$(document).ready(function () { | |
var fields = ['Brand', 'Main', 'Description', 'Function', 'KeyDriver', 'Stakeholder', 'Country', 'Strategy']; | |
var appendToSelect = function (selector, value) { | |
$(selector).append($('<option>').append(value)); | |
}; | |
var loadAllWithUniqueValues = function (fields, func) { | |
$.each(fields, function (index, item) { | |
SharepointSearchForm.loadUniqueValues(item, function (results) { | |
$.each(results, function (index, el) { | |
func('#' + item, el); | |
}); | |
}); | |
}); | |
}; | |
loadAllWithUniqueValues(fields, appendToSelect); | |
$("#searchButton").click(function () { | |
var getUserInputFromDropdown = function(selector) { | |
var selects = $(selector); | |
selects.each(function (index, element) { | |
var $e = $(element); | |
var values = []; | |
if($e.val()) { | |
values.push({ | |
id: element.id, | |
value: $e.val() | |
}); | |
} | |
return values; | |
}); | |
}; | |
var getFieldValue = function (obj, fieldName) { | |
var value = $(obj).attr("ows_" + fieldName); | |
return value ? value : 'Nothing to see here!'; | |
}; | |
SharepointSearchForm.search(fields, getUserInputFromDropdown('#mysearchform select'), function (results) { | |
results.each(function (index, item) { | |
var resultsHtml = "<tr><th scope='row' class='project'>" + getFieldValue(this, "Main") + " " + getFieldValue(this, "Country") + "</th><td class='description'><a href='#'>" + getFieldValue(this, "Description") + "</a></td><td>" + getFieldValue(this, "Brand") + "</td><td>" + getFieldValue(this, "Stakeholder") + "</td><td><a href='#' title='Download'>Documents Not yet dynamic</a></td></tr>"; | |
$("#searchResults").append(resultsHtml); | |
}); | |
}); | |
}); | |
$('#mysearchform select').change(function () { | |
$('#searchResults').empty(); | |
$(this).attr('value', $(this).val()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment