【问题标题】:ServiceNow angularjs client/server script communicationServiceNow angularjs客户端/服务器脚本通信
【发布时间】:2017-10-04 21:20:53
【问题描述】:

我在 ServiceNow 中工作,正在创建一个小部件,该小部件显示案例列表以及打印选项。我想根据选择的案例和打印选项填充一个数组,但是在客户端和服务器脚本之间传递东西时遇到问题。下面是我的 HTML 和小部件外观的快照:

<div class="btn-group" role="group">  
    <button id="btnGroupDrop1" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">  
      Printing Options   <i class="fa fa-caret-down"/>   
      </button>  
      <div class="dropdown-menu" aria-labelledby="btnGroupDrop1" >  
      <li ng-repeat="print in c.docSetPrint">  
          <a class="dropdown-item" href="#">{{print.document_set_name}}</a>  
        </li>  
      </div>  
    </div>  

<table id="print_table" class="table table-striped table-hover table-responsive">  
    <thead>  
      <tr>  
        <th><input type="checkbox" id="selectAll"/></th>  
          <th>${Case Number}</th>  
          <th>${Short Description}</th>  
          <th>${Start Date}</th>  
          <th>${Work Location}</th>  
        </tr>  
      </thead>  
      <tbody>  
      <tr ng-repeat="item in c.ONCase track by $index">  
        <td><input type="checkbox" id="{{item.number}}"/></td>  
          <td>{{item.number}}</td>  
          <td>{{item.short_description}}</td>  
          <td>{{item.start_date}}</td>  
          <td>{{item.location}}</td>            
        </tr>  
      </tbody>  
    </table>

在上表中,我想遍历每个条目,如果它已被选中或选中,则返回一个包含所有选中案例编号的数组。在我的客户端脚本中,到目前为止,我有这样的东西:

c.printDocs = function(){
    var arr = [];

    for(i=0; i<c.data.ONCase.length; i++){
        if(document.getElementById(c.data.ONCase[i].number).checked == true){
            arr.push({
                case_num: c.dataONCase.number <--??
            });
        }
    }
    c.server.get({
        action: 'print_docs',
        cases: arr
    })then(function(response) {
        // do stuff after
    });
};

我对客户端和服务器脚本之间的脚本编写方式感到很困惑。一旦我得到了案例编号数组,我该如何将它传递给服务器?

【问题讨论】:

    标签: angularjs arrays servicenow


    【解决方案1】:

    在客户端脚本上,您可以将要发送到服务器脚本的数据放入c.data。然后在服务器脚本上,这在 input 对象中可用。

    客户端脚本

    function () {
        var c = this;
    
        c.myFunction = function () {
            // populate the c.data object
            c.data.cases= ['CASENUM01','CASENUM02','CASENUMO3'];
    
            // send the c.data object to the server
            c.server.update().then(function () {
                // do cleanup if needed
                c.data.cases = [];
            });
        }
    }
    

    服务器脚本

    (function() {
    
        // input here contains c.data from client script
        if (input && input.cases) {
            for (var i = 0; i < input.cases.length; i++) {
                // write to system log
                gs.info(input.cases[i]);
            } 
        }
    });
    

    https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/ 上有一个很好的教程。

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 2011-09-05
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多