JSFiddle - React, Tailwind, and code Playground
by Steve Wash
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="http://vitalets.github.io/angular-xeditable/dist/js/xeditable.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://vitalets.github.io/angular-xeditable/dist/css/xeditable.css">
<h4>Angular-xeditable Editable row (Bootstrap 3)</h4>
<div ng-app="app" ng-controller="Ctrl">
<div class="col-md-12" style="margin-bottom: 3px">
<div class="col-md-4 col-md-offset-1" style="padding-top: 6px; padding-left: 0px"><label>Equipment</label></div>
<div class="col-md-offset-10">
<button class="btn btn-primary btn-sm" ng-click="addEquipment()">Add row</button>
</div>
</div>
<table class="table table-bordered table-hover table-condensed">
<tr style="font-weight: bold; background-color: lightblue">
<td style="width:20%">Make</td>
<td style="width:20%">Model</td>
<td style="width:45%">Description</td>
<td style="width:15%">Edit</td>
</tr>
<tr ng-repeat="equip in equipment">
<td>
<span editable-select="equip.make" e-description="make" e-form="rowform" e-ng-options="s as s for s in makes">
{{ showMake(equip) }}
</span>
</td>
<td>
<span editable-select="equip.model" e-description="model" e-form="rowform" e-ng-options="g.id as g.name for g in models">
{{ showModel(equip) }}
</span>
</td>
<td>
<span editable-text="equip.description" e-description="description" e-form="rowform" onbeforesave="checkEquipment($data, equip.id)" e-required>
{{ equip.description || 'empty' }}
</span>
</td>
<td style="white-space: nowrap">
<form editable-form...
CSS
div[ng-app] { margin: 10px; }
.table {width: 100% }
form[editable-form] > div {margin: 10px 0;}
JavaScript
var app = angular.module("app", ["xeditable"]);
app.run(function (editableOptions) {
editableOptions.theme = 'bs3';
});
app.controller('Ctrl', function ($scope, $filter, $http) {
$scope.equipment = [
{id: 1, name: 'awesome user1', make: 'Chevy', model: { id: 1, make: 'Chevy', name: 'Impala' }},
{id: 2, name: 'awesome user2', make: 'Chevy', model: { id: 3, make: 'Chevy', name: 'Blazer' }},
{id: 3, name: 'awesome user3', make: 'Ford', model: { id: 2, make: 'Ford', name: 'Mustang' }}
];
$scope.makes = ['Chevy', 'Ford'];
$scope.models = [
{ id: 1, make: 'Chevy', name: 'Impala' },
{ id: 2, make: 'Ford', name: 'Mustang' },
{ id: 3, make: 'Chevy', name: 'Blazer' },
{ id: 4, make: 'Chevy', name: 'Camaro' }
];
$scope.showModel = function (equip) {
if (equip.model && $scope.models.length) {
return equip.model.name
} else {
return '';
}
};
$scope.showMake = function (equip) {
return equip.make;
};
$scope.checkEquipment = function (data, id) {
if (id === 2 && data !== 'awesome') {
return "Username 2 should be `awesome`";
}
};
// remove equip
$scope.removeEquipment = function (index) {
$scope.equipment.splice(index, 1);
};
// add equip
$scope.addEquipment = function () {
$scope.inserted = {
id: 0 - ($scope.equipment.length + 1),
description: '',
make: null,
model: null
};
$scope.equipment.push($scope.inserted);
};
$scope.saveEquipment = function (data, id) {
//$scope.user not updated yet
angular.extend(data, { id: id });
alert('saved');
};
});