Solved

.removeAttr() not working in my SharePoint JSOM code

Posted on 2016-10-31
8
61 Views
Last Modified: 2016-11-03
I had this question after viewing Remove 'disabled' attribute.  I implemented the same code below in a list that is generated dynamically but it's not working.  

One of the fields should get disabled if user is in the SharePont 'Manager' group but nothing happens.  If you would like the creds to my SharePoint site so you can see for yourself.  Let me know and I will send you the creds to your inbox.  Thanks!


jQuery(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");  
    
 console.log("Entering SPServices");
	$().SPServices({
 		operation: "GetGroupCollectionFromUser",
 		userLoginName: $().SPServices.SPGetCurrentUser(),
 		async: false,
 		completefunc: function (xData, Status) {
 		console.log(xData.responseXML.xml);
		if ($(xData.responseXML).find("Group[Name='Manager']").length == 1) {
 			$('[id^=faRowID]').removeAttr('disabled');
 		}
 		else {
 			alert("login outside of the Group user");
 		}
	  }
 
	}); /*close().SPServices({ */        
});

	/*New to make this dynamic-had to get it working first.*/
	var htmlTbl = "<table border='0' cellspacing='5' cellpadding='5'>"+
			  "<tbody>"+
			  "<tr><td class='bold'>Minimum Cost:</td><td><input type='text' id='min' name='min'></td></tr>"+
			  "<tr><td class='bold'>Maximum Cost:</td><td><input type='text' id='max' name='max'></td></tr>"+
			  "</table>"+
	    	  "<table id='prioritize' class='display' cellspacing='0' width='100%'>"+
	          "<thead>"+
		          "<th>ID</th>"+
		          "<th>Project Title</th>"+
		          "<th>Facility Priority</th>"+
		          "<th>VISN Priority</th>"+
		          "<th>National Priority</th>"+
		          "<th>Project Number</th>"+
		          "<th>VISN</th>"+
		          "<th>Station</th>"+
		          "<th>Total Project Cost</th>"+
		          "<th>Design Fiscal Year</th>"+
		          //"<th>Construction Fiscal Year</th>"+
	          "</thead>"+
	     	  "<tbody>";


	//Retrieves all list items- Will need to modify to take in query string data to pass to CAML.
	function retrieveListItems() {
		var clientContext = new SP.ClientContext();
		var oList = clientContext.get_web().get_lists().getByTitle('Project Module');
		
		var camlQuery = new SP.CamlQuery();
	  
		this.collListItem = oList.getItems(camlQuery);
		
		clientContext.load(collListItem);
		
	    clientContext.executeQueryAsync(
	        Function.createDelegate(this, this.onQuerySucceeded),
	        Function.createDelegate(this, this.onQueryFailed)
	        );
	}

	function onQuerySucceeded(sender, args) {
	    var listItemInfo = '';
	    var listItemEnumerator = collListItem.getEnumerator();		
	
		var i = 0;
	    while (listItemEnumerator.moveNext()) {
	        var oListItem = listItemEnumerator.get_current();

			
	        htmlTbl += "<tr><td class='NRM_pj_align'>" + oListItem.get_item('ID') + "</td>" +
    		"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Title')) + "</td>"+
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('Faciltity_x0020_Priority')+"> "+
            "<input type='text' size='1' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='faRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('Faciltity_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('Faciltity_x0020_Priority')) + ">"+
            "</td>" +
            
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('VISN_x0020_Priority')+"> "+
            "<input type='text' size='2' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='viRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('VISN_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('VISN_x0020_Priority')) + ">"+
            "</td>" +
            
			"<td class='NRM_pj_align' data-order="+oListItem.get_item('National_x0020_Priority')+"> "+
            "<input type='text' size='2' disabled name = 'RowID-" + oListItem.get_item('ID') + "' id='naRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('National_x0020_Priority') + " value=" + checkForNull(oListItem.get_item('National_x0020_Priority')) + ">"+
            "</td>" +
            				        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Project_x0020_Number')) + "</td>"+	        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('VISN')) + "</td>" +
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Station_x0020_Name')) + "</td>" +

			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Total_x0020_Project_x0020_Cost')) + "</td>"+	        
			"<td class='NRM_pj_align'>" + checkForNull(oListItem.get_item('Design_x0020_Obligation_x0020_Da')) + "</td></tr>";
			//"<td>" + oListItem.get_item('Construction_x0020_Obligation_ ') + "</td></tr>";

		}
		
			htmlTbl += "</tbody></table>";
		
			$("#divListItems").html(htmlTbl);
			

	 	/*Apply DataTable style */
     	var table = $('#prioritize').DataTable();     
	    // Event listener to the two range filtering inputs to redraw on input
	    $('#min, #max').keyup( function() {
	        table.draw();
	    } );  

	
	}
	
	$.fn.dataTable.ext.search.push(
	    function( settings, data, dataIndex ) {
	        var min = parseInt( $('#min').val(), 10 );
	        var max = parseInt( $('#max').val(), 10 );
	        var totProjCost = parseFloat( data[8] ) || 0; // use data for the age column
	 
	        if ( ( isNaN( min ) && isNaN( max ) ) ||
	             ( isNaN( min ) && totProjCost <= max ) ||
	             ( min <= totProjCost && isNaN( max ) ) ||
	             ( min <= totProjCost && totProjCost <= max ) )
	        {
	            return true;
	        }
	        return false;
	    }
	);
	
	
	function checkForNull(val) {
		if (val === null) {
			return " ";
		}else {return val;}
	}

