当前位置: 代码迷 >> JavaScript >> js tools(二)
  详细解决方案

js tools(二)

热度:507   发布时间:2012-11-22 00:16:41.0
js tools(2)

?

/**

?*?

?* @param divId

?* ? ? ? ? ? ?one container

?* @param info

?* ? ? ? ? ? ?operation result message to show in the up DIV, such as: "Add

?* ? ? ? ? ? ?Successfully!"

?* @param isSuccess

?* ? ? ? ? ? ?one sign whether operate successfully.Just two values:true or

?* ? ? ? ? ? ?false.true means success.

?* @returns undefined

?*/

function showOperationResultInfo(divId,info,isSuccess){

$("#"+divId).removeAttr("style");

if(isSuccess){

$("#"+divId).attr("style","color:green");

}else{

$("#"+divId).attr("style","color:red");

}

$("#"+divId).empty();

$("#"+divId).html(info);

}

?

/**

?* @param url

?* ? ? ? ? ? ?format must be xxx.action?eventName=

?* @param params

?* ? ? ? ? ? ?format must consist of "key=value",such as deleteId=10

?* @param doDeleteCallback

?* ? ? ? ? ? ?do works after servers response

?* @param validateFun

?* ? ? ? ? ? ?do validation before submitting deletion request

?* @param deleteNoteMess

?* ? ? ? ? ? ?popup warning information. if not passing,to use default value

?* @returns {Boolean} whether this function operates successfully or not

?

?*?

?* How to use: commonDeleteEntireObjectFun(URL.deleteInstance,"id=3",deleteCallback,deleteValidationFun);

?* function deleteCallback(data){

var flag = isSuccessRetun(data);

if(flag){

location.href=URL.loadInstanceListPage;

}else{

toShowResponseResMessWithoutParsingData("operationTipMess","operationTipMess_error",data);

}

}

?*?

?* function deleteValidationFun(params)

{

return commonValidateFun(URL.validateInstanceUsed,params);

}

?*?

?*

?*/

function commonDeleteEntireObjectFun(url,params,doDeleteCallback,validateFun,deleteNoteMess){

var flag = false;

if(validateFun&&$.isFunction(validateFun)){

flag = validateFun(params);

if(flag=="true"){

alert("The record is still referenced and cannot be deleted.");

return false;

}

}

if(!validateFun||flag){

var confirmObj="";

if(deleteNoteMess){

confirmObj = confirm(deleteNoteMess);

}else{

confirmObj = confirm(Messages.deleteConfirmMess);

}

if(confirmObj){?

$.post(url+"&"+params,function(data){

doDeleteCallback(data);

});

return false;

}else {

return false;

}

}

return false;

}

?

// to show servers' response messages in the current page

function toShowResponseResMessWithoutParsingData(divId,className,data){

$("#"+divId).empty();

$("#"+divId).html(data);

$("#"+divId).attr("class",className);

}

?

// whether servers respone successfully or not

function isSuccessRetun(data){

if(!data.startWith("error")){

return true;

} else{

return false;

}

}

?

/**

?* Description: simple function to set data given to hidden area

?*?

?* @param hiddenId

?* ? ? ? ? ? ?html id of hidden area

?* @param data

?* ? ? ? ? ? ?value to be set,format:json,such as,{"objectId":"100"}

?* @param jsonKey

?* ? ? ? ? ? ?json key

?*/

function setSingleHiddenAreaValue(hiddenId,data,jsonKey){

if(data!=undefined&&data!=null&&""!=$.trim(data)){

var value = $.parseJSON(data);

$("#"+hiddenId).empty();

$("#"+hiddenId).val(value[jsonKey]);

}

}

?

/**

?* * Description: simple function to send ajax reqeuest

?*?

?* @param url

?* ? ? ? ? ? ?reqeust address,format:xxxx.action,such as ContactGroupForm.action

?* @param params

?* ? ? ? ? ? ?request params,format:json.such as,{name:"Keifer",dept:"dev"}

?* @param callback

?* ? ? ? ? ? ?do work after response successfully

?* @returns json data if callback exists,data handled by it. Or return orignal

?* ? ? ? ? ?data responed

?*/

