使用TextBox和onKeyUp和onKeyDown即时搜索 [英] Instant search using TextBox and onKeyUp and onKeyDown

查看:109
本文介绍了使用TextBox和onKeyUp和onKeyDown即时搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Google Apps脚本web应用程序在您输入文本框时立即搜索电子表格。



我现在使用的实现如下所示。键入事件新的搜索与当前搜索字符串一起被激活我正在使用onKeyUp正在调用的函数的延迟,并且如果调用onKeyDown,我尝试杀死前面的函数调用,然后继续使用新的。搜索需要不同的时间才能完成,所以最后一个完成并不总是正确的。



我不知道如何在Google Apps脚本中解决这个任务。这可以通过使用类似的东西来实现,但是这些函数并不值得怀疑:

  var timer; 
function up() {
setTimeout(mySpreadsheetSearchFunction,500);
}
function down(){
clearTimeout(timer);
}



这是当前实现的代码无法正常工作:

  function up(){
var cache = CacheService.getPrivateCache();
var now = parseInt(cache.get('iterate'));
Utilities.sleep(500);
if(parseInt(cache.get('iterate'))!== parseInt(now)){
return;
} else {
search();
}
showInGui();


function down(){
var cache = CacheService.getPrivateCache();
cache.put('iterate',1 + parseInt(cache.get('iterate')));
}

CacheService对于这项工作可能是错误的工具,可能会是什么?这是实现类似这样的正确方法吗?使用HtmlService,你可以在纯HTML和JavaScript中实现这个功能。这使您能够加载电子表格数据一次,然后执行搜索客户端,性能应该更好。


I would like to make Google Apps Script webapp that search spreadsheet instantly as you type in the TextBox.

The implementation I"m using now is following. For each key up event new search is lauched with the current search string. I'm using delay on the function that onKeyUp is calling and if the onKeyDown is called I try to kill the previous function call and just go with the new. This works sometimes but the search takes different time to complete so the last one to finish is not allways the right one.

I don't know how to solve this task in Google Apps script. This can be implemented using something like this but these functions are not awailable:

var timer;
function up(){
  setTimeout(mySpreadsheetSearchFunction, 500);
}
function down(){
  clearTimeout (timer);
}

This is the code of the current implementation that is not working correctly:

function up(){
  var cache = CacheService.getPrivateCache();
  var now = parseInt(cache.get('iterate'));
  Utilities.sleep(500);
  if(parseInt(cache.get('iterate')) !== parseInt(now)){
    return;
  }else{
    search();
  }
  showInGui();
}

function down(){
  var cache = CacheService.getPrivateCache();
  cache.put('iterate', 1+parseInt(cache.get('iterate')));
}

The CacheService is probably wrong tool for this job, what could be beter? Is this the right way to implement something like this?

解决方案

Using the HtmlService you can instead implement this in pure HTML and JavaScript. This gives you the ability to load the spreadsheet data once, and then do the search client-side, where the performance should be better.

这篇关于使用TextBox和onKeyUp和onKeyDown即时搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