MVC bootstrap-table顯示資料時顯示No matching records found
問題:bootstrap-table載入資料不顯示
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script>
$(function () {
InitMainTable();
document.onkeydown = function (e) {
var ev = document.all ? window.event : e;
if (ev.keyCode == 13) {// 如(ev.ctrlKey && ev.keyCode==13)為ctrl+Center 觸發
$("#table").bootstrapTable('refresh');
}
}
$("#rearch").click(function () {
$("#table").bootstrapTable('refresh');
});
});
function InitMainTable() {
//先銷燬表格
$("#table").bootstrapTable('destroy');
$("#table").bootstrapTable({
method: "get"
, url: "/Type/DataList"
, striped: true, //表格顯示條紋
pagination: true,// 是否顯示分頁
pageSize: 10, //每頁顯示條數
pageNumber: 1,//當前第幾頁
search: true, //顯示搜尋框
buttonsAlign: "right", //按鈕對齊方式
pageList: [5, 10, 15, 20, 25],
showColumns: true,
showRefresh: true, //是否顯示重新整理按鈕
sidePagination: "server", //表示從服務端獲取資料
queryParamsType: "undefined", //定義引數型別
queryParams: function (params) {
//console.log(params);
var param = {
keyword: $("#search").val(),//搜尋
pageIndex: params.pageNumber,
pageSize: params.pageSize
};
return param;
},
columns: [{
field: "TypeName"
, title: "名稱"
, sortable: true
}, {
field: "Typevalue"
, title: "型別值"
, sortable: true
}, {
field: "FullPath"
, title: "表名"
, sortable: true
}, {
field: "StateName"
, title: "是否顯示"
, sortable: true
, formatter: function (value, row, index) {
var isdisplay = row["StateName"];
if (isdisplay == "6f9619ff-8b86-d011-b42d-34c04fc964ff") {
isdisplay = ' <span class="label label-lg label-yellow arrowed-in">是</span>';
} else {
isdisplay = '<span class="label label-lg label-purple arrowed">否</span>';
}
return isdisplay;
}
}, {
title: '操作',
align: 'center',
width: 230,
formatter: function (value, row, index) {
var icon = '<button class="btn btn-xs btn-primary" onclick="Update(\'' + row.TyId + '\')"><i class="icon-edit bigger-120">編輯</i></button> '
icon += '<button class="btn btn-xs btn-danger" onclick="Del(\'' + row.TyId + '\')"><i class="icon-trash bigger-120">刪除</i></button>';
return icon;
}
}],
onLoadSuccess: function (data) {
console.log(data);
},
onLoadError: function () {
alert("資料載入失敗!");
}
});
}
//刪除
function Del(tyid) {
if (confirm("你確定要刪除嗎?")) {
$.post("/Type/Reovme", { tyid: tyid }, function (data) {
if (data.code == 200) {
window.location.reload();
} else {
layer.msg(data.msg, { icon: 5 });
}
});
}
}
function Update(typeid) {
window.location.href = "/Type/AddUpdate?typeId="+typeid;
}
function Add() {
window.location.href = "/Type/AddUpdate";
}
</script>
<div class="input-group col-md-3" style="margin-top:0px">
<input type="text" class="form-control"placeholder="請輸入欄位名" id="search" / >
<span class="input-group-btn">
<button class="btn btn-sm btn-search" id="rearch">
<i class="icon-search bigger-110"></i>
<span class="bigger-110 no-text-shadow">查詢</span>
</button>
<button class="btn btn-sm btn-success" onclick="Add()"><i class="icon-pencil bigger-110">新增</i></button>
</span>
</div>
<table class="table table-hover table-striped" border="0" id="table" style="border-width: 0px; border-collapse: collapse;">
</table>
程式碼是沒有問題的但是就是一直不顯示,載入的時候是有資料輸出,當時找了好久。一直都找不出來,後來發現控制器返回json格式有問題。
//控制器
/// <summary>
/// 查詢角色表
/// </summary>
/// <returns></returns>
public ActionResult getRoleAll(int pageIndex, int pageSize)
{
ISys_RoleBLL irb = new Sys_RoleBLL();
List<Sys_Role> list = irb.getRoleAll();
var data = (from role in list
select new
{
role.RoId,
role.EnCode,
role.FullName,
DeleteMark=Convert.ToInt32(role.DeleteMark).ToString()=="1"?"可用":"不可用",
}).Skip((pageIndex-1)*pageSize).Take(pageSize);
var resut = new { rows = data }; 當時沒有加上這句話,直接返回data資料,導致頁面有資料輸出,table表格無資料。這裡需要返回匿名型別的資料
return Json(resut, JsonRequestBehavior.AllowGet);
}