Difference between revisions of "JQuery / script cheatsheet"

From TempusServa wiki
Jump to navigation Jump to search
old>Admin
old>Admin
Line 57: Line 57:
         }
         }
   });});
   });});
==== Redirecting non administrators away from list views ====
  var params =  window.location.href.split("?")[1];
  if( params == "SagID=251&command=list" ) {
    if( ! $( "#TempusServaPage" ).hasClass( "IsAdministrator" ) ) {
      console.log("redirecting to main from list");
      window.location.href = "main";
    }
  }

Revision as of 09:37, 6 October 2017

Selecting text for easy copying

 var textNode = document.getElementById('IdOfNodeToBeSelected');
 if (document.selection) {
   var range = document.body.createTextRange();
   range.moveToElementText(textNode);
   range.select();
 } else if (window.getSelection) {
   var selection = window.getSelection();
   var range = document.createRange();
   range.selectNodeContents(textNode);
   selection.removeAllRanges();
   selection.addRange(range);	
 }


Building multiselect values from existing services

In the following example the field TERRITORY has autocomplete wuth Country values. Output format example: "Denmark, Finland, Sweden"

 function split( val ) {
    return val.split( /,\s*/ );
 }
 function extractLast( term ) {
   return split( term ).pop();
 }
 $(function() {$("#DATA_TERRITORY").autocomplete({
       source: function( request, response ) {
         $.getJSON( "autocomplete?type=dk.p2e.blanket.form.fields.ajax.FieldAjaxCountry&subtype=0", {
           term: extractLast( request.term )
         }, response );
       },
      autoFocus: true, 
      min_length: 2, 
      delay: 300, 
       search: function() {
         // custom minLength
         var term = extractLast( this.value );
         if ( term.length < 2 ) {
           return false;
         }
       },
       focus: function() {
         // prevent value inserted on focus
         return false;
       },
       select: function( event, ui ) {
         var terms = split( this.value );
         // remove the current input
         terms.pop();
         // add the selected item
         terms.push( ui.item.value );
         // add placeholder to get the comma-and-space at the end
         terms.push( "" );
         this.value = terms.join( ", " );
         return false;
       }
 });});


Redirecting non administrators away from list views

 var params =  window.location.href.split("?")[1];
 if( params == "SagID=251&command=list" ) {
   if( ! $( "#TempusServaPage" ).hasClass( "IsAdministrator" ) ) {
     console.log("redirecting to main from list");
     window.location.href = "main";
   }
 }