GithubHelp home page GithubHelp logo

tiagobarreto / simple-crud-angular-js Goto Github PK

View Code? Open in Web Editor NEW
19.0 4.0 14.0 346 KB

Crud with Angular JS

Home Page: http://www.tiagobarreto.com/simples-aplicacao-em-angularjs/

JavaScript 85.21% CSS 11.32% HTML 3.47%

simple-crud-angular-js's Introduction

Angular JS: an overview

This example is upgrade of angular js tutorial from Tableless by Davi Ferreira. Features added in the code: Filter, Delete and Edit Products, complete crud.

Brunch.io

npm install -g brunch

ng-controller (ng-controller="myController")

ng-model (ng-model="item.name")

ng-view (ng-view)

ng-click (ng-click="myFunction()")

ng-repeat (ng-repeat="item in itens")

ng-hide|show (ng-hide="boolean") ng-href (ng-href="link")

ng-init (ng-init="expression")

ng-readonly (ng-readonly="expression")

ng-disabled (ng-disabled="expression")

See Angular JS Cheat Sheet.

Hello World

Below the basic example with angular js:

	<html ng-app>
		<head>
			<title>Basic Example with Angular JS </title>
			<script src="js/libs/angular-1.0.1.min.js"></script>
		</head>
		<body>
			<input type="text" ng-model="name" placeholder="Type your name">
        	<p>Hello {{ name }} !</p>
		</body>
	<html>

See JSFiddle.

List Products in Controller

Store products in items model defined by controller:

	function ListProductsController($scope) {
    	$scope.items = [
        	{product: 'Milk', amount: 2, purchased: false},
        	{product: 'Beer', amount: 12, purchased: false}
    	];
	}

Now, list products with http request to return items json:

	function ListProductsController($scope) {
		$scope.fetchProductsList = function() {
    		$http.get('http://www.url.com/products').success(function(products){
        		$scope.items = products;
    		});
		}
	}

List Products in View

Show products in view:

List Products

	<div ng-controller="ListaComprasController">
		<table>
  			<thead>
    			<tr>
      				<th>Produto</th>
      				<th>Quantidade</th>
    			</tr>
  			</thead>
  			<tbody>
    			<tr ng-repeat="item in itens">
      				<td><strong>{{ item.produto }}</strong></td>
      				<td>{{ item.quantidade }}</td>
    			</tr>
  			</tbody>
		</table>
	</div>

Filter Products in View

Added the filter attr in the ng-repeat to enabled filter:

Filter Product

<tr ng-repeat="item in items | filter:search">

Also added a input text to search products:

<input type="search" ng-model="search" placeholder="Search by…">

Add Products

Add products in view:

	<form name="products">
    	<input type="text" ng-model="item.product">
    	<input type="number" ng-model="item.amount">
    	<button ng-click="addItem()">add item</button>
	</form>

Functions in controller:

Add item in the items array

	$scope.addItem = function () {
    	$scope.items.push({product: $scope.item.product, amount: $scope.item.amount, purchase: false});
	};

Add item with HTTP Request

	$scope.addItem = function(product) {
    	$http.post('/products/', product).success(function(product) {
        	toastr.success("Item added.");
        	$scope.items.push(product);
     	}).error(function(data) {
        	toastr.error("Fail on adding product.");
    	});
 	};

Remove Products

Remove products in view:

	<button class="btn btn-danger btn-small" ng-click="deleteItem($index)">
    	<i class="icon-trash icon-white"></i> remove
	</button>

Function to delete item in Controller

	$scope.deleteItem = function(index){
    	$scope.items.splice(index, 1);
	};

Edit Products

Edit Product

Edit products used the main button from form to change the product with the ng-hide and ng-show in view:

	$scope.editItem = function(index){
    	$scope.item = $scope.items[index];
	};

Routes Application

	var application = {};

	var App = angular.module('application');

	App.config(['$routeProvider', function ($routeProvider) {
    	$routeProvider
        	.when('/products', { templateUrl: 'views/products/list.html', controller: ProductsControllers })
        	.when('…', { templateUrl: '…', controller: ... });
	}]);

Libs used by example

More Details

Other projects

simple-crud-angular-js's People

Contributors

tailomateus avatar tiagobarreto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.