function commonPreAction(url,params,callback){

var returnValue="";

$.ajaxSetup({

cache:false,

async:false

});

$.post(url,params,function(data){

if(callback&&$.isFunction(callback)){

if(callback&&$.isFunction(callback)){

callback(data);

}

}

returnValue=data;

});

return returnValue;

}

?

?

/**

?* Description: simple common function to send ajax reqeuest

?*?

?* @param url

?* ? ? ? ? ? ?reqeust address,format:xxxx.action?eventName=,such as

?* ? ? ? ? ? ?ContactGroupForm.action?validateRecordUsed=

?* @param params

?* ? ? ? ? ? ?request params,format:string.such as,'name="Keifer"&dept="dev"'

?* @param callback

?* ? ? ? ? ? ?do work after response successfully

?* @returns json data if callback exists,data handled by it. Or return orignal

?* ? ? ? ? ?data responed

?*/

function simpleCommonSubmitAction(url,params,callback){

var returnValue="";

$.ajaxSetup({

cache:false,

async:false

});

$.post(url+"&"+params,function(data){

if(callback&&$.isFunction(callback)){

if(callback&&$.isFunction(callback)){

callback(data);

}

}

returnValue=data;

});

return returnValue;

}

?

/**

?* Description: simple common function to send ajax reqeuest for validation

?*?

?* @param url

?* ? ? ? ? ? ?reqeust address,format:xxxx.action?eventName=,such as

?* ? ? ? ? ? ?ContactGroupForm.action?validateRecordUsed=

?* @param params

?* ? ? ? ? ? ?request params,format:string.such as,'name="Keifer"&dept:"dev"'

?* @returns json data return orignal data responed

?*/

function commonValidateFun(url,params){

var returnValue="";

$.ajaxSetup({

cache:false,

async:false

});

$.post(url+"&"+params,function(data){

returnValue=data;

});

return returnValue;

}

?

/**

?* Description: simple function to show single hidden element

?*?

?* @param elementId

?* ? ? ? ? ? ?id of element to show

?* @param specifiedStyle

?* ? ? ? ? ? ?specified style to show

?* @returns undefined

?*/

function showHtmlElement(elementId,specifiedStyle){

$("#"+elementId).removeAttr("style");

$("#"+elementId).attr("style",specifiedStyle);

}

?

/**

?* Description: simple function to hide single element

?*?

?* @param elementId

?* ? ? ? ? ? ?id of element to show

?* @returns undefined

?*/

function hideHtmlElement(elementId){

$("#"+elementId).attr("style","display:none");

}

?

?

/**

?* Description: simple function to set value from select element to hidden area

?*?

?* @param inputId

?* ? ? ? ? ? ?id of hidden input

?* @param selectId

?* ? ? ? ? ? ?id of select id

?* @param sperator

?* ? ? ? ? ? ?sperator between one value and another,such as 11-12

?* @returns undefined

?*/

function simpleSetHiddenValueFromSelectElement(inputId,selectId,sperator)

{

var hiddenValueStr = "";

$.each($("#"+selectId+" option"),function(){

var value = $(this).val();

if(value){

hiddenValueStr += value+sperator;

}

});

$("#"+inputId).val(hiddenValueStr);

}

?

