layuiList.js 13 KB

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