Search code examples
javascriptangularjshtml-tableangularjs-ng-repeatjavascript-objects

AngularJs - Populate the tables by checking header names


I have two objects one contains selected records and another contains selected fields. Now I need to populate the table dynamically by checking the column header name or value.

I referred Populate table with ng-repeat and check matching header data

Completed: I able to add the headers dynamically to the table/columns.

Working code is in Plunker

HTML

<table class="dataTable no-footer leadTable">
    <thead>
        <tr role="row">
            <th>
                <input type="checkbox" ng-model="selectedSObject[key]" class="select_tertiary_selectAll" />
            </th>
            <th ng-repeat="k in sFields[key]" ng-value="{{k.name}}" style="font-weight: 400; border-right: none; background-color: #0070d2; padding: 10px 18px;">
                <div> {{k.label}} </div>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr role="row" ng-class="odd" ng-repeat="record in value.filtered">
            <td>
                <input type="checkbox" ng-model="record.checked" />
            </td>
            <td ng-repeat="(key_1, value_1) in record" style="padding: 10px 18px;">
                <div> {{value_1}} </div>
            </td>
        </tr>
    </tbody>
</table>

JS

$scope.sFields = getSFields($scope.sObjectCSVData);
$scope.iterableRecords = getiterableRecords($scope.sObjectCSVData, allRecords)

function getiterableRecords(sCSVData, allRecords) {
  var sObjectRecords = {};
  if (allRecords) {
      Object.entries(sCSVData).forEach(function(key) {
          if (sCSVData[key[0]].filtered.length != 0) {
              if (sObjectRecords[key[0]]) {
                  sObjectRecords[key[0]].push(sCSVData[key[0]].filtered)
              } else {
                  sObjectRecords[key[0]] = []
                  sObjectRecords[key[0]].push(sCSVData[key[0]].filtered)
              }
          }
      });
  }
  return sObjectRecords
}

function getSFields(sCSVData) {
  var fields = {};
  Object.entries(sCSVData).forEach(function(key) {
      if (sCSVData[key[0]].filtered.length != 0) {
          for (var i = 0; i < sCSVData[key[0]].sObjectFields.length; i++) {
              if (fields[key[0]]) {
                  fields[key[0]].push(sCSVData[key[0]].sObjectFields[i])
              } else {
                  fields[key[0]] = []
                  fields[key[0]].push(sCSVData[key[0]].sObjectFields[i])
              }
          }
      }
  });
  return fields
}

enter image description here

Need to do: Now I have to populate the table with the respective values by checking the column name. I'm working in AngularJS 1.6.9


Solution

  • By adding small changes, Now I can able to populate the table,

    The code I followed is as below,

    <tbody>
     <tr role="row" ng-class="odd" ng-repeat="record in value[0]">
       <td>
        <input type="checkbox" ng-model="record.checked" />
       </td>
       <td ng-repeat="k in sFields[key]" style="padding: 10px 18px;">
         {{record [k.name]}} </div>
       </td>
     </tr>
    

    The working code is in the below mentioned plunker, Populate the table by checking the header name

    For ref.

    Populated Table