How to apply filters and paging in ng grid ?
example: Grid mvc working style ... Contains,Startswith and Endswith. and paging
2.Perform Crud Operations in mvc using angularjs NgGrid.
Please see the below uploaded example and help me..
Thanks in advanced
Emp.cs:
=======
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Grid.Models
- {
- public class Emp
- {
- public int id { get; set; }
- public string name { get; set; }
- public string designation { get; set; }
- public string mobile { get; set; }
- }
- }
DAL.cs
======
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
- namespace Grid.Models
- {
- public class DALEmp
- {
- string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- public List
ListAll() - {
- List
lst = new List (); - SqlConnection con = new SqlConnection(cs);
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from Employee", con);
- SqlDataReader dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- lst.Add(new Emp
- {
- id = Convert.ToInt32(dr["id"]),
- name = dr["name"].ToString(),
- designation = dr["designation"].ToString(),
- mobile = dr["mobile"].ToString()
- });
- }
- dr.Close();
- return lst;
- }
- public void SaveEmp(Emp emp)
- {
- string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- SqlConnection con = new SqlConnection(cs);
- con.Open();
- string save = "insert into Employee values(@id, @name, @designation, @mobile)";
- SqlCommand cmd = new SqlCommand(save, con);
- cmd.Parameters.AddWithValue("@id", emp.id);
- cmd.Parameters.AddWithValue("@name", emp.name);
- cmd.Parameters.AddWithValue("@designation", emp.designation);
- cmd.Parameters.AddWithValue("@mobile", emp.mobile);
- cmd.ExecuteNonQuery();
- con.Close();
- }
- public void Update(Emp emp)
- {
- string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- SqlConnection con = new SqlConnection(cs);
- con.Open();
- string update = "update Employee set name=@name, designation=@designation, mobile=@mobile where id=@id";
- SqlCommand cmd = new SqlCommand(update, con);
- cmd.Parameters.AddWithValue("@id", emp.id);
- cmd.Parameters.AddWithValue("@name", emp.name);
- cmd.Parameters.AddWithValue("@designation", emp.designation);
- cmd.Parameters.AddWithValue("@mobile", emp.mobile);
- cmd.ExecuteNonQuery();
- con.Close();
- }
- public void DeleteEmp(Emp emp)
- {
- string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- SqlConnection con = new SqlConnection(cs);
- con.Open();
- string delete = "delete from Employee where id=@id";
- SqlCommand cmd = new SqlCommand(delete, con);
- cmd.Parameters.AddWithValue("@id", emp.id);
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
HomeController.cs:
==================
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Grid.Models;
- namespace Grid.Controllers
- {
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- return View();
- }
- public JsonResult GetAllEmployee()
- {
- DALEmp empdal = new DALEmp();
- List
employee = empdal.ListAll().ToList(); - return Json(employee, JsonRequestBehavior.AllowGet);
- }
- [HttpPost]
- public ActionResult Create(Emp employee)
- {
- try
- {
- if (ModelState.IsValid)
- {
- DALEmp empdal = new DALEmp();
- empdal.SaveEmp(employee);
- return RedirectToAction("Index");
- }
- else
- {
- ModelState.AddModelError("", "Data Not Saved");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return View();
- }
- [HttpPost]
- public ActionResult Edit(Emp emp)
- {
- #region Update
- try
- {
- if (ModelState.IsValid)
- {
- DALEmp empdal = new DALEmp();
- empdal.Update(emp);
- return RedirectToAction("Index");
- }
- else
- {
- ModelState.AddModelError("", "Record is not Updated");
- return View();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return View();
- #endregion
- }
- [HttpPost]
- public ActionResult Delete(Emp emp)
- {
- #region Delete
- try
- {
- if (ModelState.IsValid)
- {
- DALEmp empdal = new DALEmp();
- empdal.DeleteEmp(emp);
- return RedirectToAction("Index");
- }
- else
- {
- ModelState.AddModelError("", "Record is not Deleted");
- return View();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return View();
- #endregion
- }
- public ActionResult Details()
- {
- return View();
- }
- }
- }
Index.cshtml:
=============
- "myApp">
AngularJS - "~/Content/bootstrap.min.css" rel="stylesheet" />
- "~/Content/ng-grid.css" rel="stylesheet" />
- "MyCtrl">
- class="gridStyle" ng-grid="gridOptions" ng-init="GetAllData()">
- class="btn btn-info btn-primary" data-toggle="modal" data-target="#myModal">Add New Customer
- "myModal" class="modal fade" role="dialog" >class="modal-dialog">class="modal-content">class="modal-header">
- class="close" data-dismiss="modal">×
class
="modal-title">Save Detailsclass="modal-body">"width:100%">
"width:22%"> - ="lblFont" for="Name">Employee ID class="lblMandatory">*
- "text" data-ng-model="id" required" />
- @*class="lblMandatory"ng-show="!id">Enter ID*@
"width:22%"> - ="lblFont" for="Name">Nameclass="lblMandatory">*
- "text" data-ng-model="name" name="text" required" />
- @*class="lblMandatory" ng-show="!name">Enter Name*@
"width:22%"> - ="lblFont" for="Name">Designationclass="lblMandatory">*
- "text" data-ng-model="designation" name="text" required" />
- @*class="lblMandatory" ng-show="!designation">Enter Designation*@
"width:22%"> - ="lblFont" for="Name">Mobileclass="lblMandatory">*
- "text" data-ng-model="mobile" name="number" required" />
- @*class="lblMandatory" ng-show="!mobile">Enter Mobile No.*@
class="modal-footer">- "button" class="btn btn-success" ng-click="save()" value="Submit" />
- class="btn btn-default" data-dismiss="modal">Close
Manikandan MurugesanPosted Jun 30, 2017, 7:21 AM