Refresh list widget data on click of refresh button

AbhishekJain19
Mega Guru

Hi,

1. I have created a list type custom widget which shows top 15 incidents created recently. It has a refresh button, once a user clicks on to it, the data should get refreshed(if any new incident is created that should come in the list).
2. how we can refresh the list if a new incident gets created at the backend(without clicking any button)



HTML
<div>
<div style="border: 1px solid black; width:163px;
height:30px; display:flex;
align-items: center;
background-color:grey; color:white;
padding:5px;">
Top 15 Incidents
</div>
<table border="1px solid black">
<tr ng-repeat="x in data.array">
<td style="height:30px; width:163px; padding:5px;">{{x.number}}</td>
</tr>
</table>

</div>



Server
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(15);
gr.query();
data.array = [];
while(gr.next()){
var out = {};
out.number = gr.getValue('number');
data.array.push(out);
}
})();

2 REPLIES 2

Markus Kraus
Kilo Sage

The refresh button can look like this:

<button class="btn action-btn" ng-click="c.server.update()">Update</button>

However if you want to automatically update, you should take a look on record watchers (spUtil::recordWatch😞

function (spUtil, $scope) {
  /* widget controller */
  var c =this;

  spUtil.recordWatch($scope, "incident", "active=true");
}

Before investing too much work into your custom widget, please also take a look at the out of the box Simple List widget.

siva krishna M2
Tera Guru

Hello @AbhishekJain19 

 

Use below client side controller code on your widget to show server updates with refresh rate of 2 seconds without clicking on any buttons.

 

var refresh = setInterval(timerun,2000)  // for every 2 seconds

function timerun()

{

c.server.update();

}

 

If it was helpful, Kindly click on "Helpful" and mark it as "Accepted solution"