Datatables应用实例 (极致精简)

(仅供参考,不喜勿喷)

//附sql------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1 CREATE DATABASE Exams
 2 
 3 USE Exams  4  5 CREATE TABLE datatables  6 (  7 ID INT PRIMARY KEY IDENTITY,  8 Name NVARCHAR(50),  9 Age NVARCHAR(50), 10 Sex NVARCHAR(50), 11 [Address] NVARCHAR(50), 12 Office NVARCHAR(50) 13 ) 14 DECLARE @i INT =0; 15 WHILE (@i<100) 16 BEGIN 17 18 INSERT dbo.datatables 19  ( Name, Age, Sex, Address, Office ) 20 VALUES ( N''+CONVERT(NVARCHAR(50),@i) , -- Name - nvarchar(50) 21 N'5'+@i, -- Age - nvarchar(50) 22 N'0'+@i, -- Sex - nvarchar(50) 23 N'beiji'+CONVERT(NVARCHAR(50),@i), -- Address - nvarchar(50) 24 N'bangong'+CONVERT(NVARCHAR(50),@i) -- Office - nvarchar(50) 25  ) 26 SET @i=@i+1; 27 END

//前台主界面----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

  1 <html>
  2 <head>
  3     <meta name="viewport" content="width=device-width" />
  4     <title>Index</title>
  5     @*<script src="~/DataTables-1.10.15/media/js/jquery.js"></script>*@
  6     <script src="~/jquery-1.8.2.min.js"></script>
  7     <link href="~/DataTables-1.10.15/media/css/jquery.dataTables.min.css" rel="stylesheet" />
  8     <script src="~/DataTables-1.10.15/media/js/jquery.dataTables.min.js"></script>
  9     <script src="~/layer-v3.0.3/layer/layer.js"></script>
 10 </head>
 11 <body>
 12     <div> 
 13         <input id="selname" /><input type="button" value="查询" οnclick="search()" />
 14         <div>
 15             <input type="button" value="添加" οnclick="AddStudent()" />
 16             <table id="exec" class="display" cellspacing="0" width="100%">
 17                 <thead>
 18                     <tr>
 19                         <th>ID</th>
 20                         <th>Name</th>
 21                         <th>Age</th>
 22                         <th>Sex</th>
 23                         <th>Address</th>
 24                         <th>Office</th>
 25                         <th>操作</th>
 26                     </tr>
 27                 </thead>
 28 
 29                 <tfoot>
 30                     <tr>
 31                         <th>ID</th>
 32                         <th>Name</th>
 33                         <th>Age</th>
 34                         <th>Sex</th>
 35                         <th>Address</th>
 36                         <th>Office</th>
 37                         <th>操作</th>
 38                         
 39                     </tr>
 40                 </tfoot>
 41             </table>
 42         </div>
 43         </div>
 44     <script>
 45         function UpdateFromId(id)
 46         {
 47             layer.open({
 48                 type: 2,
 49                 area: ['40%', '80%'],
 50                 content: '/Default/UpdateFromId/' + id, end: function () { search(); }
 51 
 52             });
 53         }
 54         function AddStudent()
 55         {
 56             layer.open({
 57                 type: 2,
 58                 area: ['40%','80%'],
 59                 content: '/Default/AddStuden',
 60                 end: function () { search(); }
 61             });
 62              
 63 
 64         }
 65         function delFromID(id)
 66         {
 67             if (confirm("确定要删除吗?")) 
 68             {
 69                 $.ajax({
 70 
 71                     url: "delectbyid/"+id,
 72                     type:"get",
 73                     success: function (data)
 74                     {
 75                         alert(data);
 76                         search();
 77                     }
 78                 });
 79             } 
 80         }
 81         search();
 82         function search() {
 83             $('#example').dataTable().fnClearTable();
 84 
 85             var datatable = $('#exec').dataTable({
 86                 "bStateSave": true,
 87                 "bJQueryUI": true,
 88                 "bPaginate": true,// 分页按钮  
 89                 "bInfo": true,// Showing 1 to 10 of 23 entries 总记录数没也显示多少等信息  
 90                 "bWidth": true,
 91                 "bScrollCollapse": true,
 92                 "sPaginationType": "full_numbers", // 分页,一共两种样式 另一种为two_button // 是datatables默认  
 93                 "bProcessing": true,
 94                 "bServerSide": true,                          
 95                 "bFilter": true, //过滤功能
 96                 "bAutoWidth": true,//自动宽度
 97                 "bDestroy": true,
 98                 "sScrollY": "100%",
 99                 "bSortCellsTop": true,
100                 "bLengthChange": true,
101                 "pageLength": 5, //每页显示多少条
102                 "serverSide": true,//服务端模式
103                 "paging": true, //是否显示分页
104                 "oLanguage": {
105                     "sLengthMenu": "每页显示 _MENU_条",
106                     "sZeroRecords": "没有找到符合条件的数据",
107                     "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
108                     "sInfo": "当前第 _START_ - _END_ 条 共计 _TOTAL_ 条",
109                     "sInfoEmpty": "木有记录",
110                     "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
111                     "sSearch": "搜索:",
112                     "oPaginate": {
113                         "sFirst": "首页",
114                         "sPrevious": "前一页",
115                         "sNext": "后一页",
116                         "sLast": "尾页"
117                     }
118                 },
119 
120                  destroy: true,//--------------不能少,少了就不对
121                 ajax: {
122                     url: "Getdata",
123                     data: function (d) {           
124                         d.name = $("#selname").val();
125                     }
126                 },
127                 "columns":
128                     [
129                                 { "data": "ID" },
130                                 { "data": "Name" },
131                                 { "data": "Age" },
132                                 { "data": "Sex" },
133                                 { "data": "Address" },
134                                 { "data": "Office" },
135                                 { "data": "Caozuo" }
136 
137                     ]
138                 
139                 //"columnDefs":
140                 //    [{
141                 //        targets: 6,
142                 //        render: function (data, type, row, meta) {
143                 //            return '<input type="button"  οnclick=delFromID(' + ID + ')  value="删除" />';
144                 //        }
145                 //    },
146 
147                 //          { "orderable": false, "targets": 6},
148                 //    ],
149                     
150                 });
151 
152         }
153 
154 
155 
156 
157     </script>
158 </body>
159 </html>

 

 80
 
  

 

 
{ 81 alert(data); 82 search(); 83 } 84 }); 85 } 86 } 87 search(); 88 function search() { 89 $('#example').dataTable().fnClearTable(); 90 91 var datatable = $('#exec').dataTable({ 92 "pageLength": 5, //每页显示多少条 93 "serverSide": true,//服务端模式 94 "paging": true, //是否显示分页 95 destroy: true,//--------------不能少,少了就不对 96 ajax: { 97 url: "Getdata", 98 data: function (d) { 99 d.name = $("#selname").val(); 100 } 101 }, 102 "columns": 103 [ 104 { "data": "ID" }, 105 { "data": "Name" }, 106 { "data": "Age" }, 107 { "data": "Sex" }, 108 { "data": "Address" }, 109 { "data": "Office" }, 110 { "data": "Caozuo" } 111 112 ] 113 114 //"columnDefs": 115 // [{ 116 // targets: 6, 117 // render: function (data, type, row, meta) { 118 // return '<input type="button" οnclick=delFromID(' + ID + ') value="删除" />'; 119 // } 120 // }, 121 122 // { "orderable": false, "targets": 6}, 123 // ], 124 125 }); 126 127 } 128 129 130 131 132 </script> 133 </body> 134 </html>

//添加界面------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1 @{
 2     Layout = null;
 3 }  4  5 <!DOCTYPE html>  6  7 <html>  8 <head>  9 <meta name="viewport" content="width=device-width" /> 10 11 <title>AddStuden</title> 12 <script src="~/bootstrap.min.js"></script> 13 <script src="~/jquery-1.8.2.min.js"></script> 14 </head> 15 <body> 16 <div style="margin:0 auto; width:auto; height:auto; margin-left:200px; margin-top:100px;" > 17 <form method="post" action="" enctype="multipart/form-data"> 18 <h1>添加学生</h1> 19 <table class="table-responsive" > 20 <tr><td>姓名:<input name="Name" /></td></tr> 21 <tr></tr> 22 <tr></tr> 23 <tr><td>年龄:<input name="Age" /></td></tr> 24 <tr></tr> 25 <tr></tr> 26 <tr><td>性别:<input name="Sex" /></td></tr> 27 <tr></tr> 28 <tr></tr> 29 <tr><td>住址:<input name="Address" /></td></tr> 30 <tr></tr> 31 <tr></tr> 32 <tr><td> 职务:<input name="Office" /></td></tr> 33 <tr></tr> 34 <tr></tr> 35 </table> 36 <input type="submit" value="添加" οnclick="thisclear()"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="返回" οnclick=" thisclear()"/> 37 </form> 38 </div> 39 <script> 40  function thisclear() 41  { 42 var index = parent.layer.getFrameIndex(window.name); 43  parent.layer.close(index); 44  } 45 </script> 46 </body> 47 </html>

//编辑界面------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1 @{
 2     Layout = null;
 3 }  4  5 <!DOCTYPE html>  6 @model WebApplication1.Controllers.datatables  7 <html>  8 <head>  9 <meta name="viewport" content="width=device-width" /> 10 <title>UpdateFromId</title> 11 <script src="~/bootstrap.min.js"></script> 12 <script src="~/jquery-1.8.2.min.js"></script> 13 </head> 14 <body> 15 <div style="margin:0 auto; width:auto; height:auto; margin-left:200px; margin-top:100px;"> 16 <form method="post" action="" enctype="multipart/form-data"> 17 <h1>编辑信息</h1> 18 <table class="table-responsive"> 19 <tr style="display:none;"><td>编号:@Html.TextBoxFor(T => T.ID)</td></tr> 20 <tr></tr> 21 <tr></tr> 22 23 <tr><td>姓名:@Html.TextBoxFor(T=>T.Name)</td></tr> 24 <tr></tr> 25 <tr></tr> 26 <tr><td>年龄:@Html.TextBoxFor(T => T.Age)</td></tr> 27 <tr></tr> 28 <tr></tr> 29 <tr><td>性别:@Html.TextBoxFor(T => T.Sex)</td></tr> 30 <tr></tr> 31 <tr></tr> 32 <tr><td>住址:@Html.TextBoxFor(T => T.Address)</td></tr> 33 <tr></tr> 34 <tr></tr> 35 <tr><td> 职务:@Html.TextBoxFor(T => T.Office)</td></tr> 36 <tr></tr> 37 <tr></tr> 38 </table> 39 <input type="submit" value="保存" οnclick="thisclear()" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="返回" οnclick=" thisclear()" /> 40 </form> 41 </div> 42 <script> 43  function thisclear() 44  { 45 var index = parent.layer.getFrameIndex(window.name); 46  parent.layer.close(index); 47  } 48 </script> 49 </body> 50 </html>

//控制器------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  1 using System;
  2 using System.Collections.Generic;  3 using System.Data;  4 using System.Data.SqlClient;  5 using System.Linq;  6 using System.Web;  7 using System.Web.Mvc;  8  9 namespace WebApplication1.Controllers  10 {  11 public class DefaultController : Controller  12  {  13 // GET: Default  14 public ActionResult Index()  15  {  16 return View();  17  }  18  [HttpGet]  19 public JsonResult Getdata(int draw, string name, int length = 5, int start = 0)  20  {  21 List<datatables> list = new List<datatables>();  22 int pagecount = 0;  23 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Exams;Integrated Security=True"))  24  {  25  conn.Open();  26 string sql = "select top ("+length+") * from datatables where Name like '%" + name + "%' and ID not in (select top ("+start+ ") ID from datatables where Name like '%" + name + "%' ) ";  27 SqlDataAdapter adp = new SqlDataAdapter(sql, conn);  29 SqlCommand cmd = new SqlCommand("select count(1) from datatables where Name like '%" + name + "%' ", conn);  30 pagecount = Convert.ToInt32(cmd.ExecuteScalar());  31 DataTable tb = new DataTable();  32  adp.Fill(tb);  35 foreach (DataRow item in tb.Rows)  36  {  37 datatables tbs = new datatables()  38  {  39  40 ID = Convert.ToInt32(item["ID"]),  41 Address = item["Address"].ToString(),  42 Age = item["Age"].ToString(),  43 Name = item["Name"].ToString(),  44 Office = item["Office"].ToString(),  45 Sex = item["Sex"].ToString(),  46 Caozuo = "<input type='button' οnclick=delFromID("+ Convert.ToInt32(item["ID"]) + ") value='删除' /> &nbsp;&nbsp;<input type='button' οnclick=UpdateFromId(" + Convert.ToInt32(item["ID"]) + ") value='修改' />"  47  };  48  list.Add(tbs);  49  conn.Close();  50  }  52  }  54 var result = new  55  {  56 draw = draw, //客户端唯一标识  57 recordsFiltered = pagecount,  58 recordsTotal = pagecount, //总行数 59 data = list 60 }; 61 return Json(result, JsonRequestBehavior.AllowGet); 62 } 63 64 public string delectbyid(int id) 65 { 66 int i = 0; 67 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Exams;Integrated Security=True")) 68 { 69 conn.Open(); 70 SqlCommand cmd = new SqlCommand("delete from datatables where ID="+id, conn); 71 i = Convert.ToInt32(cmd.ExecuteNonQuery()); 72 conn.Close(); 73 } 74 if (i==1) 75 { 76 return "删除成功"; 77 } 78 else 79 { 80 return "删除失败"; 81 } 82 83 } 84 85 86 public ActionResult AddStuden() 87 { 88 89 return View(); 90 91 } 92 [HttpPost] 93 public ActionResult AddStuden(datatables s) 94 { 95 int i = 0; 96 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Exams;Integrated Security=True")) 97 { 98 conn.Open(); 99 string sql = string.Format("insert datatables values ('{0}','{1}','{2}','{3}','{4}')",s.Name,s.Age,s.Sex,s.Address,s.Office); 100 SqlCommand cmd = new SqlCommand(sql,conn); 101 i = Convert.ToInt32(cmd.ExecuteNonQuery()); 102 conn.Close(); 103 } 104 if (i == 1) 105 { 106 return View("Index"); 107 } 108 else 109 { 110 return Content("<script>alert('添加失败');;</script>"); 111 } 112 113 } 114 115 116 public ActionResult UpdateFromId(int id) 117 { 118 datatables tbs = new datatables(); 119 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Exams;Integrated Security=True")) 120 { 121 #region 122 string sql = "select * from datatables where ID="+id; 123 SqlDataAdapter adp = new SqlDataAdapter(sql, conn); 124 DataTable tb = new DataTable(); 125 adp.Fill(tb); 126 foreach (DataRow item in tb.Rows) 127 { 128 tbs = new datatables() 129 { 130 131 ID = Convert.ToInt32(item["ID"]), 132 Address = item["Address"].ToString(), 133 Age = item["Age"].ToString(), 134 Name = item["Name"].ToString(), 135 Office = item["Office"].ToString(), 136 Sex = item["Sex"].ToString(), 137 Caozuo = "<input type='button' οnclick=delFromID(" + Convert.ToInt32(item["ID"]) + ") value='删除' /> &nbsp;&nbsp;<input type='button' οnclick=UpdateFromId(" + Convert.ToInt32(item["ID"]) + ") value='修改' />" 138 }; 139 140 141 } 142 #endregion 143 return View(tbs); 144 } 145 146 } 147 [HttpPost] 148 public ActionResult UpdateFromId(datatables s) 149 { 150 151 int i = 0; 152 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Exams;Integrated Security=True")) 153 { 154 conn.Open(); 155 string sql = string.Format("update datatables set Name='{0}',Age='{1}',Sex='{2}',Address='{3}',Office='{4}' where ID={5}", s.Name, s.Age, s.Sex, s.Address, s.Office,s.ID); 156 SqlCommand cmd = new SqlCommand(sql, conn); 157 i = Convert.ToInt32(cmd.ExecuteNonQuery()); 158 conn.Close(); 159 } 160 if (i == 1) 161 { 162 return Content("<script>alert('编辑成功');location.href='/Default/Index';</script>"); 163 } 164 else 165 { 166 return Content("<script>alert('编辑失败');</script>"); 167 } 168 } 169 170 171 } 172 public class datatables 173 { 174 public int ID { get; set; } 175 public string Name { get; set; } 176 public string Age { get; set; } 177 public string Sex { get; set; } 178 public string Address { get; set; } 179 public string Office { get; set; } 180 181 public string Caozuo { get; set; } 182 } 183 }

 



 

转载于:https://www.cnblogs.com/-bai/p/7929625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值