/**

?* Description: simple function to move one option of select element to another

?* select element

?*?

?* @param selectOneId

?* ? ? ? ? ? ?id of select one

?* @param selectTwoId

?* ? ? ? ? ? ?id of select two

?* @param actionNames

?* ? ? ? ? ? ?action name,such as move,delete,append

?* @param selectOneButtonIds

?* ? ? ? ? ? ?one array to store up element ids as the same direction

?* @param selectTwoButtonIds

?* ? ? ? ? ? ?one array to store up element ids as the same direction

?* @returns {Boolean} if occurring error,to return false

?*

?*?How to use:simpleCommonMouseClickFun("selectionLeftId", "selectionRightId", ['move'],['selection_RightMoveleft', 'selection_AllRightMoveleft' ], ['selection_LeftMoveRight', 'selection_AllLeftMoveRight' ],true,"----","skillLevelOnPIC");

?* or simpleCommonMouseClickFun('selectionLeftId', 'selectionRightId', ['move'],['selection_RightMoveleft', 'selection_AllRightMoveleft' ], ['selection_LeftMoveRight', 'selection_AllLeftMoveRight' ]);

?*

?*/

function simpleCommonMouseClickFun(selectOneId, selectTwoId, actionNames,

selectOneButtonIds, selectTwoButtonIds,isWithSkillLevel,separator,inputId) {

if(actionNames.length==1){

actionName = actionNames[0];

}else{

moveActionName = actionNames[0];

deleteActionName = actionNames[1];

}

$("#" + selectOneId).dblclick(function() {

if(isWithSkillLevel){

var isNumber_=OptionWithSkillLevel.removeSkilLevel(selectOneId, false, separator);

if(!isNumber_) return false;

}

$.listTolist(selectOneId, selectTwoId, actionName, false);

commonSortSelect(selectTwoId,true,"ASC");

return true;

});

$("#" + selectTwoId).dblclick(function() {

if(isWithSkillLevel){

var isNumber_=OptionWithSkillLevel.addSkilLevel(selectTwoId, false, separator, inputId);

if(!isNumber_) return false;

}

$.listTolist(selectTwoId, selectOneId, actionName, false);

commonSortSelect(selectOneId,true,"ASC");

return true;

});

if (selectOneButtonIds instanceof Array || selectOneButtonIds

&& typeof selectOneButtonIds == 'object'

&& 'length' in selectOneButtonIds) {

$.each(selectOneButtonIds, function(index, item) {

$("#" + item).bind("click",function() {

if (index < 1) {

if(isWithSkillLevel){

var isNumber_=OptionWithSkillLevel.addSkilLevel(selectTwoId, false, separator, inputId);

if(!isNumber_) return false;

}

$.listTolist(selectTwoId, selectOneId,actionName, false);

commonSortSelect(selectOneId,true,"ASC");

return true;

} else {

if(isWithSkillLevel){

var isNumber_= OptionWithSkillLevel.addSkilLevel(selectTwoId, true, separator, inputId);

if(!isNumber_) return false;

}

$.listTolist(selectTwoId, selectOneId,actionName, true);

commonSortSelect(selectOneId,true,"ASC");

return true;

}

});

});

$.each(selectTwoButtonIds, function(index, item) {

$("#" + item).bind("click",function() {

if (index < 1) {

if(isWithSkillLevel){

var isNumber_= OptionWithSkillLevel.removeSkilLevel(selectOneId, false, separator);

if(!isNumber_) return false;

}

$.listTolist(selectOneId, selectTwoId,actionName, false);

commonSortSelect(selectTwoId,true,"ASC");

return true;

} else {

if(isWithSkillLevel){

var isNumber_=OptionWithSkillLevel.removeSkilLevel(selectOneId, true,separator);

if(!isNumber_) return false;

}

$.listTolist(selectOneId, selectTwoId,actionName, true);

commonSortSelect(selectTwoId,true,"ASC");

return true;

}

});

});

} else {

alert('Parameter error!');

return false;

}

}

?

?

