2017-10-04 62 views
0

我在ServiceNow中工作,并且正在创建一个小部件,它显示案例列表以及打印选项。我想根据所选案例和打印选项来填充数组,但是在客户端和服务器脚本之间传递事物时遇到问题。下面是我的HTML和什么样的小部件看起来像快照:ServiceNow angularjs客户端/服务器脚本通信

<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> 

enter image description here

在上表中,我想通过每个条目循环,如果它已被选中或托运,然后返回选择所有案例编号的数组。在我的客户端脚本,我有这样的事情至今:

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 
    }); 
}; 

我在很困惑如何客户端和服务器脚本之间的脚本。一旦我得到了一系列案例号,我怎么才能将它传递给服务器呢?

回答

0

在客户端脚本上,您可以将您的数据发送到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/