I'm creating a table component in AngularJS but i'm having a bug that i can't solve.
The structure of my table is something like:
<table>
<thead>...</thead>
<tbody>
<tr ng-click="selectRow()" ng-repeat="$value in values track by $index" ng-click="selectRow($index,$value)>
<td><input type="checkbox" ng-model="$value.__checked" ng-click="selectRow($index,$value)"/></td>
</tr>
</tbody>
</table>
When i click on the row, it works fine, but when i click on the checkbox it doesn't work as expected, and fire the event twice. I thought that just using the stopPropagation() on selectRow
when it's fired by the checkbox would work, but it doesn't.
The function selectRow can work for multi and single seleciton.
function selectRow(ngRepeatIndex,ngRepeatValue){
var selectedValues = $scope.$parent.selectedValues;
cleanValueAndArrays(vm.checkAll,vm.checkAll);
if($attrs.onClick)vm.onClick({value: ngRepeatValue});
if(vm.config.selection == 'single'){
if(ngRepeatValue.__checked){
ngRepeatValue.__checked = false;
cleanArrays();
} else {
cleanValueAndArrays(vm.selectedIndexes.length > 0)
pushToArrays(ngRepeatValue,ngRepeatIndex);
ngRepeatValue.__checked = true;
}
} else {
ngRepeatValue.__checked = vm.selectedIndexes.filter(function(val){return val == ngRepeatIndex}).length < 1;
if((ngRepeatValue.__checked) || vm.selectedIndexes.length == 0 ){
pushToArrays(ngRepeatValue,ngRepeatIndex);
return 0;
}
var indexOfValueSelected;
selectedValues.forEach(function(val,indx){
if(angular.equals(val,ngRepeatValue)) indexOfValueSelected = indx;
})
$scope.$parent.selectedValues.splice(indexOfValueSelected, 1);
vm.selectedIndexes.splice(vm.selectedIndexes.indexOf(ngRepeatIndex),1);
}
}