首页 文章

查询为小组件数据源

提问于
浏览
1

我正在尝试将小部件的数据源定义为查询的结果,但我不确定它是否可行 .

我正在使用SQL视图和一个表,我想显示我在表中来自视图的ID的值 .

function queryValue(source, model, key){
  console.log("source " + source);
  app.datasources[model].query.filters.id._equals = source;
  app.datasources[model].load(function () {
    console.log(app.datasources.users.items[0][key]);
    return app.datasources.users.items[0][key];
  });
  app.datasources[model].query.clearFilters();
}

把它称为:

queryValue(@datasource.item.[the_id], "[the_SQLView_Datasouce]", "[the_field_i_want]");

在这种情况下,窗口小部件是一个表,因此功能将重复talbe中的项目数量

问题是,我得到的结果与meny时间相同,因为项目数量或第一个不起作用!

第二个问题是结果不会覆盖要显示的小部件文本 .
enter image description here

这是一个非常简单的功能,我确实找到了一些解决方法,但没有数据源功能,它们的工作太慢,任何消耗?是否有可能用数据源做这样的事情?

2 回答

  • 2

    如果我正确理解了这个问题,你可能想在服务器端进行查询 . 发布的示例代码的问题是它在任何负载可以返回之前多次触发单个数据源的负载 . 完成后,数据源只加载其中一个加载的结果,我相信最后一个 . 所以你可能会看到你为所有回调做的上一个查询的结果 .

    所以相反,你的代码应该是服务器端脚本,应该是这样的:

    function queryValue(source, model, key){
      console.log("source " + source);
      var query = app.models.newQuery();
      query.filters.id._equals = source;
      var results = query.run;
      return results[0].key;
    }
    

    (写于记忆中,所以请原谅任何错误 . )

  • 2

    遵循德文的建议:

    前端

    /*****************************************************************************
    Front-end function that calls the querying function @queryValue(source, model, key) in controller_TransformId
    @source => the field ID to transform to label
    @model => the model name to be queried
    @key => the label to be acquired with the query
    @wwidget => the widget making the request
    This function works as a model to manage the transactions between the 
    controller at the backend and the view.  
    ******************************************************************************/
    function buildTransformID(source, model, key, widget){ 
      google.script.run.withSuccessHandler(
        function successHandler(expectedValue){
          widget.text = expectedValue;})
      .withFailureHandler(
        function failureHandler(){
          widget.text = "undefined";})
      .queryValue(source, model, key);
    }
    

    后端

    /*****************************************************************************
    Back-end function that queries the database
    @source => the field ID to transform to label
    @model => the model name to be queried
    @key => the label to be acquired with the query    
    This function works works as a controller to query the database from the backend    ******************************************************************************/
    function queryValue(source, model, key){ 
      var query = app.models[model].newQuery();
      query.filters.id._equals = source;
      var results = query.run();
      console.log("CONTROLLER return :" + results[0][key]);
      return results[0][key];
    }
    

    是否必须通过widget.text值? successHandler回调是异步的,因此常规返回只会给我空值

相关问题