说明
本篇记录使用bootstrap-table
的formatter
列选项来控制数据的显示。
这个formatter
也就是对获取到的数据进行格式化的意思。
查看官网给的api,看下图,这个列选项是Function
类型,它有四个参数,分别是value
(data中分配到的值),row
(所在行,可以访问行内其他field
的值),index
(索引),field
(对应的field)
如下图所示 根据用户不同权限显示不同的操作按钮 ,
- 管理员显示Modify和Detail按钮 ,
- 普通用户只显示Detail按钮。
管理员
普通用户
设置
在需要显示的field
下设置formatter
和events
,
添加按钮函数
添加按钮函数和事件要放在初始化之前。
在初始化里写事件太臃肿,把它分离出来,并把参数传递给它。
$.inArray(("admin"), right)>=0
判断某字符是否存在与某数组中,存在返回索引值,不存在则返回-1
function addButton(value, row, index, field) {
var right = getRight().split(',');
if ($.inArray(("admin"), right)>=0) {
return '<button type="button" id="modify" class="btn btn-success">Modify</button><button type="button" id="detail" class="btn btn-info">Detail</button>';
} else {
return '<button type="button" id="detail" class="btn btn-info">Detail</button>';
}
;
}
按钮事件
'click #detail'
表示id="detail"
的按钮点击事件
//获取事件
window.operateEvents = {
'click #detail': function (e, value, row, index) {
window.location.href = "/Home/viewTeam?fileid=" + row.FileID + "";
},
'click #modify': function (e, value, row, index) {
window.location.href = "/Home/team?fileid=" + row.FileID + "";
}
};
比较简单
接下来记录使用filter来设置cookie
设置Cookie
global.asax
文件中有这么一句, FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
表示注册过滤器。
现在添加一个文件夹Filter,在里面新建LocalizationAttribute
类,继承于ActionFilterAttribute
using ajax_2.App_Code;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
namespace e_form.filter
{
public class LocalizationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
/// 把用户信息保存进cookie
HttpCookie _cookie = new HttpCookie("user");
_cookie.Expires = DateTime.Now.AddYears(1);
string badge = Employee.GetBadge();
_cookie["badge"] = badge;
_cookie["name"] =Employee.getNameByBadge(badge);
_cookie["right"] = Employee.getRight(badge);
filterContext.HttpContext.Response.SetCookie(_cookie);
base.OnActionExecuting(filterContext);
}
}
}
在FilterConfig中添加以下
获取Cookie
// 获取指定名称的cookie
function getCookie(name) {
var strcookie = document.cookie;//获取cookie字符串
var arrcookie = strcookie.split("; ");//分割
//遍历匹配
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].substring(arrcookie[i].indexOf('=') + 1);
if (arrcookie[i].substring(0, arrcookie[i].indexOf('=')) == name) {
return arr;
}
}
return "";
}
获取之后再使用split去分割,可以F12查看Application存储的cookie是否已经存储进来了,
以上。
更多推荐
Bootstrap-table formatter
发布评论