jQuery ui 使用不同的数据源自动完成
jQuery ui autocomplete with different data sources
我有一个表单,其中有多个字段需要自动完成,我正在使用 "multiple" 选项,这样每个字段都可以选择多个值。我可以使用单个数据源使一切正常工作。
问题来了:每个字段需要有一个不同的远程数据源(php文件输出JSON数组)。我想使用自定义 data-attribute
来列出 JSON 数据源 url。
这是我输入的内容之一:
<input type="text" name="frr" id="frr" data-searchsource="searchFrr.php" class="autofillme">
这是我的 jQuery - 我没有收到任何控制台错误,但它不起作用(自动完成根本不再起作用)。如果我在 "searchFrr.php"
中针对 $.getJSON
请求进行硬编码,则一切正常(使用该单一数据源)。
关于如何使其正常工作的任何想法?非常感谢您的关注!! :)
$( function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$('.autofillme').each(function() {
$(this)
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( $(this).data("searchsource"), {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 3 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
});
最终,我无法通过我尝试的方法实现我想要的。我相信在 jQuery 方面更有成就的人应该能够弄明白。
不过我确实找到了一个解决方案,它允许我根据输入的字段使用不同的数据源。这是使用 jQuery UI 自动完成和远程数据源,和 "multiple" 选项,因此可以为每个字段选择多个值。我在我想要自动完成的表单中的每个输入中添加了 class of autofillme
。
JAVASCRIPT
$( function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$('.autofillme').each(function() {
$(this)
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "searchDB.php", {
term: extractLast( request.term ),
// adds an extra query string to the URL, sending the ID of the field sending the request
field: $(this).attr('element').attr('id')
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 3 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
});
PHP - searchDB.php
require "config.php"; // DB credentials stored here
$term = $_GET['term']; // What's been typed so far in the field
$field = $_GET['field']; // The ID of the field that's sending the request
try {
$connection = new PDO($DB, $username, $password, $options);
if ($field == 'field_A') {
$array = $connection->query("SELECT * FROM columnA where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
elseif ($field == 'field_B') {
$array = $connection->query("SELECT * FROM columnB where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
elseif ($field == 'field_C') {
$array = $connection->query("SELECT * FROM columnC where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
else {
// whatever happens if no field sent
}
}
catch(PDOException $error) {
echo "A problem has occurred fetching data";
}
最终这可能不是一个出色的解决方案,或者可扩展性特别差,但它完全符合我的要求 - 所以它解决了我的问题。希望它对正在做类似事情的人有用。
我有一个表单,其中有多个字段需要自动完成,我正在使用 "multiple" 选项,这样每个字段都可以选择多个值。我可以使用单个数据源使一切正常工作。
问题来了:每个字段需要有一个不同的远程数据源(php文件输出JSON数组)。我想使用自定义 data-attribute
来列出 JSON 数据源 url。
这是我输入的内容之一:
<input type="text" name="frr" id="frr" data-searchsource="searchFrr.php" class="autofillme">
这是我的 jQuery - 我没有收到任何控制台错误,但它不起作用(自动完成根本不再起作用)。如果我在 "searchFrr.php"
中针对 $.getJSON
请求进行硬编码,则一切正常(使用该单一数据源)。
关于如何使其正常工作的任何想法?非常感谢您的关注!! :)
$( function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$('.autofillme').each(function() {
$(this)
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( $(this).data("searchsource"), {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 3 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
});
最终,我无法通过我尝试的方法实现我想要的。我相信在 jQuery 方面更有成就的人应该能够弄明白。
不过我确实找到了一个解决方案,它允许我根据输入的字段使用不同的数据源。这是使用 jQuery UI 自动完成和远程数据源,和 "multiple" 选项,因此可以为每个字段选择多个值。我在我想要自动完成的表单中的每个输入中添加了 class of autofillme
。
JAVASCRIPT
$( function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$('.autofillme').each(function() {
$(this)
// don't navigate away from the field on tab when selecting an item
.on( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "searchDB.php", {
term: extractLast( request.term ),
// adds an extra query string to the URL, sending the ID of the field sending the request
field: $(this).attr('element').attr('id')
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 3 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
});
PHP - searchDB.php
require "config.php"; // DB credentials stored here
$term = $_GET['term']; // What's been typed so far in the field
$field = $_GET['field']; // The ID of the field that's sending the request
try {
$connection = new PDO($DB, $username, $password, $options);
if ($field == 'field_A') {
$array = $connection->query("SELECT * FROM columnA where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
elseif ($field == 'field_B') {
$array = $connection->query("SELECT * FROM columnB where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
elseif ($field == 'field_C') {
$array = $connection->query("SELECT * FROM columnC where value LIKE '%".$term."%'")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);
}
else {
// whatever happens if no field sent
}
}
catch(PDOException $error) {
echo "A problem has occurred fetching data";
}
最终这可能不是一个出色的解决方案,或者可扩展性特别差,但它完全符合我的要求 - 所以它解决了我的问题。希望它对正在做类似事情的人有用。