Reputation: 572
I am trying to implement inline editing on datatables in AngularJS. I was trying to find some example but i couldn't. There is a good example here, but it is not compatible with AngularJS. So, how can i implement it in AngularJS?
Related code from my project:
<tbody>
<tr ng-repeat="meal in meals">
<td>{{ meal.id }}</td>
<td>{{ meal.name }}</td>
<td>
<a ng-click="open_yemek('lg', meal.name, meal.recipe, meal.ingredients)">Tıklayın</a>
</td>
<td class="text-center">
<!--<small class="label label-warning" style="font-size: 9px !important;"><i class="fa fa-clock-o"></i> {{ yemek.sure }}</small>-->
{{ meal.time }}
</td>
<td>{{ meal.category }}</td>
<td>{{ meal.region }}</td>
<td>{{ meal.user }}</td>
<td class="text-center">{{ meal.rank }}/10</td>
<td>{{ meal.status }}</td>
<td>
<i ng-click="editItem($index)" class="fa fa-pencil-square-o" aria-hidden="true"></i>
<a ng-click="removeItem('yemekler',$index)"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
<a ng-click="addItem('yemekler')"><i aria-hidden="true" class="fa fa-plus"></i></a>
</td>
</tr>
</tbody>
This is the JS:
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('lengthMenu', [5, 10, 15])
.withOption('autoWidth', true);
$scope.meals = {};
mealFactory.get()
.success(function(data) {
console.log(data);
$scope.meals = data;
});
Upvotes: 0
Views: 4071
Reputation: 2947
My suggestion here is to use an ng-include with <input>
tags inside the td element.
This way, you will be able to have a "readonly" td and an editable one with a single click on it.
Your HTML code of your table should be something like this:
<tbody>
<tr ng-repeat="row in data track by $index">
<td> </td>
<td ng-click="makeEditable($index)" ng-include="{{row.isEditable ? 'editableInput.html' : 'readonly.html'}}"> </td>
<td> </td>
</tr>
</tbody>
With the use of some templates:
<script type="text/ng-template" id="editableInput.html">
<input type="text" ng-model="row.value"></input>
</script>
<script type="text/ng-template" id="readonly.html">
<span>{{row.value}}</span>
</script>
And in your controller, the click listener:
$scope.makeEditable = function($index){
//remove every previous editable input...
if($scope.lastDataEditable != null)
$scope.lastDataEditable = false;
$scope.data[$index].isEditable = true;
$scope.lastDataEditable = $scope.data[$index];
}
Beware: you haven't provided any form of code, and your question is a bit too general. I provided you a general idea of you should do.
In fact, you should also consider to use a directive for the same purpose: it all dipends of the complexity of your environment.
Upvotes: 1