layuiList.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. (function (global) {
  2. var layList = {
  3. table: null,
  4. laydate: null,
  5. layer: null,
  6. form: null,
  7. upload:null,
  8. tableIns: null,
  9. laypage:null,
  10. layedit:null,
  11. element:null,
  12. tree:null,
  13. elemOdj:[],
  14. boxids:'ids',
  15. odj:'',
  16. initialize: function () {
  17. var that = this;
  18. layui.use(['form','table', 'laydate', 'layer', 'laypage','element','layedit','tree','upload'], function () {
  19. that.form = layui.form;
  20. that.table = layui.table;
  21. that.laydate = layui.laydate;
  22. that.layer = layui.layer;
  23. that.laypage =layui.laypage;
  24. that.element = layui.element;
  25. that.layedit=layui.layedit;
  26. that.tree=layui.tree;
  27. that.upload=layui.upload;
  28. })
  29. $('.layui-input-block').each(function () {
  30. var name = $(this).data('type');
  31. if ($(this).data('type') != undefined) {
  32. var input = $(this).find('input[name="' + name + '"]');
  33. $(this).children('button').each(function () {
  34. $(this).on('click', function () {
  35. $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
  36. input.val($(this).data('value'));
  37. })
  38. });
  39. }
  40. });
  41. },
  42. inintclass: function ($names) {
  43. var that=this;
  44. $names.find('button').each(function() {
  45. var type = $names.data('type');
  46. $(this).on('click',function () {
  47. var value=$(this).data('value');
  48. $(this).addClass('layui-btn-radius').siblings().removeClass('layui-btn-primary');
  49. $names.find('input[name="'+type+'"]').val(value);
  50. that.reload({[type]:value})
  51. })
  52. });
  53. }
  54. };
  55. //ajax POST
  56. layList.basePost = function (url, data, successCallback, errorCallback,headers) {
  57. var that = this;
  58. if(headers==undefined) headers=this.headers();
  59. $.ajax({
  60. headers: headers,
  61. url: url,
  62. data: data,
  63. type: 'post',
  64. dataType: 'json',
  65. success: function (rem) {
  66. if (rem.code == 200 || rem.status == 200)
  67. successCallback && successCallback(rem);
  68. else
  69. errorCallback && errorCallback(rem);
  70. },
  71. error: function (err) {
  72. errorCallback && errorCallback(err);
  73. that.msg('服务器异常');
  74. }
  75. })
  76. }
  77. //ajax GET
  78. layList.baseGet = function (url,successCallback, errorCallback,headers) {
  79. var that = this;
  80. if(headers==undefined) headers=this.headers();
  81. $.ajax({
  82. headers: headers,
  83. url: url,
  84. type: 'get',
  85. dataType: 'json',
  86. success: function (rem) {
  87. if (rem.code == 200 || rem.status == 200)
  88. successCallback && successCallback(rem);
  89. else
  90. errorCallback && errorCallback(rem);
  91. },
  92. error: function (err) {
  93. errorCallback && errorCallback(err);
  94. that.msg('服务器异常');
  95. }
  96. });
  97. };
  98. //设置headers头
  99. layList.headers = function () {
  100. return {
  101. 'Content-Type': 'application/x-www-form-urlencoded',
  102. 'X-Requested-With': 'XMLHttpRequest',
  103. };
  104. };
  105. //初始化 layui table
  106. layList.tableList = function (odj, url, data, limit, size,boxids,is_tables) {
  107. var limit = limit || 20, size = size || 'sm', $data = [], that = this,boxids=boxids || this.boxids;
  108. switch (typeof data) {
  109. case 'object':
  110. $data = data;
  111. break;
  112. case "function":
  113. data && ($data = data());
  114. break;
  115. }
  116. if(is_tables!=true) this.odj=odj;
  117. if(that.elemOdj[odj]==undefined) that.elemOdj[odj]=odj;
  118. var elemOdj=that.elemOdj[this.odj];
  119. that.tableIns = that.table.render({
  120. id:boxids,
  121. elem: '#' +elemOdj,
  122. url: url,
  123. page: true,
  124. limit: limit,
  125. cols: [$data]
  126. });
  127. return that.tableIns;
  128. };
  129. //获得url PHP获取当前模块 和控制器
  130. layList.Url = function (opt) {
  131. var m = opt.m || window.module, c = opt.c || window.controlle, a = opt.a || 'index', q = opt.q || '',
  132. p = opt.p || {}, params = '';
  133. params = Object.keys(p).map(function (key) {
  134. return key + '/' + p[key];
  135. }).join('/');
  136. gets = Object.keys(q).map(function (key) {
  137. return key+'='+ q[key];
  138. }).join('&');
  139. return '/' + m + '/' + c + '/' + a + (params == '' ? '' : '/' + params) + (gets == '' ? '' : '?' + gets);
  140. };
  141. layList.U = function(obj){
  142. return this.Url(obj);
  143. }
  144. //表单重构 where 搜索条件 join,page 是否返回到第一页,tableIns 多table时 this.tableList 返回的参数
  145. layList.reload = function (where, page, tableIns,initSort) {
  146. var whereOdJ = {where: where || {}};
  147. if (initSort) whereOdJ.initSort = initSort;
  148. if (page == true) whereOdJ.page = {curr: 1};
  149. if(typeof tableIns=='Object'){
  150. tableIns.reload(whereOdJ);
  151. }else{
  152. this.tableIns.reload(whereOdJ);
  153. }
  154. }
  155. //获取排序字符串
  156. layList.order = function (type, filde) {
  157. switch (type) {
  158. case 'desc':
  159. return filde + '-desc';
  160. break;
  161. case 'asc':
  162. return filde + '-asc';
  163. break;
  164. case null:
  165. return '';
  166. break;
  167. }
  168. }
  169. //监听列表
  170. layList.tool = function (EventFn, fieldStr,odj) {
  171. var that = this;
  172. // var elemOdj=elemOdj || that.elemOdj
  173. var elemOdj=that.elemOdj[odj || this.odj];
  174. this.table.on('tool(' + elemOdj + ')', function (obj) {
  175. var data = obj.data, layEvent = obj.event;
  176. if (typeof EventFn == 'function') {
  177. EventFn(layEvent, data,obj);
  178. } else if (EventFn && (typeof fieldStr == 'function')) {
  179. switch (layEvent) {
  180. case EventFn:
  181. fieldStr(data);
  182. break;
  183. default:
  184. console.log('暂未监听到事件');
  185. break
  186. }
  187. }
  188. });
  189. }
  190. //监听排序 EventFn 需要监听的值 || 函数,page 是否回到第1页,tableIns 多table时 this.tableList 返回的参数
  191. layList.sort = function (EventFn, page,tableIns,odj) {
  192. var that = this;
  193. // var elemOdj=elemOdj || that.elemOdj;
  194. var elemOdj=that.elemOdj[odj || this.odj];
  195. this.table.on('sort(' + elemOdj + ')', function (obj) {
  196. var layEvent = obj.field;
  197. var type = obj.type;
  198. if (typeof EventFn == 'function') {
  199. EventFn(obj);
  200. } else if (typeof EventFn=='object'){
  201. for(value in EventFn){
  202. switch (layEvent) {
  203. case EventFn[value]:
  204. if (page == true)
  205. that.reload({order: that.order(type, EventFn[value])}, true, tableIns, obj);
  206. else
  207. that.reload({order: that.order(type, EventFn[value])}, null, tableIns, obj);
  208. continue;
  209. }
  210. }
  211. }else if(EventFn){
  212. switch (layEvent) {
  213. case EventFn:
  214. if (page == true)
  215. that.reload({order: that.order(type, EventFn)}, true, tableIns, obj);
  216. else
  217. that.reload({order: that.order(type, EventFn)}, null, tableIns, obj);
  218. break;
  219. default:
  220. console.log('暂未监听到事件');
  221. break
  222. }
  223. }
  224. });
  225. }
  226. layList.msg = function (msg,fun) {
  227. var msg = msg || '未知错误';
  228. try {
  229. return this.layer.msg(msg,fun);
  230. } catch (e) {}
  231. }
  232. //时间选择器
  233. layList.date = function (IdName) {
  234. if (typeof IdName == 'string' && $('#' + IdName).length == 0) return console.info('并没有找到此元素');
  235. var json = typeof IdName == 'object' ? IdName : {elem: '#' + IdName, range: true};
  236. this.laydate.render(json);
  237. }
  238. //监听复选框
  239. layList.switch = function (switchname, successFn) {
  240. this.form.on('switch(' + switchname + ')', function (obj) {
  241. successFn && successFn(obj, this.value, this.name);
  242. });
  243. }
  244. //监听select
  245. layList.select = function (switchname, successFn) {
  246. this.form.on('select(' + switchname + ')', function (obj) {
  247. successFn && successFn(obj, this.value, this.name);
  248. });
  249. }
  250. //获取复选框选中的数组
  251. layList.getCheckData = function (boxids) {
  252. var boxids = boxids || this.boxids;
  253. return this.table.checkStatus(boxids).data;
  254. }
  255. //搜索
  256. layList.search = function (btnname, successFn) {
  257. var name = typeof btnname == 'string' ? btnname : '';
  258. var that = this;
  259. if (name == '') return false;
  260. this.form.on('submit(' + btnname + ')', function (data) {
  261. if (typeof successFn == "function") {
  262. successFn(data.field);
  263. } else {
  264. that.reload(data.field);
  265. }
  266. return false;
  267. })
  268. }
  269. layList.codeType = function (name, type) {
  270. switch (name) {
  271. // case :
  272. }
  273. }
  274. layList.edit=function(name,successFn,odj){
  275. var that = this;
  276. var elemOdj=that.elemOdj[odj || this.odj];
  277. this.table.on('edit('+elemOdj+')',function (obj) {
  278. var value = obj.value //得到修改后的值
  279. ,data = obj.data //得到所在行所有键值
  280. ,field = obj.field; //得到字段
  281. if (typeof name == "function") {
  282. name && name(obj);
  283. }else{
  284. switch (field){
  285. case name:
  286. successFn && successFn(obj);
  287. break;
  288. default:
  289. console.log('未检测到指定字段'+name);
  290. break;
  291. }
  292. }
  293. });
  294. }
  295. //页面有多个table请用此函数包裹起来
  296. layList.tables=function(odj,data,value,successFn){
  297. var url=data.url || '',limit=data.limit || 20,size=data.size || 'lg',that=this;
  298. this.tableList(odj,url,value,limit,size);
  299. }
  300. layList.createModalFrame=function(title,src,opt){
  301. opt === undefined && (opt = {});
  302. var area=[(opt.w || 750)+'px', (opt.h || 680)+'px'];
  303. // $(window).resize(function() {
  304. // $('.layui-layer-iframe').css('top',);
  305. // });
  306. // $('.layui-layer-iframe').on('mousedown',function () {
  307. // console.log($(document).height());
  308. // })
  309. return this.layer.open({
  310. type: 2,
  311. title:title,
  312. area: area,
  313. fixed: false, //不固定
  314. maxmin: true,
  315. moveOut:false,//true 可以拖出窗外 false 只能在窗内拖
  316. anim:5,//出场动画 isOutAnim bool 关闭动画
  317. offset:'auto',//['100px','100px'],//'auto',//初始位置 ['100px','100px'] t[ 上 左]
  318. shade:0,//遮罩
  319. resize:true,//是否允许拉伸
  320. content: src,//内容
  321. move:'.layui-layer-title',// 默认".layui-layer-title",// 触发拖动的元素
  322. moveEnd:function(){//拖动之后回调
  323. console.log(this);
  324. }
  325. });
  326. };
  327. //提取主键
  328. Array.prototype.getIds = function (field) {
  329. var ids = [];
  330. $.each(this, function (name, value) {
  331. if (value[field] != undefined) ids.push(value[field]);
  332. });
  333. return ids;
  334. }
  335. //初始化layui
  336. layList.initialize();
  337. global.layList = layList;
  338. return layList;
  339. }(this));