var OptionWithSkillLevel={

validateSkillLevelNumber:function(inputId){

var skillLevel = $("#"+inputId).val();

if(skillLevel){

var inputValue = ?checkIsPositiveInteger(skillLevel);

if(!inputValue){

alert(Messages.numberrequired);

return false;

}

}

return true;

},

validateSkillLevelNull:function(inputId){

var skillLevel = $("#"+inputId).val();

if($.trim(skillLevel)==""){

return false;

}

return true;

},

addSkilLevel:function(selectId,isAll,separator,inputId){

var options ;

if(isAll){

options = $("#"+selectId+" option");

}else{

options = $("#"+selectId+" option:selected");

}

var skillLevelNull = OptionWithSkillLevel.validateSkillLevelNull(inputId);

if(!skillLevelNull){

alert("Skill Level"+Messages.unnull);

return false;

}else if(OptionWithSkillLevel.validateSkillLevelNumber(inputId)){

var skillLevel = $("#"+inputId).val();

$.each(options,function(){

var textField = $(this).text();

$(this).text(textField+separator+skillLevel);

});

return true;

}else{

return false;

}

},

removeSkilLevel:function(selectId,isAll,separator){

var options ;

if(isAll){

options = $("#"+selectId+" option");

}else{

options = $("#"+selectId+" option:selected");

}

var flag = true;

$.each(options,function(){

var textField = $(this).text();

var skillLevel = textField.split(separator)[1];

var subsystem = ?textField.split(separator)[0];

if(skillLevel){

var inputValue = ?checkIsPositiveInteger(skillLevel);

if(!inputValue){

alert(Messages.numberrequired);

flag=false;

return;

}else{

if(subsystem){

$(this).text(subsystem);

}

}

}

});

return flag;

}

};

?

function simpleCommonCreateSelect(data,textfieldKey,valuefiledKey,selectId){

$("#"+selectId).empty();

// var result = eval(data);

var result = data;

$.each(result,function(index,item){

$("#"+selectId).AddOption(item[textfieldKey],item[valuefiledKey],false,index);?

});

}

?

/**

?* DESC:Append option element to the selection?

?* @param text ? text on show in the selection?

?* @param value ? value on show in the attribute "value" of option

?* @param selected ?whether selected current option,two values:true,false

?* @param index ?add option to special index

?* Such as <option value='value'>text</option>

?*/

jQuery.fn.AddOption = function(text, value, selected, index) {

option = new Option(text, value);

this[0].options.add(option, index);

this[0].options[index].selected = selected;

};

?

/**

?* @param url

?* @param selectOneId

?* @param selectTwoId

?* ? ? ? ? ? ?{selectId:"selGroupId",inputId:"groupId"}

?* @param params

?* ? ? ? ? ? ?{datatype:"json",textfield:"group",valuefiled:"id",parameter:"contactParams.detailId"}

?* @param callback

?* ? ? ? ? ? ?one function without params that is usesd to do final something

?*/

? function simpleCommonCascade(url,selectOneId,selectTwoId,params,callback){

?$("#"+selectOneId).bind("change",function(){

?try{

?var text ?=$("#"+selectOneId+" option:selected")[0].value;

?if(text==""||"Create"==text){

?$("#"+selectTwoId.selectId).empty();

?$("#"+selectTwoId.inputId).val("");

?return false;

?}

?}catch(e){}

var data = simpleCommonSubmitAction(url,params.parameter+"="+$("#"+selectOneId+" option:selected").val());

$("#"+selectTwoId.selectId).empty();

var result = eval(data);

if(result.length>1){

var groupId = $("#"+selectOneId+" option:selected")[0].alt;

$.each(result,function(index,item){

$("#"+selectTwoId.selectId).AddOption(item[params.textfield],item[params.valuefiled],false,index);?

if(item[params.valuefiled]==groupId){

$("#"+selectTwoId.selectId)[0].selectedIndex=index;

}

});

}else if(result.length==1){

$("#"+selectTwoId.selectId).AddOption(result[0][params.textfield],result[0][params.valuefiled],true,0);

}

$("#"+selectTwoId.inputId).val($("#"+selectTwoId.selectId+" option:selected").text());

callback();

?});

}

?

