Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UI -
- <aura:component controller="DatatableContrl">
- <aura:attribute name="selectedRows" type="List"/>
- <aura:attribute name="objectName" type="String"/>
- <aura:attribute name="fields" type="List" default=""/>
- <aura:attribute name="isEdit" type="Boolean"/>
- <aura:attribute name="rowsToUpd" type="List"/>
- <aura:attribute name="msg" type="String"/>
- <aura:attribute name="msgType" type="String"/>
- <aura:registerEvent name="inClsEnt" type="c:InputFormCloseEvent"/>
- <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
- <div class="demo-only" style="height: 640px;">
- <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
- <div class="slds-modal__container">
- <header class="slds-modal__header">
- <div class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse">
- <lightning:buttonIcon iconName="utility:close" variant="bare-inverse" alternativeText="close" onclick="{!c.onClose}"/>
- </div>
- <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Input Form</h2>
- </header>
- <aura:if isTrue="{!v.msg}">
- <c:Message msg="{!v.msg}" msgType="{!v.msgType}"/>
- </aura:if>
- <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
- <table class="slds-table slds-table_bordered slds-table_cell-buffer">
- <thead>
- <tr class="slds-text-title_caps">
- <aura:iteration items="{!v.fields}" var="fld">
- <th scope="col">
- <div class="slds-truncate" title="{!fld.label}">{!fld.label}</div>
- </th>
- </aura:iteration>
- </tr>
- </thead>
- <tbody>
- <aura:iteration items="{!v.selectedRows}" var="row" indexVar="index">
- <tr id="{!index}">
- <aura:iteration items="{!row.entries}" var="fldVal">
- <td data-label="{!fldVal.value}">
- <div class="slds-form-element">
- <div class="slds-form-element__control">
- <input type="text" id="{!fldVal.fieldName}" class="slds-input" placeholder="{!fldVal.type}" value="{!fldVal.value}"/>
- </div>
- </div>
- </td>
- </aura:iteration>
- </tr>
- </aura:iteration>
- </tbody>
- </table>
- </div>
- <footer class="slds-modal__footer">
- <button class="slds-button slds-button_neutral" disabled="{!!v.msg}" onclick="{!c.onClose}">Ok</button>
- <button class="slds-button slds-button_brand" onclick="{!c.onSave}">Save</button>
- </footer>
- </div>
- </section>
- <div class="slds-backdrop slds-backdrop_open"></div>
- </div>
- </aura:component>
- Controller Js -
- ({
- doInit : function(cmp, event, helper) {
- var records = cmp.get("v.selectedRows");
- var fields = cmp.get("v.fields");
- for(var i = 0; i < records.length; i++) {
- records[i].entries = [];
- for(var j = 0; j < fields.length; j++) {
- records[i].entries.push({
- label : fields[j].label,
- fieldName : fields[j].fieldName,
- type : fields[j].type,
- value : records[i][fields[j].fieldName],
- });
- }
- }
- console.log('records: '+JSON.stringify(records));
- cmp.set("v.selectedRows",records);
- },
- onClose : function(cmp, event, helper) {
- cmp.set("v.msg",'');
- cmp.set("v.msgType",'');
- cmp.set("v.isEdit",false);
- var compEvent = cmp.getEvent("inClsEnt");
- // Optional: set some data for the event (also known as event shape)
- // A parameter’s name must match the name attribute
- // of one of the event’s <aura:attribute> tags
- // compEvent.setParams({"myParam" : myValue });
- compEvent.fire();
- },
- onSave : function(cmp, event, helper) {
- //var input = document.getElementById("0").getElementsByTagName("td")[0].getElementsByClassName("slds-input")[0];
- //alert(input.type);
- debugger;
- var records = cmp.get("v.selectedRows");
- var recSize = records.length;
- var recs = [];
- for(var i=0;i< recSize; i++) {
- var tds = document.getElementById(i).getElementsByTagName("td");
- var json = {};
- json['Id'] = records[i].Id;
- json['sObjectType'] = cmp.get("v.objectName");
- for(var j=0;j<tds.length;j++) {
- var type = tds[j].getElementsByClassName("slds-input")[0].placeholder;
- //console.log(type);
- var val = tds[j].getElementsByClassName("slds-input")[0].value;
- json[tds[j].getElementsByClassName("slds-input")[0].id] = (type == 'double' || type == 'currency') ? parseInt(val) : val;
- }
- console.log('*** json:** '+JSON.stringify(json));
- recs.push(json);
- }
- //console.log('### recs: '+JSON.stringify(recs));
- cmp.set("v.rowsToUpd",recs);
- helper.handleOnSave(cmp, event);
- }
- })
- Helper Js -
- ({
- handleOnSave : function(cmp, event) {
- debugger;
- var records = cmp.get("v.rowsToUpd");
- console.log(JSON.stringify(records));
- var action = cmp.get("c.save");
- action.setParams({
- records : records
- });
- action.setCallback(this,function(response) {
- var state = response.getState();
- if(state == 'SUCCESS') {
- var res = response.getReturnValue();
- cmp.set("v.msg",res);
- if(res.startsWith('Error'))
- cmp.set("v.msgType",'error');
- else
- cmp.set("v.msgType",'success');
- }
- else {
- cmp.set("v.msg",'Error.');
- cmp.set("v.msgType",'error');
- }
- });
- $A.enqueueAction(action);
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement