Introduction

In this article, we will learn MVC using xeditable json data using for html element & tables, which are given below.

  • HTML Control
  • Table

Angular-xeditable:

This is angular directives allows you to create editable element. Such technique is Click to edit or edit to place. It is based on idea of x-editable.

Features

  • It doesn’t depend on any libraries
  • It support bootstrap CSS
  • We can include some extra control from Angular UI Bootstrap
  • It will support must of html5 controls (text, textarea, select, and check box….etc.)

Follow the steps we can use xeditable in Angular JS in MVC.

  • Create MVC project
  • Configure angular xeditable
  • Work with angular xeditable.

Create MVC Project

Open Visual studio 2015

MVC

Go to New menu. Click New & Project. Now, it will open New Project Window.

MVC

You can select ASP.NET Web Application on Framework 4.6. Enter the name of the project in Solution name textbox and click OK button.

MVC

One more Window should appear. Select MVC Template in this popup & click OK button. Now, it starts to play.

Configure Angular xeditable

We will download the xeditable plug in from

Open the _Layout.cshtml and refer the xeditable.js file from the downloaded folder to this view page.

 <link href=”~/Plugin/angular-xeditable/dist/css/xeditable.css” rel=”stylesheet” />
My files
Angular Module Code
  1. Add dependence “’xeditable’” keyword in angular module after refer the plug in files

    var XEdit = angular.module(‘XEdit’, [‘ui.router’,’xeditable’]);

    Angular Controller Code:

    Inject the “editableOptions”, “editableThemes” keyword, it will help to improve our UI level

    function htmlctrlController($scope, editableOptions, editableThemes, $filter, $http)Set theme in this application, like given below code

     

    editableOptions.theme = ‘bs3’;

    editableThemes.bs3.inputClass = ‘input-sm’;

    editableThemes.bs3.buttonsClass = ‘btn-sm’;

    editableThemes.bs3.submitTpl = ‘<button type=”submit” class=”btn btn-success”><span class=”fa fa-check”></span></button>’;

    editableThemes.bs3.cancelTpl = ‘<button type=”button” class=”btn btn-default” ng-click=”$form.$cancel()”>’

    ‘<span class=”fa fa-times text-muted”></span>’ +

    ‘</button>’;

    Work with Angular xeditable

HTML control

Open HTML page, make it design, using Editable attribute and bind the even null values also.

  1. <a href=“” editable-email=“xform.htmlctrl.email”>{{ xform.htmlctrl.email || ’empty’ }}</a>

Here, we can see some HTML basic control.

HTML code

  1. <a href=“” editable-tel=“xform.htmlctrl.tel” e-pattern=“\d{3}-\d{2}-\d{2}” e-title=“xxx-xx-xx”>{{ xform.htmlctrl.tel || ’empty’ }}</a>
  2. lt;a href=“” editable-number=“xform.htmlctrl.number” e-min=“18”>{{ xform.htmlctrl.number || ’empty’ }}</a>
  3. <a href=“” editable-color=“xform.htmlctrl.color”>{{ xform.htmlctrl.color || ’empty’ }}</a>
  4.   <a href=“” editable-date=“xform.htmlctrl.date”>{{ xform.htmlctrl.date || ’empty’ }}</a>
  5.    <a href=“” editable-text=“xform.htmlctrl.name”>{{ xform.htmlctrl.name || ’empty’ }}</a>
  6.     <a href=“” editable-textarea=“xform.htmlctrl.desc” e-rows=“3” e-cols=“30”>{{ xform.htmlctrl.desc || ‘no description’ }}    </a>
  7. <a href=“” editable-select=“xform.htmlctrl3.text” ng-init=“xform.loadGroups()” e-ng-options=“g.text as g.text for g in xform.groups”>{{ xform.htmlctrl3.text || ‘not set’ }}  </a>
  8. t;a href=“” editable-textarea=“xform.htmlctrl.desc” e-rows=“3” e-cols=“30”>{{ xform.htmlctrl.desc || ‘no description’ }}    </a>

