[英]Populate Jquery Autocomplete With Images From Google Sheets
我使用以下代碼從 Google 電子表格填充 jQuery 自動完成列表以顯示用戶名建議:
自動完成.html
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
<script>
// This code in this function runs when the page is loaded.
$(function() {
google.script.run.withSuccessHandler(buildTagList)
.getAvailableTags();
});
function buildTagList(availableTags) {
$( "#tags" ).autocomplete({
source: availableTags
});
}
</script>
getAvailableTags()
function getAvailableTags() {
var ss = SpreadsheetApp.openById("0Avt7ejriwlxudGZfV2xJUGJZLXktm2RhQU1uRUgtaXc");
var s = ss.getSheetByName("Options");
var data = s.getDataRange().getValues();
var headers = 1; // number of header rows to skip at top
var tagColumn = 0; // column # (0-based) containing tag
var availableTags = [];
for (var row=headers; row < data.length; row++) {
availableTags.push(data[row][tagColumn]);
}
return( availableTags );
}
在我的 Google 電子表格中,我還有一個包含圖像 URL 的列,我希望在每個自動完成建議中顯示這些圖像 URL 。 這是一個片段,其中包含我要實現的目標的示例。 有沒有辦法修改這兩個代碼以確保從同一個 Google 電子表格中提取圖像 URL ?
$(function() { $(".search").autocomplete({ source: //"autocomplete.php", [ {id:"John Doe", value:"John Doe", label:"John Doe", img:"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQGIx0cvucwTL1pstrlX_m5eIurp-bFAVnvBQ&usqp=CAU"}, {id:"system Admin", value:"system Admin", label:"system Admin", img:"http://www.ericom.com/imgs/braftonArticles/3-things-every-system-admin-should-know-about-virtualization_16001411_800906167_0_14057272_500.jpg"} ], minLength: 1, select: function(event, ui) { /* var url = ui.item.id; if(url.= '') { location.href = '..;' + url, } */ }: html, true: open, function(event. ui) { $(".ui-autocomplete"),css("z-index"; 1000). } }).autocomplete( "instance" ),_renderItem = function( ul. item ) { return $( "<li><div><img src='"+item.img+"'><span>"+item.value+"</span></div></li>" );appendTo( ul ); }; });
.ui-menu img{ width:30px; height:30px; margin:0 5px 0 0px; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <input type="text" class="search"><br>
我相信你的目標如下。
autocomplete
一起使用。
autocomplete
一起使用。
[{id: value, value: value, label: value, img: url},,,]
。當以上幾點反映到你的腳本中時,它變成如下。
function buildTagList(availableTags) { $( "#tags" ).autocomplete({ source: availableTags }); }
到:
function buildTagList(availableTags) { $( "#tags" ).autocomplete({ source: availableTags }).autocomplete( "instance" )._renderItem = function( ul, item ) { return $( "<li><div><img src='"+item.img+"'><span>"+item.value+"</span></div></li>" ).appendTo( ul ); }; }
for (var row=headers; row < data.length; row++) { availableTags.push(data[row][tagColumn]); }
到:
for (var row=headers; row < data.length; row++) { var value = data[row][tagColumn]; var url = data[row][tagColumn + 1]; // In this modification, it supposes that URLs are put in the column "B". availableTags.push({id: value, value: value, label: value, img: url}); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.