予測検索結果:GetJsonがキーアップ機能内で完了した後のjQuery Run関数
質問
ここでは、GetJson / Ajax要求に関する遅延テクニックについてここで多くの回答を読んできましたが、シナリオに合うようには何もありません。私は検索入力を行っています4文字の後のGetJSON呼び出しに基づいて一連の結果を返します。これは、返された結果の量、さらにAjax要求の量を制限する(より効果的な解決策に開く)。
search_input.on('keyup', function() {
var string = $(this).val();
var quiet_chars = 4;
if(string.length < quiet_chars) {
// Simple notifications function which passes a message to a div, and optional class
notification('Type '+(quiet_chars - string.length)+' more characters to search');
}
if(string.length == quiet_chars) {
notification('Searching...', 'loading');
$.getJSON('stores/stores-search/'+string, function(data) {
// Loop through data and build list....
$('.search_results').html('<ul>'+list_html+'</ul>');
// Function which shows/hides list items based on Jquery :contains
searchResults(string);
});
}
if(string.length > quiet_chars) {
searchResults(string);
}
});
.
直面している特定の問題は、迅速に(4を超える)文字を入力した場合、if(string.length > quiet_chars)
の条件は$('.search_results').html('<ul>'+list_html+'</ul>');
が実行される前に満たされ、ユーザーに検索結果がないと指示します。
if(string.length > quiet_chars)
条件を満たす必要があり、返された結果のフィルタリングを続行する必要がありますが、GetJSON要求からDOMにリストが追加された後にのみ。ありがとう。
解決 2
それを手に入れた!
var json_success = false,
ajax_term;
function searchJson(string) {
ajax_term = string;
notification('Searching...', 'loading');
$.getJSON('stores/stores-search/'+string, function(data) {
// Loop through data and build list....
$('.search_results').html('<ul>'+list_html+'</ul>');
searchResults(string);
json_success = true;
});
}
search_input.on('keyup', function() {
var string = $(this).val();
var quiet_chars = 4;
if(string.length < quiet_chars) {
json_success = false;
notification('Type '+(quiet_chars - string.length)+' more characters to search');
} else {
if(json_success !== false || string.substr(0,4) !== ajax_term) {
$.when(searchJson(string)).then(searchResults(string))
} else {
searchResults(string)
}
}
});
.
ここから: https://stackoverflow.com/questions/5280699#5291067
誰かが似たような仕事を手に入れようとしている場合は、ここに私の検索結果が機能します:
//extends :contains to be case insensitive
$.extend($.expr[':'], {
'containsi': function(elem, i, match, array) {
return (elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
// Instant search results
function searchResults(searchTerm) {
var searchSplit = searchTerm.replace(/ /g, "'):containsi('");
$(".search_results .tile").not(":containsi('" + searchSplit + "')").each(function() {
$(this).addClass('hide').removeClass('show');
});
$(".search_results .tile:containsi('" + searchSplit + "')").each(function() {
$(this).removeClass('hide').addClass('show');
});
var resultCount = $('.search_results .show').length;
if(resultCount != '0') {
notification('Showing '+resultCount+' results');
} else {
notification('No results', 'error');
}
}
.
:contains
の大文字と小文字を区別しないバージョン
他のヒント
あなたの仕様に合うように編集された
bool dataLoaded = false; //this is a global variable we have outside of the scope of the search_input.on function
search_input.on('keyup', function() {
var string = $(this).val();
var quiet_chars = 4;
if(string.length < quiet_chars) {
//let's reset the dataLoaded flag if the user ends up deleting characters and what not
if(dataLoaded)
dataLoaded = !dataLoaded;
// Simple notifications function which passes a message to a div, and optional class
notification('Type '+(quiet_chars - string.length)+' more characters to search');
}
if(string.length == quiet_chars) {
notification('Searching...', 'loading');
$.getJSON('stores/stores-search/'+string, function(data) {
// Loop through data and build list....
$('.search_results').html('<ul>'+list_html+'</ul>');
dataLoaded = true;
// Function which shows/hides list items based on Jquery :contains
searchResults(string);
});
}
else if(dataLoaded){
searchResults(string);
}
});
. 所属していません StackOverflow