/**
|
* date:
|
* author: neeler
|
*/
|
|
//test data start
|
// var data_getCompanyList = Mock.mock('/company/getList.htm', {code: 0, 'list|5-20': [{id: 'id', companyNo: 'companyNo', note: 'note', name: 'name'}], pages: 12})
|
// var data_getList = Mock.mock('/user/getList.htm', {code: 0, 'list|5-20': [{companyName: 'companyName', id: 'id',name: 'name',pinyin: 'pinyin',headpinyin: 'headpinyin',sex: '男',phone: 'phone',state: false,username: 'username',companyNo: 'companyNo',}], pages: 12})
|
// var data_getCompanyList = Mock.mock('/basicconfig/materielType/getList.htm', {code: 0, 'list|5': [{id: 'id', name: 'name', note: 'note', pinyin: 'pinyin', jianpin: 'jianpin', helpcode: 'helpcode'}], pages: 12})
|
// var data_doDel = Mock.mock('/user/delItem.htm', {code: 0, errmsg: '不能删除!'})
|
// var data_doSave = Mock.mock('/user/saveItem.htm', {code: 0, errmsg: '不能删除!'})
|
// var data_doValid = Mock.mock('/user/saveState.htm', {code: 0, item: true})
|
//test data end
|
|
var TYPE = ['男' ,'女' ]
|
var UTYPE = [{key: '普通用户', value: 'worker'}, {key: '管理员', value: 'admin'}]
|
|
window.I = {
|
PZ: 30,
|
keyword: $('#keyword'),
|
searchBtn: $('#searchBtn'),
|
pageValue: !!$('#p').val() ? $('#p').val() - 1 : 0,
|
keywordValue: $('#k').val(),
|
newObj: $('#newObj'),
|
modal: $('#modal'),
|
save: $('#save')
|
}
|
|
var Obj = {
|
id: 0,
|
name: '',
|
sex: '',
|
phone: '',
|
state: true,
|
username: '',
|
password: '',
|
companyNo: '',
|
utype: ''
|
}
|
|
var ViewModel = function() {
|
var self = this;
|
self.tList = ko.observableArray();
|
self.mtid = ko.observable();
|
self.o = ko.observable(ko.mapping.fromJS(Obj));
|
|
self.keyword = ko.observable(I.keywordValue);
|
self.list = ko.observableArray();
|
|
self.edit = function(item) {
|
doEdit(item);
|
}
|
self.del = function(item) {
|
delConfirm(item);
|
}
|
|
self.change = function() {
|
doOpen();
|
}
|
self.mtvalid=function(item){
|
doValid(item);
|
}
|
|
self.tyList = ko.observableArray(TYPE);
|
self.utypeList = ko.observableArray(UTYPE);
|
}
|
|
$(function () {
|
window.vm = new ViewModel();
|
ko.applyBindings(vm);
|
doSearch();
|
autoHeight();
|
getCompanyList();
|
pageInit();
|
});
|
|
function pageInit() {
|
$('.table-responsive').scroll(function() {
|
$('.th').css('top', $(this).scrollTop());
|
})
|
I.save.click(function(event) {
|
if (valid()) {
|
doSave();
|
}
|
});
|
I.newObj.click(function(event) {
|
vm.o(ko.mapping.fromJS(Obj));
|
// getNewNo();
|
I.modal.modal('show');
|
});
|
I.searchBtn.click(function(event) {
|
doOpen();
|
});
|
I.modal.on('hidden.bs.modal', function (e) {
|
clearError();
|
});
|
I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) {
|
if (event.keyCode == 13) {
|
doOpen();
|
}
|
});
|
}
|
|
function doOpen() {
|
doSearch();
|
}
|
|
function doSearch() {
|
getList(vm.keyword(), vm.mtid(), I.pageValue, I.PZ, 1);
|
}
|
|
function getList(keyword, companyNo, page, pagesize, pages) {
|
I._page = page;
|
$.post('/user/getList.htm', {
|
searchword: keyword,
|
companyNo: companyNo,
|
page: page,
|
pagesize: pagesize,
|
pages: pages
|
}, function(data, textStatus, xhr) {
|
vm.list.removeAll();
|
if (data.code >= 1) {
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
return;
|
}
|
if (data.code == 0) {
|
if (isList(data.list)) {
|
$.each(data.list, function(index, val) {
|
val.state=ko.observable(val.state);
|
val.password = '';
|
vm.list.push(val);
|
});
|
$('tbody tr').click(function (e) {
|
$('tbody tr').removeClass('info');
|
$(this).addClass('info');
|
});
|
}
|
if (!!pages) {
|
pages = data.pages;
|
$('#pagdiv').unbind('page').empty();
|
if (pages > 1) {
|
$('#pagdiv').bootpag({total: pages, maxVisible: 10, page: page + 1}).on('page',function(event,num){
|
getList(keyword, companyNo, num - 1, pagesize, 0);
|
});
|
}
|
}
|
return;
|
}
|
}, 'json');
|
}
|
|
function valid(){
|
var isValid = false;
|
var iF = $('#form');
|
iF.validate({
|
rules: {
|
'name': {
|
required: true
|
},
|
'phone': {
|
required: true,
|
isPhone: true
|
}
|
},
|
messages: {
|
'name': {
|
required: '不能为空!'
|
},
|
'phone': {
|
required: '不能为空!',
|
isPhone: '手机号不正确!'
|
}
|
},
|
errorPlacement: function (err, element) {
|
var p = element.parents('.form-group').eq(0);
|
var d = p.find('.text-error').eq(0);
|
d.css('color', '#D9534F');
|
err.appendTo(d);
|
}
|
})
|
isValid = iF.valid();
|
return isValid;
|
}
|
|
function doEdit(item) {
|
if(isFunction(item.valid)){
|
item.valid=item.valid();
|
}
|
vm.o(ko.mapping.fromJS(item));
|
I.modal.modal('show');
|
}
|
|
function delConfirm(item) {
|
$("#modalContent").html('确定要删除【' + item.name + '】吗?');
|
$("#dialog-confirm").removeClass('hide').dialog({
|
resizable: false,
|
width: '320',
|
modal: true,
|
title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
|
title_html: true,
|
position: { my: "center", at: "center", of: window },
|
buttons: [
|
{
|
html: "<i class='ace-icon fa fa-trash-o bigger-110'></i> 确定",
|
"class" : "btn btn-danger btn-minier",
|
click: function() {
|
$( this ).dialog( "close" );
|
doDel(item);
|
}
|
}
|
,
|
{
|
html: "<i class='ace-icon fa fa-times bigger-110'></i> 取消",
|
"class" : "btn btn-minier",
|
click: function() {
|
$( this ).dialog( "close" );
|
}
|
}
|
]
|
});
|
}
|
|
function doDel(item) {
|
if (!!I.AjaxDelItem) return;
|
I.AjaxDelItem = true;
|
$.post('/user/delItem.htm', {id: item.id}, function(data, textStatus, xhr) {
|
I.AjaxDelItem = false;
|
if (data.code >= 1) {
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
return;
|
}
|
if (data.code == 0) {
|
vm.list.remove(item);
|
parent.showErrmsg('删除成功!');
|
I.modal.modal('hide');
|
return;
|
}
|
}, 'json');
|
}
|
|
function doSave() {
|
if (!I.AjaxSaveItem &&valid()) {
|
I.AjaxSaveItem = true;
|
I.save.prop('disabled', true);
|
var o = ko.mapping.toJS(vm.o());
|
o.state = $('#state').prop('checked');
|
$.post('/user/saveItem.htm', o, function(data, textStatus, xhr) {
|
I.AjaxSaveItem = false;
|
I.save.prop('disabled', false);
|
if (data.code >= 1) {
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
return;
|
}
|
if (data.code == 0) {
|
parent.showErrmsg('保存成功!');
|
I.modal.modal('hide');
|
getList(vm.keyword(), vm.mtid(), I._page, I.PZ, 1);
|
return;
|
}
|
}, 'json');
|
}
|
}
|
|
function clearError() {
|
$('#form input').removeClass('error');
|
$('#form .text-error').html('');
|
}
|
|
function getCompanyList() {
|
$.post('/company/getList.htm', null, function(data, textStatus, xhr) {
|
vm.tList.removeAll();
|
if (data.code >= 1) {
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
return;
|
}
|
if (data.code == 0) {
|
if (isList(data.list)) {
|
$.each(data.list, function(index, val) {
|
vm.tList.push(val);
|
});
|
}
|
}
|
}, 'json');
|
}
|
|
|
function doValid(item){
|
var state=true;
|
if(isFunction(item.state)){
|
state=item.state();
|
}else{
|
state=item.state;
|
}
|
jQuery.post('/user/saveState.htm', {id: item.id, state: !state}, function(data, textStatus, xhr) {
|
//optional stuff to do after success
|
if(data.code==0){
|
item.state(data.item);
|
}else{
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
}
|
},'json');
|
}
|
|
function getNewNo() {
|
$.post("/basicconfig/prefix/getNewNo.htm", {type: 'materiel'},
|
function (data, textStatus, jqXHR) {
|
if (data.code >= 1) {
|
//parent.showErrmsg(data.errmsg);
|
swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
|
return;
|
}
|
if (data.code == 0) {
|
// vm.o().visitNo(data.number);
|
$('#materielNo').val(data.number);
|
return;
|
}
|
},
|
"json"
|
);
|
}
|
|
function autoHeight() {
|
$('.page-content').css('min-height', $(parent.window).height() - 117);
|
$('.table-responsive').css('height', $(parent.window).height() - 217);
|
}
|