Easyui datagrid 怎麼新增操作按鈕,rowStyler
阿新 • • 發佈:2019-01-11
說明:本篇文章主要是展示怎麼設定easyUI datagrid的格式,包括行樣式和列樣式,以及新增操作按鈕列
開發環境 vs2012 asp.net mvc4 c#
1、效果圖
3、HTML程式碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataGridTest.aspx.cs" Inherits="MvcAppTest.DataGridTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="Easyui/themes/default/easyui.css" rel="stylesheet" />
<script src="Easyui/jquery-1.7.2.min.js" ></script>
<script src="Easyui/jquery.easyui.min.js"></script>
<script src="Easyui/locale/easyui-lang-zh_CN.js"></script>
<style type="text/css">
/*.datagrid-btable .datagrid-cell{overflow: hidden;text-overflow:ellipsis;white-space: nowrap;}
.datagrid-header {position: absolute; visibility: hidden;} */
</style>
<script type="text/javascript">
var datagrid;
var officeId = 100;
var searchText = '';
$(function () {
InitData();
});
function InitData()
{
datagrid = $('#wg').datagrid({
url: 'Home/getWGList',
title: 'datagrid列表',
iconCls: 'icon-save',
pagination: true,
pagePosition: 'bottom',
pageSize: 10,
height: 500,
width:600,
pageList: [10, 20, 30, 40],
queryParams: { officeId: officeId, srText: searchText },
fitColumns: false,
nowrap: true,
border: false,
idField: 'PID',
sortName: 'PID',
sortOrder: 'desc',
rownumbers: false,
singleSelect: false,
checkOnSelect: true,
selectOnCheck: true,
columns: [[{
field: 'ck',
checkbox:true
}, {
title: 'PID',
field: 'PID',
width:50
}, {
title: '專案名稱',
field: 'PRJNAME',
width:120,
formatter: function (value, row, index) {
return '<span title=' + value + '>' + value + '</span>'
}
}, {
title: '價格',
field: 'Price',
width: 100,
formatter: function (value, row, index) {
if (value % 2 == 0) {
return '<span style="color:red;">' + value + '</span>';
} else {
return value;
}
}
},
{
field: 'ID', title: '操作', width: '35%', align: 'center', formatter: formatOper
}]],
rowStyler: function (index, row) {
if(row.Price>30){
return 'background-color:#6293BB;color:#fff;font-weight:bold;';
}
},
onRowContextMenu: function (e, rowIndex, rowData) {
e.preventDefault();
$(this).datagrid('unselectAll');
$(this).datagrid('selectRow', rowIndex);
},
onCheck: function (rowIndex, rowData) {
var PID = rowData.PID;
},
onLoadSuccess: function (data) {
}
});
}
function formatOper(val, row, index) {
return '<input type="button" onclick="remove(\'' + row["PID"] + '\')" value="校驗" /> <input type="button" onclick="removeXM(\'' + row["PID"] + '\')" value="刪除" />';
}
function removeXM(pid) {
alert('刪除');
}
function remove(pid) {
alert('校驗');
}
function rowStyle(index,row)
{
if (row.Price > 30) {
//return '<span style="color:red;">'+value+'</span>';
return 'background-color:#6293BB;color:#fff;';
}
}
</script>
</head>
<body>
<div>
<table id="wg"></table>
</div>
</body>
</html>
4、Home控制器後臺程式碼
public JsonResult getWGList(string officeId, string srText) { DataGridWGModel model = new DataGridWGModel(); var pageIndex = 1; var pageSize = 15; if (Request["page"] != null) { pageIndex = Int32.Parse(Request["page"].ToString()); } if (Request["rows"] != null) { pageSize = Int32.Parse(Request["rows"].ToString()); } List<WGTotalTableModel> myList = new List<WGTotalTableModel>(); for (int i = 0; i < 50;i++ ) { WGTotalTableModel HModel = new WGTotalTableModel(); HModel.PID = i + 1; HModel.PRJNAME="專案"+(i+1).ToString(); HModel.LANDFOUR = "土地四至" + (i + 1).ToString(); HModel.EARTHCOUNTRY = "地塊" + (i + 1).ToString(); HModel.EARTHTWON = "位置" + (i + 1).ToString(); HModel.Price = i + 2; myList.Add(HModel); } List<WGTotalTableModel> ItemList = myList.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderBy(t => t.PRJNAME).ToList(); model.rows = ItemList; model.total = myList.Count; return Json(model, JsonRequestBehavior.DenyGet); }View Code
public class DataGridWGModel { public List<WGTotalTableModel> rows { get; set; } public Int32 total { get; set; } } public class WGTotalTableModel { public System.Int32 PID { get; set; } public System.String PRJNAME { get; set; } public System.String LANDFOUR { get; set; } public System.String EARTHTWON { get; set; } public System.String EARTHCOUNTRY { get; set; } public System.Int32 Price { get; set; } }View Code
5、Easyui 引用檔案
連結:https://pan.baidu.com/s/1KxL2QeVEbEVHU9UxV6LBWw
提取碼:cwbd