The Now Platform® Washington DC release is live. Watch now!
04-10-2020 11:23 PM
Hi all,
I'm trying to create a very basic widget where the input field takes value and updates the DATA object every time the function is clicked. And then I display it using ng-repeat int he HTML view. But I ran across into an issue where ng-repeat is not showing the list of all items I entered, instead only showing the last one. Please look into the issue.
HTML:
<ul><li ng-repeat='x in c.data.items'>{{x}}</li></ul>
<input type='text' ng-model='c.data.message'/>
<button ng-click='c.additem()'>Add</button>
Client script:
function($scope) {
var c = this;
c.additem=function(){
c.server.update().then(function(response){
c.data.message='';
});
}}
Server script:
(function() {
data.items=[];
if(input)
{
data.items.push(input.message);
}
})();
I've tried uisng c.server.get() as well like the below one. It's still not working.
c.server.get({additem:c.data.message}).then(function(response){
c.data.message='';
c.data.items=response.data.items;});
Server:
if(input.additem)
data.items.push(input.additem);
Solved! Go to Solution.
04-11-2020 06:05 AM
I've found a way to make it work. You just need to add this line in the server script. What it does is it stores the previous value of data.items through input.items.
data.items=[];
if(input)
{
data.items=input.items;
data.items.push(input.message);
}
04-11-2020 12:18 AM
Hello Vinay,
Well, looking at your code, ng-repeat goes good with table tag.
Please look to the similar code that displays all the items,
HTML :
<table class="table table-bordered">
<thead>
<tr>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr >
<td><input type='text' ng-model="c.data.message"></td>
</tr>
<tr ng-repeat="m in c.data.dataFromFields track by $index">
<td>{{m.message}}</td>
</tr>
</tbody>
</table>
<button type="button" ng-click="c.add()" class="btn btn-warning">Add</button>
CLIENT SCRIPT:
function($scope) {
/* widget controller */
var c = this;
c.add = function(){
c.data.action = "add";
c.server.update().then(function(){
c.data.action=undefined;
//Clear the Boxes.
c.data.message = "";
});
}
}
SERVER SCRIPT:
(function() {
data.dataFromFields = [];
if(input){
data.dataFromFields = input.dataFromFields;
}
if(input.action=="add"){
var dataArray={};
dataArray.message = input.message;
data.dataFromFields.push(dataArray)
data.dataFromFields.forEach(function(item){
gs.addInfoMessage(item)
})
}
})();
I'll try to simply the code more if you want.
Please mark helpful if that served your requirement.
Regards,
Kush
04-11-2020 12:59 AM
Hi Vinay,
can you check below link if it helps
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
04-11-2020 06:05 AM
I've found a way to make it work. You just need to add this line in the server script. What it does is it stores the previous value of data.items through input.items.
data.items=[];
if(input)
{
data.items=input.items;
data.items.push(input.message);
}