You need to sign in to do that
Don't have an account?
Somu Somu
If check box is checked those records only update in lightning component
Hi
Here my requirement is display all account records and mass update the records here condition is if the record check box is checked (true)then only records need to be update with out selecting check box record does not update
(the code is updating the records with out selecting the check box but i need after selecting the checkbox then only update the records)
here my code is
apex:
public with sharing class massupdate
{
@AuraEnabled
public static list < Account > fetchAccount()
{
// query 10 records from account with their relevant contacts and return query.
List < Account > lstOfAcc = [select Name, AnnualRevenue, BillingState, Website,Rating,Phone from Account limit 10 ];
return lstOfAcc;
}
// method for update records after inline editing
@AuraEnabled
public static List < account > saveAccount(List<Account> lstAccount) {
update lstAccount;
return lstAccount;
}
}
-------------------------------------
component
<aura:component controller="massupdate">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="ListOfAccount" type="Account[]" description="store accounts with there child contacts"/>
<div>
<table class="table table-bordered table-hover" width="70%">
<thead>
<tr>
<th width="5%"> <strong> Select </strong> </th>
<th width="20%"> <strong> Id </strong> </th>
<th width="20%"> <strong> Name </strong> </th>
<th width="20%"><strong> Website </strong></th>
<th width="20%"><strong> Phone </strong></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.ListOfAccount}" var="ac">
<tr>
<td><ui:inputCheckbox text="{!ac.Id}" aura:id="boxPack" value="" /></td>
<td> <a href="{! '/'+ac.Id}"> {!ac.Id} </a> </td>
<td><ui:inputText value="{!ac.Name}"/> </td>
<td><ui:inputText value="{!ac.Website}"/> </td>
<td><ui:inputText value="{!ac.Phone}"/> </td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<br/>
<lightning:button label="Save" onclick="{!c.Save}" variant="success"/>
<lightning:button label="Cancel" onclick="{!c.Cancel}" variant="success"/>
</aura:component>
-----------------------------------------------
controller
({
doInit: function(component, event, helper) {
//call apex class method
var action = component.get('c.fetchAccount');
action.setCallback(this, function(response)
{
//store state of response
var state = response.getState();
if (state === "SUCCESS") {
//set response value in ListOfAccount attribute on component.
component.set('v.ListOfAccount', response.getReturnValue());
}
});
$A.enqueueAction(action);
},
Save: function(component, event, helper)
{
var newAcc = component.get("v.ListOfAccount");
var action = component.get("c.saveAccount");
action.setParams({"lstAccount": newAcc });
action.setCallback(this, function(a) {
var state = a.getState();
if (state === "SUCCESS")
{
var name = a.getReturnValue();
component.set("v.ListOfAccount", name);
alert('Updated...');
location.reload();
}
})
$A.enqueueAction(action);
},
Cancel: function(component, event, helper)
{
alert('Clicked on cancled ...');
location.reload();
}
})
Here my requirement is display all account records and mass update the records here condition is if the record check box is checked (true)then only records need to be update with out selecting check box record does not update
(the code is updating the records with out selecting the check box but i need after selecting the checkbox then only update the records)
here my code is
apex:
public with sharing class massupdate
{
@AuraEnabled
public static list < Account > fetchAccount()
{
// query 10 records from account with their relevant contacts and return query.
List < Account > lstOfAcc = [select Name, AnnualRevenue, BillingState, Website,Rating,Phone from Account limit 10 ];
return lstOfAcc;
}
// method for update records after inline editing
@AuraEnabled
public static List < account > saveAccount(List<Account> lstAccount) {
update lstAccount;
return lstAccount;
}
}
-------------------------------------
component
<aura:component controller="massupdate">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="ListOfAccount" type="Account[]" description="store accounts with there child contacts"/>
<div>
<table class="table table-bordered table-hover" width="70%">
<thead>
<tr>
<th width="5%"> <strong> Select </strong> </th>
<th width="20%"> <strong> Id </strong> </th>
<th width="20%"> <strong> Name </strong> </th>
<th width="20%"><strong> Website </strong></th>
<th width="20%"><strong> Phone </strong></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.ListOfAccount}" var="ac">
<tr>
<td><ui:inputCheckbox text="{!ac.Id}" aura:id="boxPack" value="" /></td>
<td> <a href="{! '/'+ac.Id}"> {!ac.Id} </a> </td>
<td><ui:inputText value="{!ac.Name}"/> </td>
<td><ui:inputText value="{!ac.Website}"/> </td>
<td><ui:inputText value="{!ac.Phone}"/> </td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<br/>
<lightning:button label="Save" onclick="{!c.Save}" variant="success"/>
<lightning:button label="Cancel" onclick="{!c.Cancel}" variant="success"/>
</aura:component>
-----------------------------------------------
controller
({
doInit: function(component, event, helper) {
//call apex class method
var action = component.get('c.fetchAccount');
action.setCallback(this, function(response)
{
//store state of response
var state = response.getState();
if (state === "SUCCESS") {
//set response value in ListOfAccount attribute on component.
component.set('v.ListOfAccount', response.getReturnValue());
}
});
$A.enqueueAction(action);
},
Save: function(component, event, helper)
{
var newAcc = component.get("v.ListOfAccount");
var action = component.get("c.saveAccount");
action.setParams({"lstAccount": newAcc });
action.setCallback(this, function(a) {
var state = a.getState();
if (state === "SUCCESS")
{
var name = a.getReturnValue();
component.set("v.ListOfAccount", name);
alert('Updated...');
location.reload();
}
})
$A.enqueueAction(action);
},
Cancel: function(component, event, helper)
{
alert('Clicked on cancled ...');
location.reload();
}
})
Try below code:-
create one Checkbox field on Account object, here i have created pNish__isUpdate__c (pNish is my Namespace)
1) Apex Controller**********
public with sharing class massupdate
{
@AuraEnabled
public static list < Account > fetchAccount()
{
// query 10 records from account with their relevant contacts and return query.
List < Account > lstOfAcc = [select Name,pNish__isUpdate__c, AnnualRevenue, BillingState, Website,Rating,Phone from Account limit 10 ];
return lstOfAcc;
}
// method for update records after inline editing
@AuraEnabled
public static List < account > saveAccount(List<Account> lstAccount) {
List<Account> accList = new List<Account>();
List<Account> accListTemp = new List<Account>();
for(Account acc : lstAccount){
if(acc.pNish__isUpdate__c){ // added extra check - if pNish__isUpdate__c is checked then only add to accList
acc.pNish__isUpdate__c = false; // and make pNish__isUpdate__c false so that it will not show checked on application
accList.add(acc);
}
}
if(accList != null && accList.size() > 0) // Null checking
update accList; // update only pNish__isUpdate__c checked list
return lstAccount;
}
}
2) Component**********
<aura:component controller="pNish.massupdate">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="ListOfAccount" type="Account[]" description="store accounts with there child contacts"/>
<div>
<table class="table table-bordered table-hover" width="70%">
<thead>
<tr>
<th width="5%"> <strong> Select </strong> </th>
<th width="20%"> <strong> Id </strong> </th>
<th width="20%"> <strong> Name </strong> </th>
<th width="20%"><strong> Website </strong></th>
<th width="20%"><strong> Phone </strong></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.ListOfAccount}" var="ac">
<tr>
<td><ui:inputCheckbox text="{!ac.pNish__isUpdate__c}" aura:id="boxPack" value="{!ac.pNish__isUpdate__c}" /></td>
<td> <a href="{! '/'+ac.Id}"> {!ac.Id} </a> </td>
<td><ui:inputText value="{!ac.Name}"/> </td>
<td><ui:inputText value="{!ac.Website}"/> </td>
<td><ui:inputText value="{!ac.Phone}"/> </td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<br/>
<lightning:button label="Save" onclick="{!c.Save}" variant="success"/>
<lightning:button label="Cancel" onclick="{!c.Cancel}" variant="success"/>
</aura:component>
3) Client-side Controller ********** (No change)
({
doInit: function(component, event, helper) {
//call apex class method
var action = component.get('c.fetchAccount');
action.setCallback(this, function(response)
{
//store state of response
var state = response.getState();
if (state === "SUCCESS") {
//set response value in ListOfAccount attribute on component.
component.set('v.ListOfAccount', response.getReturnValue());
}
});
$A.enqueueAction(action);
},
Save: function(component, event, helper)
{
var newAcc = component.get("v.ListOfAccount");
console.log('newAcc Length------'+newAcc.length);
var action = component.get("c.saveAccount");
action.setParams({"lstAccount": newAcc });
action.setCallback(this, function(a) {
var state = a.getState();
if (state === "SUCCESS")
{
var name = a.getReturnValue();
component.set("v.ListOfAccount", name);
alert('Updated...');
location.reload();
}
})
$A.enqueueAction(action);
},
Cancel: function(component, event, helper)
{
alert('Clicked on cancled ...');
location.reload();
}
})
Note:- pNish namespace.
Let me know in case of any concerns.
Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.
Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
i had some changes with u r code for my requirement