1
0

jquery-extend.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. (function ($) {
  2. /*********************************对jQuery类型原型扩展********************************************/
  3. jQuery.extend($.prototype, {
  4. Grid: function (options) {
  5. //分页Id
  6. var pager = this.attr('id') + 'Pager';
  7. this.after('<div id="' + pager + '"></div>');
  8. this.defaults = {
  9. width: 1000,
  10. styleUI: 'Bootstrap',
  11. datatype: "json",
  12. viewrecords: true,
  13. height: 550,
  14. rowNum: 10,
  15. rowList: [10, 30, 50],
  16. rownumbers: true,
  17. rownumWidth: 25,
  18. autowidth: true,
  19. multiselect: true,
  20. jsonReader: {
  21. root: "page.list",
  22. page: "page.currPage",
  23. total: "page.totalPage",
  24. records: "page.totalCount"
  25. },
  26. prmNames: {
  27. page: "page",
  28. rows: "limit",
  29. order: "order"
  30. },
  31. pager: "#" + pager
  32. };
  33. var param = $.extend(this.defaults, options);
  34. this.jqGrid(param);
  35. }
  36. });
  37. /*********************************对Date类型原型扩展********************************************/
  38. jQuery.extend(Date.prototype, {
  39. /**
  40. * 格式化日期 默认格式:'yyyy-MM-dd hh:mm:ss'
  41. * @param fmt
  42. * @returns {*}
  43. */
  44. dateFormat: function (fmt) {
  45. if (!fmt) {
  46. fmt = 'yyyy-MM-dd hh:mm:ss';
  47. }
  48. var o = {
  49. "M+": this.getMonth() + 1, //月份
  50. "d+": this.getDate(), //日
  51. "h+": this.getHours(), //小时
  52. "m+": this.getMinutes(), //分
  53. "s+": this.getSeconds(), //秒
  54. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  55. "S": this.getMilliseconds() //毫秒
  56. };
  57. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  58. for (var k in o)
  59. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  60. return fmt;
  61. }
  62. });
  63. /*********************************对String类型原型扩展********************************************/
  64. /**
  65. * 功能描述:对String 类型原型扩展
  66. *
  67. * @author Scott
  68. */
  69. jQuery.extend(String.prototype, {
  70. /**
  71. * String转Date(兼容IE)
  72. *
  73. * eg:"2017-3-24 08:30:30".toDate();
  74. *
  75. * @returns {Date}
  76. */
  77. toDate: function () {
  78. return new Date(this.replace("-", "/").replace("-", "/"));
  79. },
  80. /**
  81. * 在字符串末尾追加字符串
  82. *
  83. * @param {String} str 要追加的字符串
  84. *
  85. * @returns {string}
  86. */
  87. append: function (str) {
  88. return this.concat(str);
  89. },
  90. /**
  91. * Truncate a string and add an ellipsis ('...') to the end if it
  92. * exceeds the specified length.
  93. *
  94. * eg:'这是一个测试案例'.ellipsis(7); return "这是一个..."
  95. *
  96. * @param {Number} length The maximum length to allow before
  97. * truncating.
  98. * @param {Boolean} [word=false] 'true' to try to find a common word
  99. * break.
  100. * @return {String} The converted text.
  101. */
  102. ellipsis: function (length, word) {
  103. var value = this;
  104. if (value && value.length > length) {
  105. if (word) {
  106. var vs = value.substr(0, length - 2),
  107. index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
  108. if (index !== -1 && index >= (length - 15)) {
  109. return vs.substr(0, index) + "...";
  110. }
  111. }
  112. return value.substr(0, length - 3) + "...";
  113. }
  114. return value;
  115. },
  116. /**
  117. * 判断字符串是否以指定内容结尾
  118. *
  119. * @returns {boolean}
  120. */
  121. endsWith: function () {
  122. return this.substr(this.length - arguments[0].length) == arguments[0];
  123. },
  124. /**
  125. * 比较两个字符串是否相等,不区分大小写
  126. *
  127. * @param {String} str
  128. *
  129. * @returns {boolean}
  130. */
  131. equalIgnoreCase: function (str) {
  132. var temp1 = this.toLowerCase();
  133. var temp2 = str.toLowerCase();
  134. return temp1.equal(temp2);
  135. },
  136. /**
  137. * 插入字符串,索引无效将直接追加到字符串的末尾
  138. *
  139. * @param {String} value 插入的值
  140. * @param {Number} index 插入的索引
  141. *
  142. * @returns {String}
  143. */
  144. insert: function (value, index) {
  145. var s = this;
  146. if (!s) {
  147. return value;
  148. }
  149. if (!value) {
  150. return s;
  151. }
  152. var len = s.length;
  153. if (!index && index !== 0) {
  154. index = len;
  155. }
  156. if (index < 0) {
  157. index *= -1;
  158. if (index >= len) {
  159. // negative overflow, insert at start
  160. index = 0;
  161. } else {
  162. index = len - index;
  163. }
  164. }
  165. if (index === 0) {
  166. s = value + s;
  167. } else if (index >= s.length) {
  168. s += value;
  169. } else {
  170. s = s.substr(0, index) + value + s.substr(index);
  171. }
  172. return s;
  173. },
  174. /**
  175. * 判断字符串是否为Null或者为空字符串。
  176. *
  177. * @returns {boolean}
  178. */
  179. isNullOrEmpty: function () {
  180. return this === undefined || this === null || this == "" || this == "null";
  181. },
  182. /**
  183. * 判断字符串是否为Null或者为空字符串或者全是空格。
  184. *
  185. * @returns {boolean}
  186. */
  187. isEmpty: function () {
  188. return this.isNullOrEmpty() || this.trim().isNullOrEmpty();
  189. },
  190. /**
  191. * 判断字符串是否包含指定的内容。
  192. *
  193. * @param {String} val 指定内容
  194. *
  195. * @returns {boolean}
  196. */
  197. isContains: function (val) {
  198. return this.indexOf(val) > -1;
  199. },
  200. /**
  201. * 从左截取指定长度的字串
  202. *
  203. * @param {Number} n
  204. *
  205. * @returns {String}
  206. */
  207. left: function (n) {
  208. return this.slice(0, n);
  209. },
  210. /**
  211. * 左填充 eg:"12".leftPad(5, '0'); return "00012"
  212. *
  213. * @param {Number} size 整个字符串长度
  214. * @param {String} character 要填充的字符串
  215. *
  216. * @returns {string}
  217. */
  218. leftPad: function (size, character) {
  219. var result = String(this);
  220. character = character || " ";
  221. while (result.length < size) {
  222. result = character + result;
  223. }
  224. return result;
  225. },
  226. /**
  227. * 去除字符串左边的空格。
  228. *
  229. * @returns {*|XML|string|void}
  230. */
  231. ltrim: function () {
  232. return this.replace(/(^\s*)/g, "");
  233. },
  234. /**
  235. * Returns a string with a specified number of repetitions a given
  236. * string pattern. The pattern be separated by a different string.
  237. *
  238. * '---'.repeat(4); // = '------------' '--'.repeat(3, '/'); // =
  239. * '--/--/--'
  240. *
  241. * @param {Number} count The number of times to repeat the pattern
  242. * (may be 0).
  243. * @param {String} sep An option string to separate each pattern.
  244. */
  245. repeat: function (count, sep) {
  246. var pattern = this;
  247. if (count < 1) {
  248. count = 0;
  249. }
  250. for (var buf = [], i = count; i--;) {
  251. buf.push(pattern);
  252. }
  253. return buf.join(sep || '');
  254. },
  255. /**
  256. * 逆序
  257. *
  258. * @returns {string}
  259. */
  260. reverse: function () {
  261. return this.split("").reverse().join("");
  262. },
  263. /**
  264. * 从右截取指定长度的字串
  265. *
  266. * @param {Number} n
  267. *
  268. * @returns {String}
  269. */
  270. right: function (n) {
  271. return this.slice(this.length - n);
  272. },
  273. /**
  274. * 去除字符串右边的空格。
  275. *
  276. * @returns {*|XML|string|void}
  277. */
  278. rtrim: function () {
  279. return this.replace(/(\s*$)/g, "");
  280. },
  281. /**
  282. * 判断字符串是否以指定内容开头
  283. *
  284. * @returns {boolean}
  285. */
  286. startsWith: function () {
  287. return this.substr(0, arguments[0].length) == arguments[0];
  288. },
  289. /**
  290. * Utility function that allows you to easily switch a string
  291. * between two alternating values. The passed value is compared to
  292. * the current string, and if they are equal, the other value that
  293. * was passed in is returned. If they are already different, the
  294. * first value passed in is returned. Note that this method returns
  295. * the new value but does not change the current string. //
  296. * alternate sort directions sort.toggle('ASC', 'DESC'); // instead
  297. * of conditional logic: sort = (sort === 'ASC' ? 'DESC' : 'ASC');
  298. *
  299. * @param {String} string The current string.
  300. * @param {String} value The value to compare to the current string.
  301. * @param {String} other The new value to use if the string already
  302. * equals the first value passed in.
  303. * @return {String} The new value.
  304. */
  305. toggle: function (value, other) {
  306. return this.toString() === value ? other : value;
  307. },
  308. /**
  309. * 除去两边空白
  310. *
  311. * @returns {*|XML|string|void}
  312. */
  313. trim: function () {
  314. return this.replace(/(^\s*)|(\s*$)/g, "");
  315. },
  316. /**
  317. * 计算字符串长度(英文占1个字符,中文汉字占2个字符)
  318. *
  319. * @return {int} 字符串所占字符数。如:"你abc" 返回结果就为 5
  320. */
  321. toChar: function () {
  322. var len = 0;
  323. for (var i = 0; i < this.length; i++) {
  324. if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
  325. len += 2;
  326. } else {
  327. len++;
  328. }
  329. }
  330. return len;
  331. }
  332. });
  333. /**
  334. * 功能描述:对Array 类型原型扩展
  335. *
  336. * @author Scott
  337. */
  338. jQuery.extend(Array.prototype, {
  339. /**
  340. * 插入一条记录
  341. *
  342. * @param item
  343. */
  344. add: function (item) {
  345. this.push(item);
  346. },
  347. /**
  348. * 插入数组
  349. *
  350. * @param items
  351. */
  352. addRange: function (items) {
  353. var length = items.length;
  354. if (length != 0) {
  355. for (var index = 0; index < length; index++) {
  356. this.push(items[index]);
  357. }
  358. }
  359. },
  360. /**
  361. * 消空数组元素.
  362. */
  363. clear: function () {
  364. if (this.length > 0) {
  365. this.splice(0, this.length);
  366. }
  367. },
  368. /**
  369. * 删除无效的元素(null/"")
  370. */
  371. removeVoidElement: function () {
  372. for (var i = 0; i < this.length; i++) {
  373. if ("" == this[i] || null == this[i] || "null" == this[i]) {
  374. this.remove(this[i]);
  375. i--;// 移除一个对象,数组长度减1
  376. }
  377. }
  378. },
  379. /**
  380. * 判断是否为空
  381. *
  382. * @returns {boolean}
  383. */
  384. isEmpty: function () {
  385. if (this.length == 0) {
  386. return true;
  387. } else {
  388. return false;
  389. }
  390. },
  391. /**
  392. * 克隆数组对象
  393. *
  394. * @returns {Array}
  395. */
  396. clone: function () {
  397. var clonedArray = [];
  398. var length = this.length;
  399. for (var index = 0; index < length; index++) {
  400. clonedArray[index] = this[index];
  401. }
  402. return clonedArray;
  403. },
  404. /**
  405. * 搜索指定的Object,并返回第一个匹配项从零开始的索引
  406. *
  407. * @param item 要查找的Object对象
  408. *
  409. * @returns {number} 找到返回该元素在数组中的索引,否则返回-1
  410. */
  411. indexOf: function (item) {
  412. var length = this.length;
  413. if (length != 0) {
  414. for (var index = 0; index < length; index++) {
  415. if (this[index] == item) {
  416. return index;
  417. }
  418. }
  419. }
  420. return -1;
  421. },
  422. /**
  423. * 插入数组元素
  424. *
  425. * @param index 插入元素的下标.
  426. * @param item
  427. */
  428. insert: function (index, item) {
  429. this.splice(index, 0, item);
  430. },
  431. /**
  432. * 确定某个元素是否在数组中.
  433. *
  434. * @param item 要查找的Object对象
  435. *
  436. * @returns {boolean} 找到返回true,否则返回false;
  437. */
  438. contains: function (item) {
  439. var index = this.indexOf(item);
  440. return (index >= 0);
  441. },
  442. /**
  443. * 删除数组元素.
  444. *
  445. * @param {Object} item 要删除的对象
  446. */
  447. remove: function (item) {
  448. var index = this.indexOf(item);
  449. if (index >= 0) {
  450. this.splice(index, 1);
  451. }
  452. },
  453. /**
  454. * 替换数组中的reg为rpby 注意本方法不改变给定的数组本身。
  455. *
  456. * @param reg 要替换的元素
  457. * @param rpby 替换后的元素
  458. *
  459. * @returns {string[]}
  460. */
  461. replace: function (reg, rpby) {
  462. var ta = this.slice(0), d = '\0';
  463. var str = ta.join(d);
  464. str = str.replace(reg, rpby);
  465. return str.split(d);
  466. },
  467. /**
  468. * 删除数组元素.
  469. *
  470. * @param {Number} index 删除元素的下标.
  471. */
  472. removeAt: function (index) {
  473. this.splice(index, 1);
  474. },
  475. /**
  476. * 数字数组由小到大排序
  477. *
  478. * @returns {jQuery}
  479. */
  480. min2Max: function () {
  481. var oValue;
  482. for (var i = 0; i < this.length; i++) {
  483. for (var j = 0; j <= i; j++) {
  484. if (this[i] < this[j]) {
  485. oValue = this[i];
  486. this[i] = this[j];
  487. this[j] = oValue;
  488. }
  489. }
  490. }
  491. return this;
  492. },
  493. /**
  494. * 数字数组由大到小排序
  495. *
  496. * @returns {jQuery}
  497. */
  498. max2Min: function () {
  499. var oValue;
  500. for (var i = 0; i < this.length; i++) {
  501. for (var j = 0; j <= i; j++) {
  502. if (this[i] > this[j]) {
  503. oValue = this[i];
  504. this[i] = this[j];
  505. this[j] = oValue;
  506. }
  507. }
  508. }
  509. return this;
  510. },
  511. /**
  512. * 获得数字数组中最大项
  513. *
  514. * @returns {number}
  515. */
  516. getMax: function () {
  517. var oValue = 0;
  518. if (this.length > 0) {
  519. oValue = this[0];
  520. }
  521. for (var i = 0; i < this.length; i++) {
  522. if (this[i] > oValue) {
  523. oValue = this[i];
  524. }
  525. }
  526. return oValue;
  527. },
  528. /**
  529. * 获得数字数组中最小项
  530. *
  531. * @returns {number}
  532. */
  533. getMin: function () {
  534. var oValue = 0;
  535. if (this.length > 0) {
  536. oValue = this[0];
  537. }
  538. for (var i = 0; i < this.length; i++) {
  539. if (this[i] < oValue) {
  540. oValue = this[i];
  541. }
  542. }
  543. return oValue;
  544. }
  545. });
  546. })(jQuery);