📜 ⬆️ ⬇️

OData + Angular.js + Bootstrap + JavaScript Grid = application in 5 minutes

Suppose in a certain project there is a need to add some form of user polling on a web site (detailed form) and a form for viewing and editing the list of users for the system administrator (list form).

Consider the process of creating these forms using OData, Angular.js, Bootstrap and JavaScript Grid. All requirements for such an application are already implemented in these tools, and we practically do not need to write anything.


We list the basic requirements for the survey form (in parentheses are the tools that implement this requirement):

Basic requirements for the form to work with the list of users:

Any JavaScript grid fully satisfies all these requirements. For example, you should look at the Kendo UI libraries, DevExtreme Web , Syncfusion HTML5 controls , Infragistics HTML5 controls , OpenUI5 , Wijmo .
')
Basic server requests:

To avoid the need to implement handlers for all the listed requests, we will take the databoom , which is a ready-made OData server and fully provides automatic processing of all these requests.

Survey Form


image
image
We will create a survey form (detailed form) using Bootstrap and Angular.js:

We recommend that you always have on hand several ready-made templates of various shapes. For example:

It is very good to keep templates on any site like JSFiddle, Bootply, CodePen, Plunker, etc.

Using a simple copy-paste based on a similar example form:




<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example - example-example32-production</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <link href="http://bootswatch.com/cerulean/bootstrap.min.css" rel="stylesheet" type="text/css"> <style>.form-control {border-width:2px}</style> </head> <body ng-app="formExample" ng-controller="ExampleController"> <div class="container"> <div class="col-md-12"> <h1>Simple Bootstrap + Angular.js form</h1> <br /> </div> <form name="myForm"> <div class="col-md-4"> <div class="form-group"> <label for="firstname">First Name:</label> <input class="form-control" id="firstname" type="text" placeholder="Enter first name" ng-model="user.firstname" required autofocus /> </div> <div class="form-group"> <label for="lastname">Last Name:</label> <input class="form-control" type="text" id="lastname" placeholder="Enter last name" ng-model="user.lastname" required /> </div> <div class="form-group"> <label for="email">Email:</label> <input class="form-control" type="email" id="email" name="input" placeholder="Enter email" ng-model="user.email" required> </div> </div> <div class="col-md-8"> <div class="form-group"> <label for="comment">Comment:</label> <textarea rows="9" class="form-control" id="comment" placeholder="Enter comment" ng-model="user.comment"></textarea> </div> </div> <div class="col-md-12"> <input class="btn btn-primary" type="submit" ng-click="insert(myForm.$valid, user)" value="Add new user" /> <input class="btn btn-info" type="submit" ng-click="update(myForm.$valid, user)" value="Update" /><br /><br /> </div> </form> </div> <script> angular.module('formExample', []) .controller('ExampleController', ['$scope', '$http', function ($scope, $http) { $scope.update = function (isvalid, user) { if (!isvalid) return; $http.post('https://samples.databoom.space/api1/sandboxdb/collections/user', JSON.stringify(user)) .success(function (data, status, headers, config) { $scope.user = data.d.results; }); }; $scope.insert = function (isvalid, user) { if (!isvalid) return; user.id = undefined; $scope.update(isvalid, user); } }]); </script> </body> </html> 

a list of users


image

The list of users (list form) will be made based on the example of the grid control from the Kendo UI library. Virtually all programming will be reduced to specifying the correct URLs for data modification operations and to the description of the grid columns.

 <!DOCTYPE html> <html> <head> <base href="http://demos.telerik.com/kendo-ui/grid/remote-data-binding"> <style>html {font-size: 12px;font-family: Arial, Helvetica, sans-serif;} body {margin: 0;} </style> <title></title> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" /> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" /> <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script> <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script> </head> <body> <div id="example"> <div id="grid"></div> <script> $(document).ready(function () { $("#grid").kendoGrid({ dataSource: { type: "odata", transport: { read: "https://samples.databoom.space/api1/sandboxdb/collections/user", create: { url: "https://samples.databoom.space/api1/sandboxdb/collections/user" }, update: { url: "https://samples.databoom.space/api1/sandboxdb/collections/user" }, destroy: { url: function (data) { return "https://samples.databoom.space/api1/sandboxdb/collections/user(" + data.id + ")"; } } }, pageSize: 20, serverPaging: true, serverFiltering: true, serverSorting: true, schema: { model: { id: "id" } } }, filterable: true, editable: true, toolbar: ["create", "save", "cancel"], sortable: true, pageable: true, columns: ["firstname", "lastname", "email", "comment", { command: "destroy", title: " ", width: 150 }] }); }); </script> </div> </body> </html> 


Server


To process all the requests required in this example, we will not need to develop any codes at all. All database requests are fully covered by the Odata standard, and the databoom automatically processes them.

Conclusion


There are many libraries to simplify web development, for example: Angular.js , Bootstrap , Kendo UI , DevExtreme Web , Syncfusion HTML5 controls , Infragistics HTML5 controls , OpenUI5 , Wijmo , JayData , Breeze.js , datajs , ODataJS , databoom , etc. Each of them solves his problem. Successful selection of a set of libraries for a specific task can significantly reduce development time.

Using ready-made form templates using the libraries listed above allows you to develop on the fly using copy-paste and making only minor changes to the code.

Source: https://habr.com/ru/post/252657/


All Articles