Friday 9 December 2016

Angularjs Recursive TreeView




<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Test</title>
    <script src="angular.js"></script>
    <script type="text/javascript">
        angular.module('myApp', [])
        .controller('myController', function ($scope,$http) {
            $http.get('MenuHandler.ashx').success(function (response) {               
                $scope.MenuList = response;
            }).error(function (error) {
                alert(error);
            })
        })
    </script>
</head>
<body ng-controller="myController">
    <ul>
        <li ng-repeat="menu in MenuList" ng-include="'categoryTree'">          
        </li>
    </ul>

    <script type="text/ng-template" id="categoryTree">
        {{menu.MenuText}}      
        <ul ng-if="menu.List">
            <li ng-repeat="menu in menu.List" ng-include="'categoryTree'">
                {{menu.MenuText}}
            </li>
        </ul>
    </script>

</body>
</html>


Menu Handler.ashx
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

public class MenuHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        List<Menu> listMenu = new List<Menu>();
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("spGetMenuData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Menu menu = new Menu();
                menu.Id = Convert.ToInt32(rdr["Id"]);
                menu.MenuText = rdr["MenuText"].ToString();
                menu.ParentId = rdr["ParentId"] != DBNull.Value
                    ? Convert.ToInt32(rdr["ParentId"]) : (int?)null;
                menu.Active = Convert.ToBoolean(rdr["Active"]);
                listMenu.Add(menu);
            }
        }

        List<Menu> menuTree = GetMenuTree(listMenu, null);

        JavaScriptSerializer js = new JavaScriptSerializer();
        context.Response.Write(js.Serialize(menuTree));
    }

    public List<Menu> GetMenuTree(List<Menu> list, int? parent)
    {
        return list.Where(x => x.ParentId == parent).Select(x => new Menu
        {
            Id = x.Id,
            MenuText = x.MenuText,
            ParentId = x.ParentId,
            Active = x.Active,
            List = GetMenuTree(list, x.Id)
        }).ToList();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}


Output


Sunday 25 September 2016

AngularJs UI Modal




<!DOCTYPE html>
<html ng-app="modaltest">
<head>
    <title></title>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.4.js"></script>
        <script src="modal.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    </head>
</head>
<body ng-controller="ModalCtrl" class="container">
    <div class="jumbotron">
        <div class="row">
            <div class="col-lg-offset col-lg-4">
                <button class="btn btn btn-primary" ng-click="open()">
                    Open
                </button>
            </div>
            <div class="col-lg-4">
                <select class="form-control" ng-model="size">                               
                    <option value="">-Select-</option>
                    <option value="lg">Large Modal</option>
                    <option value="md">Medium Device Modal</option>
                    <option value="sm">Small Modal</option>
                </select>
            </div>
        </div>       
    </div>   
    <script type="text/ng-template" id="modal.html">
        <div class="modal-header">
            <h3>Modal Test</h3>
        </div>
        <div class="modal-body">
            <table class="table table-condensed">
                <tr>
                    <td>
                         Name
                    </td>
                    <td>
                        City
                    </td>
                </tr>
                <tr ng-repeat="emp in employees">
                    <td>
                        {{emp.name}}
                    </td>
                    <td>
                        {{emp.city}}
                    </td>
                </tr>
            </table>
        </div>
        <div class="modal-footer">
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
                <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
            </div>
        </div>
    </script>
</body>
</html>


Modal.js

'use strict'
angular.module('modaltest', ['ngAnimate', 'ngSanitize', 'ui.bootstrap'])
.controller('ModalCtrl', ['$scope', '$uibModal', function ($scope, $uibModal) {
    $scope.open = function () {
        var modalInstance = $uibModal.open({
            templateUrl: 'modal.html',
            controller: 'modalctrl',
            size: $scope.size
        });
    }
}])
.controller('modalctrl', function ($uibModalInstance, $scope) {
    $scope.employees = [
        { name: 'Ranjeet', city: 'Mumbai' },
        { name: 'Munna', city: 'Odisha' },
        { name: 'Abhishek', city: 'Mumbai' },
    ];
    $scope.ok = function () {
        $uibModalInstance.close();
    }

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    }
})