Forum Discussion
xorrkaz
15 years agoGenius
Doug, thanks for this example. We implemented it, but ran into some problems. We fixed them, and I want to share the modified code.
<#assign result_list_size = 3 />
<#assign subject_input_class = "lia-form-subject-input" />
<#assign results_label = text.format("custom.component.ajax_search_before_post.results_label") />
<@liaAddScript>
;(function($) {
if (typeof LITHIUM.CUSTOMER == "undefined") {
LITHIUM.CUSTOMER = {};
}
if (typeof LITHIUM.CUSTOMER.EXAMPLE == "undefined") {
LITHIUM.CUSTOMER.EXAMPLE = {};
}
LITHIUM.CUSTOMER.EXAMPLE.AjaxSearch = function() {
$(document).ready(function() {
$(".${subject_input_class}").each(function(index, subject_input) {
var subject_input = $(subject_input);
subject_input.parent().append("<div class='ajax-message-subject-search-results'></div>");
subject_input.blur(function() {
var subj_input = $(subject_input);
var subjectValue = subj_input.val();
if (subjectValue.length > 0) {
$.ajax({
type: "GET",
url: "/${community.id}/restapi/vc/search/messages",
data: ({
"q":subjectValue,
"page_size":"${result_list_size}",
"restapi.response_style":"view",
"xslt":"json.xsl"
}),
success: function(result) {
$(subj_input).parent().find(".ajax-message-subject-search-results").each(function(index, results_div) {
if (result.response.messages == null || result.response.messages.message.length < 1) {
$(results_div).empty();
} else {
var msgMarkup = "<div><span>${results_label}</span></div>";
msgMarkup += "<table class='lia-list-wide'>";
if (typeof result.response.messages.message.length === 'undefined') {
msgMarkup += "<tr><td><a href='" + result.response.messages.message.view_href + "'>" + result.response.messages.message.subject.$ + "</a></td></tr>";
} else {
$.each(result.response.messages.message, function(index, msg) {
msgMarkup += "<tr><td><a href='" + msg.view_href + "'>" + msg.subject.$ + "</a></td></tr>";
});
}
msgMarkup += "</table>";
$(results_div).empty().append(msgMarkup);
}
});
}
});
}
});
});
});
};
})(LITHIUM.jQuery);
LITHIUM.CUSTOMER.EXAMPLE.AjaxSearch();
</@liaAddScript>
The typeof conditional check is needed there because the json XSLT returns a single element as the object itself instead of a one-element array. I think the XSLT should be updated so that all results are returned as arrays. This will help avoid special casing like this.
DougS
14 years agoKhoros Alumni (Retired)
Ah, good catch -- thanks for the fix xorrkaz!