/** * 字符串全部替换 * @param s1 * @param s2 * @returns */ String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2); } /** * 判断是否以特定字符串结尾 * @param suffix * @returns {Boolean} */ String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; //ie支持Array.prototype.includes() if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(searchElement, fromIndex) { // 1. Let O be ? ToObject(this value). if (this == null) { throw new TypeError('"this" is null or not defined'); } var o = Object(this); // 2. Let len be ? ToLength(? Get(O, "length")). var len = o.length >>> 0; // 3. If len is 0, return false. if (len === 0) { return false; } // 4. Let n be ? ToInteger(fromIndex). // (If fromIndex is undefined, this step produces the value 0.) var n = fromIndex | 0; // 5. If n ≥ 0, then // a. Let k be n. // 6. Else n < 0, // a. Let k be len + n. // b. If k < 0, let k be 0. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); // 7. Repeat, while k < len while (k < len) { // a. Let elementK be the result of ? Get(O, ! ToString(k)). // b. If SameValueZero(searchElement, elementK) is true, return true. // c. Increase k by 1. // NOTE: === provides the correct "SameValueZero" comparison needed here. if (o[k] === searchElement) { return true; } k++; } // 8. Return false return false; } }); } /** * 获取上下文路径 * @returns */ function getContextPath() { var pathName = document.location.pathname; var index = pathName.substr(1).indexOf("/"); var result = pathName.substr(0,index+1); result=result=="/myconsole"?"":result; return result; } /** * 隐藏jgrid的编辑对话框中的行并清空数据 * * @param inputid */ function hide(inputid) { $("#tr_" + inputid).hide(); $("#" + inputid).val(""); } /** * 显示jgrid的编辑对话框中的行 * * @param inputid */ function show(inputid) { $("#tr_" + inputid).show(); } /** * 行内显示图片,需要在colModel中设置formatter: alarmFormatter * * @param cellvalue * @param options * @param rowdata * @returns {String} */ function alarmFormatter(cellvalue, options, rowdata) { return ' '; } /** * 修改JSON中key的方法 */ function modifyJosnKey(json, oddkey, newkey) { var val = json[oddkey]; delete json[oddkey]; json[newkey] = val; } /** * 给jgrid添加Jfinal中getModel方法中必需的model * * @param postdata * @param modelName * @returns */ function addModel(modelName, postdata) { $.each(postdata, function(index, value) { // console.log(index); if (index != "jqGrid_id" && index != "oper" && index != "id") { modifyJosnKey(postdata, index, modelName + "." + index); } if (index == "id") { postdata[modelName + "." + index] = value } // console.log(index); }) return postdata; } /*--------------以下为表单中的tree展示-------------------*/ function makeTree(inputid,actionURL,type){ var menuContentID=inputid+"_menuContent"; var treeDemoID=inputid+"_tree"; //myconsole.log(type); var check={enable: true,chkStyle: "radio",radioType: "all"}; if(type=="checkbox"){ check={enable: true,chkboxType: {"Y":"", "N":""}}; } var hideMenu=function () { $("#"+menuContentID).fadeOut("fast"); $("body").unbind("mousedown", onBodyDown); } var onBodyDown=function (event) { if (!(event.target.id == "menuBtn" || event.target.id == inputid || event.target.id == menuContentID || $(event.target).parents( "#"+menuContentID).length > 0)) { hideMenu(); } } var onClick=function (e, treeID, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeID); zTree.checkNode(treeNode, !treeNode.checked, null, true); return false; } var onCheck=function (e, treeID, treeNode) { var zTree = $.fn.zTree.getZTreeObj(treeID), nodes = zTree.getCheckedNodes(true), v = "",// 显示值 rv = "";// 真正值 // console.log(nodes); for (var i = 0, l = nodes.length; i < l; i++) { rv += nodes[i].id + ","; v += nodes[i].name + ","; } if (v.length > 0) v = v.substring(0, v.length - 1); if (rv.length > 0) rv = rv.substring(0, rv.length - 1); //myconsole.log(inputid+"---"+rv+"----"+v); //myconsole.log(inputid+"---"+rv+"----"+v); $("#" + inputid).val(rv); $("#" + inputid).attr("offval",rv); // $("#jqGrid").jqGrid("setRowData", inputid, rv+""); $("#_" + inputid).val(v); } $.get(getContextPath()+actionURL,{"parentid":$("#"+inputid).val()},function(data){ $("#_"+inputid).val(data); }) var value=$("#"+inputid).val(); var upbtn=$(''); //myconsole.log($("#"+inputid)); $("#"+inputid).parent().append(upbtn); $("#"+inputid).hide(); var setting = { check: check, view: { dblClickExpand: false }, data: { simpleData: { enable: true, idKey: "id", pIdKey: "parentid", rootPId: "0" } }, callback: { onClick: onClick, onCheck: onCheck } }; $("#_"+inputid).click(function(){ var id = $("#"+inputid).val(); $.get(getContextPath()+actionURL,{id:$("#"+inputid).attr("rowid")},function(data){ zNodes=data; $("#"+menuContentID).remove(); var menu='
'; $("#"+inputid).parent().append(menu); var tree=$.fn.zTree.init($("#"+treeDemoID), setting, zNodes); // tree.expandAll(false); var cityObj = $("#_"+inputid); var cityOffset = $("#_"+inputid).position(); $("#"+menuContentID).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast"); $("body").bind("mousedown", onBodyDown); var nodes = tree.getNodesByParam("id", id, null); tree.checkNode(nodes[0], true, true); }); }) } /*--------------以上为表单中的tree展示-------------------*/ /** * iframe高度自适应 */ function dyniframesize(down) { var pTar = null; if (document.getElementById) { pTar = document.getElementById(down); } else { eval('pTar = ' + down + ';'); } if (pTar && !window.opera) { // begin resizing iframe pTar.style.display = "block" if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) { // ns6 syntax pTar.height = pTar.contentDocument.body.offsetHeight + 20; pTar.width = pTar.contentDocument.body.scrollWidth + 20; } else if (pTar.Document && pTar.Document.body.scrollHeight) { // ie5+ syntax pTar.height = pTar.Document.body.scrollHeight; pTar.width = pTar.Document.body.scrollWidth; } } } function design(templateFile){ window.open(templateFile); } function fullOpen(winurl) { objWin__ = window.open( winurl,"","top=0,left=0,scrollbars=yes,width="+(screen.availWidth-200)+",height="+(screen.availHeight-30)); objWin__ .focus(); } /*$(function(){ jQuery.fn.center = function () { this.css("position","absolute"); var amend=80; if(this.height()>$(window).height()){ amend=300 } //console.log($(window).height()+"_"+this.height()+"_"+$(window).scrollTop()); this.css("top", ( $(window).height() - this.height()+amend) / 2+$(window).scrollTop() + "px"); this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px"); return this; } $('body').on('DOMNodeInserted', '.ui-jqdialog', function () { $(this).center(); }); })*/