Writing Angular control & assign some values for the model. Usually, we have use the ng-Model keyword for data binding but in this case, we want to use editable+ControlName.

Editable keyword Angular/HTML keyword Descriptions
Editable-controlname Ng-Model Binding the data
e-ng-options Ng-options Binding the value as list
e-pattern Ng-pattern Using Validation
e-title Title Set as placeholder
e-min Min Set minimum values

JS code

  1. vm.htmlctrl = {
  2.         email: ’email@example.com’,
  3.         tel: ‘123-45-67’,
  4.         number: 29,
  5.         range: 10,
  6.         url: http://example.com&#8217;,
  7.         search: ‘blabla’,
  8.         date:new Date(),
  9.         color: ‘#6a4415’,
  10.         desc: ‘Click and edit the details. ‘
  11.     };
  12.     vm.htmlctrl2 = {
  13.         status: 2
  14.     };
  15.     vm.statuses = [
  16.       { value: 1, text: ‘status1’ },
  17.       { value: 2, text: ‘status2’ },
  18.       { value: 3, text: ‘status3’ },
  19.       { value: 4, text: ‘status4’ }
  20.     ];
  21.     vm.showStatus = function () {
  22.         var selected = $filter(‘filter’)(vm.statuses, { value: vm.htmlctrl2.status });
  23.         return (vm.htmlctrl.status && selected.length) ? selected[0].text : ‘Not set’;
  24.     };
  25.     // select remote
  26.     // ———————————– 
  27.     vm.htmlctrl3 = {
  28.         id: 4,
  29.         text: ‘admin’
  30.     };
  31.     vm.groups = [];
  32.     vm.loadGroups = function () {
  33.         return vm.groups.length ? null : $http.get(‘server/xeditable-groups.json’).success(function (data) {
  34.             vm.groups = data;
  35.         });
  36.     };
  37.     $scope.$watch(‘htmlctrl3.id’function (newVal, oldVal) {
  38.         if (newVal !== oldVal) {
  39.             var selected = $filter(‘filter’)(vm.groups, { id: vm.htmlctrl3.id });
  40.             vm.htmlctrl3.text = selected.length ? selected[0].text : null;
  41.         }
  42.     });

I have used as a JSON data for editable-Select & the files are given below.

  1. [{
  2.   “id”: 1,
  3.   “text”“user”
  4. }, {
  5.   “id”: 2,
  6.   “text”“customer”
  7. }, {
  8.   “id”: 3,
  9.   “text”“vip”
  10. }, {
  11.   “id”: 4,
  12.   “text”“admin”
  13. }]

Proceed, as shown below.

  1. <a href=“” editable-select=“xform.htmlctrl3.text”
  2. ng-init=“xform.loadGroups()”
  3. e-ng-options=“g.text as g.text for g in xform.groups”>
  4. {{ xform.htmlctrl3.text || ‘not set’ }}
  5. </a>”

Run the Application. In the output 1, we have archived the binding data to HTML & proceed.

Output 1

MVC

If you have any doubt for on configuration, refer to the links

Table

We can you use xeditable and proceed

Editable Row

This option is mostly useful for all of us, since end users find it easy to interact this way.