? /**

* @param selectId

* ? ? ? ? ? ?select element id that needs to sort.

* @param sOrder

* ? ? ? ? ? ?if to sort. true means by sort

* @param sortType

* ? ? ? ? ? ?format:String. Just two values:"ASC","DESC"

?*

?* How to use : commonSortSelect("selectionId",true,"ASC");

*/

? ?function commonSortSelect(selectId,sOrder,sortType){

?function addOption(object, object2) {?

? ? ? ? ? ?each(object2, function(o, index) {?

? ? ? ? ? ? ? ?object.options[index] = o;?

? ? ? ? ? ?});?

? ? ? ?}?

? ? ? ?function sortlist(sortName,isDesc,sortType) {?

? ? ? ? ? ?var what = document.getElementById(sortName);?

? ? ? ? ? ?this._options = map(what.options, function(o) {?

? ? ? ? ? ? ? ?return o;?

? ? ? ? ? ?});?

? ? ? ? ? ?this._options.sort(

? ? ? ? ? ? function(a, b) {?

? ? ? ? ? ? if(sortType=="DESC"){

? ? ? ? ? ? ? ? ? ?if (a.text > b.text) {?

? ? ? ? ? ? ? ? ? ? ? ?return isDesc == true ? 1 : -1;?

? ? ? ? ? ? ? ? ? ?} else {?

? ? ? ? ? ? ? ? ? ? ? ?return isDesc == true ? -1 : 1;?

? ? ? ? ? ? ? ? ? ?}?

? ? ? ? ? ? }else if(sortType=="ASC"){

? ? ? ? ? ? ? ? ? ?if (a.text > b.text) {?

? ? ? ? ? ? ? ? ? ? ? ?return isDesc == true ? -1 : 1;?

? ? ? ? ? ? ? ? ? ?} else {?

? ? ? ? ? ? ? ? ? ? ? ?return isDesc == true ? 1 : -1;?

? ? ? ? ? ? ? ? ? ?}?

? ? ? ? ? ? ? ?}

? ? ? ? ? ?}

? ? ? ? ? ?);?

? ? ? ? ? ?what.options.length = 0;// clear current options

? ? ? ? ? ?addOption(what, this._options);?

? ? ? ?}?

? ? ? ?function map(object, callback, thisp) {?

? ? ? ? ? ?var ret = [];?

? ? ? ? ? ?each.call(thisp, object, function() {?

? ? ? ? ? ? ? ?ret.push(callback.apply(thisp, arguments));?

? ? ? ? ? ?});?

? ? ? ? ? ?return ret;?

? ? ? ?}?

? ? ? ?function each(object, callback) {?

? ? ? ? ? ?if (undefined === object.length) {?

? ? ? ? ? ? ? ?for ( var name in object) {?

? ? ? ? ? ? ? ? ? ?if (false === callback(object[name], name, object))?

? ? ? ? ? ? ? ? ? ? ? ?break;?

? ? ? ? ? ? ? ?}?

? ? ? ? ? ?} else {?

? ? ? ? ? ? ? ?for ( var i = 0, len = object.length; i < len; i++) {?

? ? ? ? ? ? ? ? ? ?if (i in object) {?

? ? ? ? ? ? ? ? ? ? ? ?if (false === callback(object[i], i, object))?

? ? ? ? ? ? ? ? ? ? ? ? ? ?break;?

? ? ? ? ? ? ? ? ? ?}?

? ? ? ? ? ? ? ?}?

? ? ? ? ? ?}?

? ? ? ?}?

? ? ? ?function sort(){ ? ?

? ? ? ? ? ?if(sOrder){?

? ? ? ? ? ? ? ?sOrder = false;?

? ? ? ? ? ?}else{?

? ? ? ? ? ? ? ?sOrder = true;?

? ? ? ? ? ?}?

? ? ? ? ? ?sortlist(selectId,sOrder,sortType);?

? ? ? ?}?

? ? ? ?sort();

? }

?

