datatables

Controllerjavascript

 

using Datatables.datatablesCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;css

namespace Datatables.Controllers
{
public class UserTbController : Controller
{
testdbEntities db = new testdbEntities();
//
// GET: /UserTb/java

public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult GetData(FormCollection collection)
{
int start = Convert.ToInt16(Request.Form["start"]);
int length= Convert.ToInt16(Request.Form["length"]);
List<UserTb> list = db.UserTb.OrderBy(p=>p.id).Skip(start).Take(length).ToList();ajax

var aa = new { iTotalRecords = db.UserTb.Count(), iTotalDisplayRecords = db.UserTb.Count(), aaData = list };
//sEcho = 2,
// Thread.Sleep(2000);
return Json(aa, JsonRequestBehavior.AllowGet);
}app

 

public ActionResult List()
{
//延時加載
IQueryable<UserTb> users = db.UserTb.AsQueryable<UserTb>();dom

if (Request["start"] != null)
{ide

var parser = new DataTableParser<UserTb>(Request, users);url

return Json(parser.Parse());spa

}翻譯

return Json(new { aaData = users });

}


//
// GET: /UserTb/Details/5

public ActionResult Details(int id)
{
return View();
}

//
// GET: /UserTb/Create

public ActionResult Create()
{
return View();
}

//
// POST: /UserTb/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
UserTb ut = new UserTb {
UerName=collection["UerName"],
UserNo = collection["UserNo"],
Age =int.Parse( collection["Age"])
};

db.UserTb.Add(ut);
db.SaveChanges();

return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /UserTb/Edit/5

public ActionResult Edit(int id)
{
return View();
}

//
// POST: /UserTb/Edit/5

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here

return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /UserTb/Delete/5

public ActionResult Delete(int id)
{
return View();
}

//
// POST: /UserTb/Delete/5

[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here

return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}

 

 

 

JS:

$(function () {
$('#list').dataTable({
"oLanguage": {//下面是一些漢語翻譯
"sProcessing": "處理中...",
"sLengthMenu": "顯示 _MENU_ 項結果",
"sZeroRecords": "沒有匹配結果",
"sInfo": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
"sInfoEmpty": "顯示第 0 至 0 項結果,共 0 項",
"sInfoFiltered": "(由 _MAX_ 項結果過濾)",
"sInfoPostFix": "",
"sSearch": "搜索:",
"sUrl": "",
"sEmptyTable": "暫無數據",
"sLoadingRecords": "數據加載中...",
"sInfoThousands": ",",
"oPaginate": {
"sFirst": "首頁",
"sPrevious": "上頁",
"sNext": "下頁",
"sLast": "末頁"
},
"oAria": {
"sSortAscending": ": 以升序排列此列",
"sSortDescending": ": 以降序排列此列"
}
},
"processing": true,
//"iDisplayLength": 10,//每頁顯示10條數據
//"iDisplayStart":1,
"serverSide": true,
"sPaginationType": "full_numbers",

"dom": '<"#toolsbar"><"top"i>rt<"bottom"flp><"clear">',
// "dom": '<"top"i>rt<"bottom"flp><"clear">',
"ajax": {
"url": "/UserTb/GetData", //List
"type": "POST",
"data": function (d) {
d.ttStart = "iDisplayStart",
d.ttyLength = "iDisplayLength",// + " 23:59:59",
d.uu=89
//d.keyword = "sSearch"
}
},
"columns": [
{ "data": "id" },
{ "data": "UserNo" },
{ "data": "UerName" },
{ "data": "Age" }
],
initComplete: initComplete
});

});

function initComplete(data) {
// $("#toolsbar").css("float", "left");
$("#toolsbar").append('<div style="float: left;"><input type="text"/></div>');
}

 

view:

@{
ViewBag.Title = "Index";
}
@*<script src="~/Scripts/dataTables.buttons.min.js" type="text/javascript"></script>*@

<h2>Index</h2>
<div>

<table id="list" class="gridtable" style="width:100%;"> <thead> <th>ID</th> <th>USERNO</th> <th>USERNAME</th> <th>AGE</th> </thead> <tbody> </tbody> </table></div>

相關文章
相關標籤/搜索