预输入回调不起作用
typeahead callback not working
我对 Typeahead 还很陌生,不能让它与回调一起工作。很抱歉,如果有人问过这个问题,我在搜索时找不到确切的问题。
如果记录只是来自带有字符串的变量,但当它来自数据库时,我让 Typeahead 工作。我不确定如何正确编写回调代码。
我们正在使用 MVC 6,看起来这是 typeahead.js 0.11.1。
什么有效:
var records = [ "Alabama", "Alaska", "Arizona" . . .];
var substringMatcher1 = function (records) {
return function findMatches(searchString, callback) {
var matches, substringRegex;
matches = [];
substrRegex = new RegExp(searchString, 'i');
$.each(records, function (index, record) {
if (substrRegex.test(record)) {
matches.push(record);
}
});
callback(matches);
};
};
$('#field1').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'records',
source: substringMatcher1(records)
});
什么不起作用:
var substringMatcher2 = function (records) {
return function findMatches(searchString, callback) {
$.ajax({
url: "/Test/GetRecords/",
cache: false,
data: { searchString: searchString },
type: "POST",
success: function (data) {
callback(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
};
};
$('#field2').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'records2',
source: substringMatcher2()
});
Test/GetRecords根据searchString正确returns过滤记录(List of strings),但页面上没有任何显示。我调试并正确填充了数据。 (data = [New Jersey, New York, . . . ] when searchString is "new")
我错过了什么?这种情况可行吗?
任何帮助将不胜感激。
提前致谢!
这是使它对我有用的更新代码,以防它对其他人有所帮助:
var substringMatcher2 = function (records) {
return function findMatches(searchString, processSync, processAsync) {
$.ajax({
url: "/Test/GetRecords/",
cache: false,
data: { searchString: searchString },
type: "POST",
success: function (data) {
processAsync(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
};
};
我对 Typeahead 还很陌生,不能让它与回调一起工作。很抱歉,如果有人问过这个问题,我在搜索时找不到确切的问题。
如果记录只是来自带有字符串的变量,但当它来自数据库时,我让 Typeahead 工作。我不确定如何正确编写回调代码。
我们正在使用 MVC 6,看起来这是 typeahead.js 0.11.1。
什么有效:
var records = [ "Alabama", "Alaska", "Arizona" . . .];
var substringMatcher1 = function (records) {
return function findMatches(searchString, callback) {
var matches, substringRegex;
matches = [];
substrRegex = new RegExp(searchString, 'i');
$.each(records, function (index, record) {
if (substrRegex.test(record)) {
matches.push(record);
}
});
callback(matches);
};
};
$('#field1').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'records',
source: substringMatcher1(records)
});
什么不起作用:
var substringMatcher2 = function (records) {
return function findMatches(searchString, callback) {
$.ajax({
url: "/Test/GetRecords/",
cache: false,
data: { searchString: searchString },
type: "POST",
success: function (data) {
callback(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
};
};
$('#field2').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'records2',
source: substringMatcher2()
});
Test/GetRecords根据searchString正确returns过滤记录(List of strings),但页面上没有任何显示。我调试并正确填充了数据。 (data = [New Jersey, New York, . . . ] when searchString is "new")
我错过了什么?这种情况可行吗?
任何帮助将不胜感激。
提前致谢!
这是使它对我有用的更新代码,以防它对其他人有所帮助:
var substringMatcher2 = function (records) {
return function findMatches(searchString, processSync, processAsync) {
$.ajax({
url: "/Test/GetRecords/",
cache: false,
data: { searchString: searchString },
type: "POST",
success: function (data) {
processAsync(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
};
};