I have a helper function on my lightning component's helper component named 'upsertAccount'. Here is the code snippet of 'upsertAccount' method.
upsertAccount : function(cmp, newAccount, callback)
{
var action = cmp.get("c.upsertAccount");
action.setParams({
"account": newAccount
});
action.setCallback(this, function(response){
var state = response.getState();
if (cmp.isValid() && state === "SUCCESS")
{
var oldAcc = cmp.get("v.accountList");
oldAcc.push(response.getReturnValue());
cmp.set("v.accountList", oldAcc);
}
else
{
// do nothing...
}
});
$A.enqueueAction(action);
}
View attribute 'v.accountList' is initialised in doInit() method. And 'newAccount' is the newly created account.
This method works good and add the newly created account as the last element to the attribute 'v.accountList'. Instead, I want the newly created account to be added as the first element.I tried below code, but it doesn't work.
I would to understand how to set the new accout as the first element to the list.upsertAccount : function(cmp, newAccount, callback)
{
var action = cmp.get("c.upsertAccount");
action.setParams({
"account": newAccount
});
action.setCallback(this, function(response){
var state = response.getState();
if (cmp.isValid() && state === "SUCCESS")
{
var newAcc = response.getReturnValue()cmp.get("v.accountList");
newAcc.push(cmp.get("v.accountList"));
cmp.set("v.accountList", newAcc);
}
else
{
// do nothing...
}
});
$A.enqueueAction(action);
}
Try using unshift in place of push method.Ex: newAcc.unshift(cmp.get("v.accountList"));