[jQuery] autocomplete
in JavaScript
autocomplete
1. JS νμΌ λ‘λ
<!-- νμν CSS, JS λ‘λ -->
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
2.기본 ꡬ쑰
var auto_source = [ 'apple', 'banana', 'chocolate' ];
$('#autoInputTag').autocomplete({
source: auto_source
});
3.μ μ©ν μμ±
- source / minLength / autofocus / select
var auto_source = [ 'apple', 'banana', 'chocolate' ];
$('#autoInputTag').autocomplete({
source: auto_source,
minLength: 2,
autoFocus: true,
select: function (event, ui) { }
});
source | Array/String/Function | autocompleteμ μ¬μ©ν μμ€ λ°μ΄ν° |
---|---|---|
minLength | Integer | κ²μκΈ°λ₯μ νμ±νν μ΅μ κΈμ κ°μ κ°μ΄ 2λ©΄, 2κΈμλ₯Ό μ λ ₯ν΄μΌ κ²μλλ€ |
autoFocus | boolean | μμ€ λ°μ΄ν°μμ κ²μνμ λ 맨 μ λ°μ΄ν°μ μλ ν¬μ»€μ€ |
select | Function | autocompleteμμ μ¬μ©μκ° λ°μ΄ν°λ₯Ό μ ννμ λ μ΄λ²€νΈ |
4.λ°©λ²1
λͺ©ν: μμ Select λ°μ€μμ μ νν νλͺ©μ λ°λΌ λ€λ₯Έ μμ€ λ°μ΄ν° λ°μΈλ©νκΈ°
var data = {
'bird': [ 'fly', 'sit', 'tweet', 'sing', 'eat worm' ],
'dog': [ 'bark', 'run', 'follow', 'sleep' ]
}
$('#action').autocomplete({
source : data['bird']
});
$('#animal').change(function() {
var animal = $(this).val();
$('#action').autocomplete('option', 'source', data[animal]);
});
5.λ°©λ² 2
λͺ©ν: inputμ κ²μνλ ν€μλμ μ€μ κ²μ μλ£νμ λ inputμ 보μ¬μ€ κ° λΆλ¦¬νκΈ°
var data = {
'bird': [
{ label: 'Fly', value: 'fly' },
{ label: 'Sit', value: 'sit' },
{ label: 'Tweet', value: 'tweet' },
{ label: 'Sing', value: 'sing' },
{ label: 'Eat', value: 'eat worm' }
],
'dog': [
{ label: 'Bark', value: 'bark' },
{ label: 'Run', value: 'run' },
{ label: 'Follow', value: 'follow' },
{ label: 'Sleep', value: 'sleep' }
]
}
$('#action').autocomplete({
source : data['bird']
});
$('#animal').change(function() {
var animal = $(this).val();
$('#action').autocomplete('option', 'source', data[animal]);
});
6. λ°©λ² 3
λͺ©ν: κ²μμ΄ μμ±λλ νμ΄λ°μ νλ¨μ λ¬Έμ₯μΌλ‘ νμνκΈ° (select νμ©μ)
var data = {
'bird': [
{ label: 'Fly', value: 'fly', 'id': 0 },
{ label: 'Sit', value: 'sit', 'id': 1 },
{ label: 'Tweet', value: 'tweet', 'id': 2 },
{ label: 'Sing', value: 'sing', 'id': 3 },
{ label: 'Eat', value: 'eat worm', 'id': 4 }
],
'dog': [
{ label: 'Bark', value: 'bark', 'id': 5 },
{ label: 'Run', value: 'run', 'id': 6 },
{ label: 'Follow', value: 'follow', 'id': 7 },
{ label: 'Sleep', value: 'sleep', 'id': 8 }
]
}
$('#action').autocomplete({
source: function (request, response) {
var term = request.term; // search keyword
var filteredData = data['bird'].filter(x => (x.label.indexOf(term) >= 0));
response($.map(filteredData, function (item) {
return {
label: item.label,
value: item.value,
'data-id': item.id
}
}))
},
autoFocus: true,
select: function(event, ui) {
$('#sentence').text(`${ui.item['data-id']} ${ui.item.value}`);
}
});
$('#animal').change(function() {
var animal = $(this).val();
var source = data[animal];
$('#action').autocomplete('option', 'source', function (request, response) {
var term = request.term; // search keyword
var filteredData = source.filter(x => (x.label.indexOf(term) >= 0));
response($.map(filteredData, function (item) {
return {
label: item.label,
value: item.value,
'data-id': item.id
}
}))
});
});
7. λ°©λ² 4
idλ₯Ό κΈ°μ€μΌλ‘ autocomplete κΈ°λ₯μ μ μΈ ν΄μ€λλ€.
$("#testInput").autocomplete({
source : function(request, response) {
$.ajax({
url : "/get/test"
, type : "GET"
, data : {keyWord : $("#testInput").val()} // κ²μ ν€μλ
, success : function(data){ // μ±κ³΅
response(
$.map(data, function(item) {
return {
label : item.testNm //λͺ©λ‘μ νμλλ κ°
, value : item.testNm //μ ν μ inputμ°½μ νμλλ κ°
, idx : item.testIdx // db μΈλ±μ€λ₯Ό λ΄μμ μμ
};
})
); //response
}
,
error : function(){ //μ€ν¨
alert("ν΅μ μ μ€ν¨νμ΅λλ€.");
}
});
}
, minLength : 1
, autoFocus : false
, select : function(evt, ui) {
console.log("μ 체 data: " + JSON.stringify(ui));
console.log("db Index : " + ui.item.idx);
console.log("κ²μ λ°μ΄ν° : " + ui.item.value);
}
, focus : function(evt, ui) {
return false;
}
, close : function(evt) {
}
});
β» μ΄λ²€νΈ μ€λͺ
- source : input νλμ νμ΄νμ λμν©λλ€.
- minLength : μ‘°νλ₯Ό μν μ΅μ κΈμμμ λλ€.
- autoFocus : 첫λ²μ§Έ νλͺ© μλ ν¬μ»€μ€(κΈ°λ³Έκ° : false)
- select : κ²μ 리μ€νΈμμ ν΄λΉ νλ μ νμ select μ΄λ²€νΈκ° λ°μν©λλ€.
- focus : νκΈ μ€λ₯ λ°©μ§μ λλ€.
- close : 리μ€ν μ°½μ΄ λ«νλ λ°μνλ μ΄λ²€νΈ μ λλ€.