HTML code

  1. <table class=“table table-bordered table-hover bg-white”>
  2.                    <tr style=“font-weight: bold”>
  3.                        <td style=“width:35%”>Name</td>
  4.                        <td style=“width:20%”>Status</td>
  5.                        <td style=“width:20%”>Group</td>
  6.                        <td style=“width:25%”>Edit</td>
  7.                    </tr>
  8.                    <tr ng-repeat=“user in xtable.users”>
  9.                        <td>
  10.                            <!– editable username (text with validation)–>
  11.                            <span editable-text=“user.name” e-name=“name” e-form=“rowform” onbeforesave=“xtable.checkName($data, user.id)” e-required=“”>{{ user.name || ’empty’ }}</span>
  12.                        </td>
  13.                        <td>
  14.                            <!– editable status (select-local)–>
  15.                            <span editable-select=“user.status” e-name=“status” e-form=“rowform” e-ng-options=“s.value as s.text for s in xtable.statuses”>{{ xtable.showStatus(user) }}</span>
  16.                        </td>
  17.                        <td>
  18.                            <!– editable group (select-remote)–>
  19.                            <span editable-select=“user.group” e-name=“group” onshow=“xtable.loadGroups()” e-form=“rowform” e-ng-options=“g.id as g.text for g in xtable.groups”>{{ xtable.showGroup(user) }}</span>
  20.                        </td>
  21.                        <td style=“white-space: nowrap”>
  22.                            <!– form–>
  23.                            <form editable-form=“” name=“rowform” onbeforesave=“xtable.saveUser($data, user.id)” ng-show=“rowform.$visible” shown=“xtable.inserted == user” class=“form-buttons form-inline”>
  24.                                <button type=“submit” ng-disabled=“rowform.$waiting” title=“Save” class=“btn btn-sm btn-info”>
  25.                                    Save
  26.                                </button>
  27.                                <button type=“button” ng-disabled=“rowform.$waiting” ng-click=“rowform.$cancel()” title=“Cancel” class=“btn btn-sm btn-default”>
  28.                                    Cancel
  29.                                </button>
  30.                            </form>
  31. “!rowform.$visible” class=“buttons”>
  32.                                “rowform.$show()” title=“Edit” class=“btn btn-sm btn-info”>
  33.                                    Edit
  34.                                “xtable.removeUser($index)” title=“Delete” class=“btn btn-sm btn-danger”>
  35.                                    Delete
  •                        </td>
  •                    </tr>
  •                </table>
  •                <button ng-click=“xtable.addUser()” class=“btn btn-default”>Add row</button>

Angular Controller code

  1. vm.users = [
  2.       { id: 1, name: ‘ user1’, status: 2, group: 4, groupName: ‘admin’ },
  3.       { id: 2, name: ‘ user2’, status: undefined, group: 3, groupName: ‘vip’ },
  4.       { id: 3, name: ‘ user3’, status: 2, group: null }
  5.     ];
  6.     vm.statuses = [
  7.       { value: 1, text: ‘status1’ },
  8.       { value: 2, text: ‘status2’ },
  9.       { value: 3, text: ‘status3’ },
  10.       { value: 4, text: ‘status4’ }
  11.     ];
  12.     vm.groups = [];
  13.     vm.loadGroups = function () {
  14.         return vm.groups.length ? null : $http.get(‘server/xeditable-groups.json’).success(function (data) {
  15.             vm.groups = data;
  16.         });
  17.     };
  18.     vm.showGroup = function (user) {
  19.         if (user.group && vm.groups.length) {
  20.             var selected = $filter(‘filter’)(vm.groups, { id: user.group });
  21.             return selected.length ? selected[0].text : ‘Not set’;
  22.         } else {
  23.             return user.groupName || ‘Not set’;
  24.         }
  25.     };
  26.     vm.showStatus = function (user) {
  27.         var selected = [];
  28.         if (user.status) {
  29.             selected = $filter(‘filter’)(vm.statuses, { value: user.status });
  30.         }
  31.         return selected.length ? selected[0].text : ‘Not set’;
  32.     };
  33.     vm.checkName = function (data, id) {
  34.         if (id === 2 && data !== ‘awesome’) {
  35.             return ‘Username 2 should be `awesome`’;
  36.         }
  37.     };

Delete row in the table

vm.removeUser = function (index) {

  1.         vm.users.splice(index, 1);
  2.     };

Add the new row

  1. vm.addUser = function () {
  2.         vm.inserted = {
  3.             id: vm.users.length + 1,
  4.             name: ,
  5.             status: null,
  6.             group: null,
  7.             isNew: true
  8.         };
  9.         vm.users.push(vm.inserted);
  10.     };

Run the Application. If you click Add row button, it will add one new row in this table. Suppose, we can edit or delete in same way. Save button enables, when you are going to edit a row.