Open in new window

0
Comment
Question by:Isaac
  • 4
  • 4
8 Comments
 
LVL 5

Author Comment

by:Isaac
ID: 41867499
Here's my html file

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<script src="/Webpart/JavaScript%20Library/js/jquery.js"></script>

<link rel="stylesheet" type="text/css" href="/Webpart/JavaScript%20Library/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="/Webpart/NRM/projectModule.css">
<script type="text/javascript" src="/Webpart/JavaScript%20Library/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/Webpart/JavaScript%20Library/js/jquery.SPServices-2014.02.js"></script>
<script type="text/javascript" src="/Webpart/NRM/priorityModule.js"></script>

<div id="divListItems"></div>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 41867775
Place an alert to confirm :
alert( "found: " + $('[id^=faRowID]').length + " and its value is: " + $('[id^=faRowID]').attr('disabled') );
$('[id^=faRowID]').removeAttr('disabled');

Open in new window


if you get at least 1 please send me your creds.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41867777
also try this :
jQuery('[id^=faRowID]').removeAttr('disabled');
instead :
$('[id^=faRowID]').removeAttr('disabled');
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Author Comment

by:Isaac
ID: 41867827
Neither one of those worked.  The alert never generated only when I went into developer mode (f12). See image below:

alert
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41869356
so found 0, if no element found, how do you want to remove the disabled attribute ?
as you can see, the browser need a bit of time to draw/generate the table.
the bad way is to add a timer :
So you replace :
jQuery('[id^="faRowID"]').removeAttr('disabled');

Open in new window

by :
setTimeout(function() { 
   $('[id^="faRowID"]').removeAttr('disabled'); 
}, 5000); // wait 5 seconds before removing the disabled attribute

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 41871427
look like you find the good way and use SP.ClientContext.executeQueryAsync(succeededCallback, failedCallback) which throw once the table is completed and drawed, could you please close the question?
For any further reference : https://msdn.microsoft.com/en-us/library/office/dn168907.aspx
0
 
LVL 5

Author Comment

by:Isaac
ID: 41872424
It's still not behaving the way I would hope.  When you go to the next page, the boxes become disabled.  I will close the ticket though and open up another question.  Thanks!
Adding the double quotes helped with a slight variation.

jQuery('input[id^="faRowID"]').removeAttr('disabled');
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41872425
jQuery('input[id^="faRowID"]').removeAttr('disabled');
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question