layuiList.js 14 KB

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