Output 2

MVC

Editable Column

We can edit the instance of table column on time.

HTML code

  1. <table class=“table table-bordered table-hover bg-white”>
  2.                     <tr style=“font-weight: bold; white-space: nowrap”>
  3.                         <!– username header–>
  4.                         <td style=“width:40%”>
  5.                             Name
  6.                             <form editable-form=“” name=“nameform” onaftersave=“xtable.saveColumn(‘name’)” ng-show=“nameform.$visible”>
  7.                                 <button type=“submit” ng-disabled=“nameform.$waiting” class=“btn btn-info”>save</button>
  8.                                 <button type=“button” ng-disabled=“nameform.$waiting” ng-click=“nameform.$cancel()” class=“btn btn-default”>cancel</button>
  9.                             </form>
  10.                             <button ng-show=“!nameform.$visible” ng-click=“nameform.$show()” class=“btn  btn-info”>
  11.                                Edit
  12.                             </button>
  13.                         </td>
  14.                         <!– status header–>
  15.                         <td style=“width:30%”>
  16.                             Status
  17.                             <form editable-form=“” name=“statusform” onaftersave=“xtable.saveColumn(‘status’)” ng-show=“statusform.$visible”>
  18.                                 <button type=“submit” ng-disabled=“statusform.$waiting” class=“btn btn-info”>save</button>
  19.                                 <button type=“button” ng-disabled=“statusform.$waiting” ng-click=“statusform.$cancel()” class=“btn btn-default”>cancel</button>
  20.                             </form>
  21.                             <button ng-show=“!statusform.$visible” ng-click=“statusform.$show()” class=“btn  btn-info”>
  22.                                 Edit
  23.                             </button>
  24.                         </td>
  25.                         <!– group header–>
  26.                         <td style=“width:30%”>
  27.                             Group
  28.                             <form editable-form=“” name=“groupform” onaftersave=“xtable.saveColumn(‘group’)” ng-show=“groupform.$visible”>
  29.                                 <button type=“submit” ng-disabled=“groupform.$waiting” class=“btn btn-info”>save</button>
  30.                                 <button type=“button” ng-disabled=“groupform.$waiting” ng-click=“groupform.$cancel()” class=“btn btn-default”>cancel</button>
  31.                             </form>
  32.                             <button ng-show=“!groupform.$visible” ng-click=“groupform.$show()” class=“btn btn-info”>
  33.                                 Edit
  34.                             </button>
  35.                         </td>
  36.                     </tr>
  37.                     <tr ng-repeat=“user in xtable.users”>
  38.                         <td>
  39.                             <span editable-text=“user.name” e-name=“name” e-form=“nameform” onbeforesave=“xtable.checkName($data)”>{{ user.name || ’empty’ }}</span>
  40.                         </td>
  41.                         <td>
  42.                             <span editable-select=“user.status” e-name=“status” e-form=“statusform” e-ng-options=“s.value as s.text for s in xtable.statuses”>{{ xtable.showStatus(user) }}</span>
  43.                         </td>
  44.                         <td>
  45.                             <span editable-select=“user.group” e-name=“group” onshow=“xtable.loadGroups()” e-form=“groupform” e-ng-options=“g.id as g.text for g in xtable.groups”>{{ xtable.showGroup(user) }}</span>
  46.                         </td>
  47.                     </tr>
  48.                 </table>

Once you click Edit button given above the table column header, it will enable to edit mode.

Angular Controller code

  1. vm.saveColumn = function (column) {
  2.         var results = [];
  3.         angular.forEach(vm.users, function (user) {
  4.             alert(‘Saving column: ‘ + column);
  5.         });
  6.         return $q.all(results);
  7.     };

Output 3

MVC

Editable Table

The whole table into <from > tag with editable-from attribute. It allows to retrieve all the changes.

