I have created a html table that user can add dynamic columns and rows. When you click on add column it adds a column and when you start typing in the first row it should create a new row.
Here is my jsfiddle: http://jsfiddle.net/ydevb1Lx/
There are some issues with add row here. When user types something I am creating a new row. But when the user types in second letter it again creates another row and so on. Also on backspace it again creates a new row. Not sure whats going on here.
Second thing which I am not able to find out is how can I validate the table so that only the ID column does not have any duplicate. Since I am using ng-change event how can I check whether there already exists the same ID in the table and show some kind of alert. Below is the relevant code:
HTML Table:
<table class="table table-bordered" ng-form="targetTableForm" ng-class="{submitted: targetTableSubmitted}">
<colgroup>
<col class="unique-id">
</colgroup>
<thead>
<tr>
<th class="unique-id">Name #</th>
<th contenteditable="true" ng-repeat="c in targetTable.columns" ng-model="c.label"></th>
<!--<th class="view-next-set"><a href="#">...</a></th>-->
<td class="center add-column"><a href ng-click="open()">+ Add Column</a></td>
</tr>
</thead>
<tbody>
<tr ng-repeat="r in targetTable.rows">
<td contenteditable="true" ng-model="r.tableId" ng-change="r.tableId? addNewRow(r.tableId, r): undefined"></td>
<td contenteditable="true" ng-repeat="column in targetTable.columns" ng-required="!$parent.$last"></td>
<!--<td class="blank" colspan="2"></td>-->
</tr>
</tbody>
</table>
Code used to create add row:
$scope.addNewRow = function(value, row) {
if (!value || value === "" || typeof value !== 'string') return;
$scope.targetTable.rows.push({});
row.id = $scope.targetTable.uniqueIdCounter++;
};
Below is how I initialize array:
var table = {
id: 0,
name: "table 1",
columns: [],
rows: [{}],
uniqueIdCounter: 1037
};
$scope.tables = [table];
$scope.targetTable = $scope.tables[0];