$(document).ready(function () {
initRoleSelect();
initUserTable();
});
function initRoleSelect() {
$.ajax({
url: "/Admin/GetAllRoles",
method: "GET",
headers: { "Content-Type": "application/json" },
async: true,
success: function (result) {
if (result.code === 0) {
for (var i = 0; i < result.data.length; i++) {
var id = result.data[i].id;
var value = result.data[i].name;
$("#Role").append("");
}
} else if (result.code === 1) {
alert(result.message);
}
}
});
}
var userTable;
var size;
var number;
function initUserTable() {
var queryUrl = '/Admin/GetUsers';
userTable = $("#user_table").bootstrapTable({
url: queryUrl,
columns: [
{ field: "id", title: "ID", visible: false },
{ field: "jobNo", title: "工号"},
{ field: "name", title: "名称" },
{ field: "roleId", title: "角色Id", visible: false },
{ field: "roleName", title: "角色名称" },
{ field: "createTime", title: "创建时间" },
{
title: '操作',
field: "id",
formatter: operation,
width: 200
}
],
pagination: true,
pageNumber: 1,
pageSize: size,
pageList: [10, 25, 50, 100],
smartDisplay: false,
sidePagination: "server",
queryParams: function (params) {
var temp = {
pageNumber: (params.offset / params.limit) + 1,
pageSize: params.limit,
keyword: $("#keyword").val()
};
number = temp.pageNumber;
return temp;
}
});
}
function operation(value, row, index) {
var htm =
" 编辑" +
" 删除";
return htm;
}
function modelCreateUser() {
$("#Id").val("");
$("#FormJobNo")[0].disabled = false;
$("#modal_user_add").modal("show");
}
function refreshUserTable() {
userTable.bootstrapTable("refreshOptions", { pageNubmer: 1 });
}
function selectPageUserTable() {
userTable.bootstrapTable("selectPage", number);
}
function saveUser() {
var id = $("#Id").val();
var url;
if (id === null || id === "") {
url = "/Admin/CreateUser";
} else {
url = "/Admin/UpdateUser/" + id;
}
$.ajax({
url: url,
method: "POST",
headers: { "Content-Type": "application/json" },
async: true,
data: JSON.stringify({
"jobNo": $("#FormJobNo").val(),
"name": $("#Name").val(),
"roleId": $("#Role").val(),
}),
success: function (result) {
if (result.code === 0) {
$("#modal_user_add").modal("hide");
if (id === null || id === "") {
refreshUserTable();
} else {
selectPageUserTable();
}
} else if (result.code === 1) {
alert(result.message);
}
}
});
}
function modelUpdateUser(id) {
$("#Id").val(id);
$("#FormJobNo")[0].disabled = true;
$.ajax({
url: "/Admin/User/" + id,
method: "GET",
headers: { "Content-Type": "application/json" },
async: true,
success: function (result) {
if (result.code === 0) {
$("#FormJobNo").val(result.data.jobNo);
$("#Name").val(result.data.name);
$("#Role").val(result.data.roleId);
} else if (result.code === 1) {
alert(result.message);
}
}
});
$("#modal_user_add").modal("show");
}
function deleteUser(id) {
$.ajax({
url: "/Admin/DeleteUser/" + id,
method: "POST",
headers: { "Content-Type": "application/json" },
async: true,
success: function (result) {
if (result.code === 0) {
if (id === null || id === "") {
refreshUserTable();
} else {
selectPageUserTable();
}
} else if (result.code === 1) {
alert(result.message);
}
}
});
}