? ?function simpleManuallyAssembleSelect(data,selectId,valueKey,textKey,isAlt,altKey){

? ?var optionHtml ="";

? ?if(isAlt){

? ? if(!altKey){

? ? alert("Parameter error!");

? ? return false;

? ? }

? ? if(data&&data.length>1){

? ? optionHtml = "<option value='' alt=''></option>";

? ? }

? ? if(data){

? ? $.each(data,function(index,item){

? ? optionHtml +="<option value="+item[valueKey]+" alt="+item[altKey]+">"+item[textKey]+"</option>";

? ? });

? ? }

? ?}else{

? ? if(data&&data.length>1){

? ? optionHtml = "<option value=''></option>";

? ? }

? ? if(data){

? ? $.each(data,function(index,item){

? ? optionHtml +="<option value="+item[valueKey]+">"+item[textKey]+"</option>";

? ? });

? ? }

? ?}

$("#"+selectId).empty();

$(optionHtml).appendTo($("#"+selectId));

? ?}

?

? ?function simpleManuallyAssembleSelect3(data,selectId,valueKey,textKey){

? ?var optionHtml ="";

? ?if(data){

? ?$.each(data,function(index,item){

? ? optionHtml +="<option value="+item[valueKey]+">"+item[textKey]+"</option>";

? ?});

? ?}

$("#"+selectId).empty();

$(optionHtml).appendTo($("#"+selectId));

? }

?

? ?function simpleManuallyAssembleSelect2(data,selectId,valueKey,textKey,objectKey,isAlt,altKey){

? ?var optionHtml ="";

? ?if(isAlt){

? ? if(!altKey){

? ? alert("Parameter error!");

? ? return false;

? ? }

? ? if(data&&data.length>1){

? ? optionHtml = "<option value='' alt=''></option>";

? ? }

? ? if(data){

? ? $.each(data,function(index,item){

? ? optionHtml +="<option value="+item[objectKey][valueKey]+" alt="+item[altKey]+">"+item[objectKey][textKey]+"</option>";

? ? });

? ? }

? ?}else{

? ? if(data&&data.length>1){

? ? optionHtml = "<option value=''></option>";

? ? }

? ? if(data){

? ? $.each(data,function(index,item){

? ? optionHtml +="<option value="+item[objectKey][valueKey]+">"+item[objectKey][textKey]+"</option>";

? ? });

? ? }

? ?}

$("#"+selectId).empty();

$(optionHtml).appendTo($("#"+selectId));

? }

?

? ?/**

? ? * ?DESC:add tip for selection

? ? * ?how to use : selectionShowTipFun.addSelectionTip("demoId");

? ? */

? ?var selectionShowTipFun = {

? showSelectionTip:function(e,selectId){

? var Tip = document.getElementById("Tip");

if (Tip == null || Tip == 'undefined')

Tip = document.createElement("div");

?

var target = e.srcElement ? e.srcElement : e.target;

var scrollT = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;

Tip.innerHTML = $("#"+selectId+" option:selected").text();

Tip.setAttribute("id", "Tip");

Tip.style.display = "block";

Tip.style.backgroundColor = "#FFFFCC";

Tip.style.width = 250;

Tip.style.height = 15;

Tip.style.left = (parseInt(e.clientX) ?+ 10 ) + ?"px";

Tip.style.top = (parseInt(e.clientY) + parseInt(scrollT) + 3) + "px";

Tip.style.position = "absolute";

document.body.appendChild(Tip);

? },

? closeSelectionTip:function(){

? var Tip = document.getElementById("Tip");

? Tip.style.display = "none";

? },

? addSelectionTip:function(selectId){

? var selectItem = $("#"+selectId);

? var showText = $.trim($("#"+selectId+" option:selected").val());

? // add event handlers in order that selection light up

if(showText){

selectItem.hover(function() {

selectionShowTipFun.showSelectionTip(event,selectId);

},function() {

selectionShowTipFun.closeSelectionTip();

});

}

? }

? ?};

?

  相关解决方案