HTML code

  1. <form editable-form=“” name=“tableform” onaftersave=“xtable.saveTable()” oncancel=“xtable.cancel()”>
  2.                     <!– table–>
  3.                     <table class=“table table-bordered table-hover bg-white”>
  4.                         <tr style=“font-weight: bold”>
  5.                             <td style=“width:40%”>Name</td>
  6.                             <td style=“width:30%”>Status</td>
  7.                             <td style=“width:30%”>Group</td>
  8.                             <td style=“width:30%”>
  9.                                 <span ng-show=“tableform.$visible”>Action</span>
  10.                             </td>
  11.                         </tr>
  12.                         <tr ng-repeat=“user in xtable.users | filter:filterUser”>
  13.                             <td>
  14.                                 <!– editable username (text with validation)–>
  15.                                 <span editable-text=“user.name” e-form=“tableform” onbeforesave=“xtable.checkName($data, user.id)”>{{ user.name || ’empty’ }}</span>
  16.                             </td>
  17.                             <td>
  18.                                 <!– editable status (select-local)–>
  19.                                 <span editable-select=“user.status” e-form=“tableform” e-ng-options=“s.value as s.text for s in xtable.statuses”>{{ xtable.showStatus(user) }}</span>
  20.                             </td>
  21.                             <td>
  22.                                 <!– editable group (select-remote)–>
  23.                                 <span editable-select=“user.group” e-form=“tableform” onshow=“xtable.loadGroups()” e-ng-options=“g.id as g.text for g in xtable.groups”>{{ xtable.showGroup(user) }}</span>
  24.                             </td>
  25.                             <td>
  26.                                 <button type=“button” ng-show=“tableform.$visible” ng-click=“deleteUser(user.id)” class=“btn btn-danger pull-right”>
  27.                                     Delete
  28.                                 </button>
  29.                             </td>
  30.                         </tr>
  31.                     </table>
  32.                     <!– buttons–>
  33. class=“btn-edit”>
  34.                         “button” ng-show=“!tableform.$visible” ng-click=“tableform.$show()” class=“btn btn-default”>edit
  • “tableform.$visible” class=“btn-form”>
  •                         “button” ng-disabled=“tableform.$waiting” ng-click=“addUser()” class=“btn btn-default pull-right”>add row
  •                         “submit” ng-disabled=“tableform.$waiting” class=“btn btn-info”>save
  •                         “button” ng-disabled=“tableform.$waiting” ng-click=“tableform.$cancel()” class=“btn btn-default”>cancel
  •                 </form>

Angular Controller code

  1. vm.filterUser = function (user) {
  2.         return user.isDeleted !== true;
  3.     };
  4.     vm.deleteUser = function (id) {
  5.         var filtered = $filter(‘filter’)(vm.users, { id: id });
  6.         if (filtered.length) {
  7.             filtered[0].isDeleted = true;
  8.         }
  9.     };
  10.     vm.cancel = function () {
  11.         for (var i = vm.users.length; i–;) {
  12.             var user = vm.users[i];
  13.             if (user.isDeleted) {
  14.                 delete user.isDeleted;
  15.             }
  16.             if (user.isNew) {
  17.                 vm.users.splice(i, 1);
  18.             }
  19.         }
  20.     };
  21.     vm.saveTable = function () {
  22.         var results = [];
  23.         for (var i = vm.users.length; i–;) {
  24.             var user = vm.users[i];
  25.             if (user.isDeleted) {
  26.                 vm.users.splice(i, 1);
  27.             }
  28.             if (user.isNew) {
  29.                 user.isNew = false;
  30.             }
  31.             alert(‘Saving Table…’);
  32.         }
  33.         return $q.all(results);
  34.     };

Run the Application

Output 4

Before editable table

MVC

After editable table

MVC

Note

New row data will be added in JSON format. Afterwards, click Save button. If we need, we can valid the right data. Don’t forget to add the “editable” keyword, using Angular xeditable.

For source download, click here.

Conclusion

In this article, we have learned MVC, using Angular xeditable. If you have any queries, please tell me through the comments section. Your comments are very valuable.

Advertisements