Show info on predecessors in DispForm of tasks list

Home Forums Classic DFFS Show info on predecessors in DispForm of tasks list

Tagged: 

Viewing 1 reply thread
  • Author
    Posts
    • #8602
      Alexander Bautz
      Keymaster

        This code example shows how you can display more information on predecessors in DispForm of a task. By default, only the “Title” field is shown, and as the predecessors are selected from a regular lookup column vLookup cannot be used to show more information.

        This code will work for SharePoint 2010 and 2013.

        Add this code to the Custom JS textarea:

        function showPredesessors(){
        	var listTitle = "Tasks", pArr = spjs.dffs.beforeProperties.Predecessors, qb = [], cc, q, list, items, res = [];
        	if(pArr.length === 0){
        		$("#PutPredecessorsHere").html("No items found");
        		return;
        	}
        	cc = new SP.ClientContext.get_current();
        	list = cc.get_web().get_lists().getByTitle(listTitle);
        	qb.push("<View Scope='RecursiveAll'><Query>");
        	qb.push("<Where>");
        		qb.push("<In>");
        			qb.push("<FieldRef Name='Title' />");
        			qb.push("<Values>");
        				$.each(pArr,function(i,t){
        					qb.push("<Value Type='Text'>"+t+"</Value>");
        				});
        			qb.push("</Values>");
        		qb.push("</In>");
        	qb.push("</Where>");	
        	qb.push("</Query></View>");
        	q = new SP.CamlQuery();
        	q.set_viewXml(qb.join(''));
        	items = list.getItems(q);
        	cc.load(items);
        	cc.executeQueryAsync(
        		function(sender, args){
        			var e = items.getEnumerator();
        			res.push("<table cellpadding='2' cellspacing='0'>");
        			res.push("<tr>");
        			res.push("<th valign='top'>Title</th>");
        			res.push("<th valign='top'>% Complete</th>");
        			res.push("</tr>");
        			while(e.moveNext()){
        				item = e.get_current();
        				res.push("<tr>");
        				res.push("<td valign='top'>"+item.get_item("Title")+"</td>");
        				res.push("<td valign='top'>"+(item.get_item("PercentComplete") !== null ? (item.get_item("PercentComplete") * 100) + " %" : "")+"</td>");
        				res.push("</tr>");					
        			}
        			res.push("</table>");
        			$("#PutPredecessorsHere").html(res.join(""));
        		},
        		function(sender, args){
        			alert(args.get_message());
        		}
        	);
        }

        Then add this code to a “HTML” area in a tab:

        <div id="PutPredecessorsHere"></div>

        Last step is to add a rule with trigger “Selected tab index” equals the tab index you put the div tag in the previous step. This rule must have the name of the function “showPredesessors” in the “Run these functions / trigger these rules” field.

        You will now have a basic table with information on “Title” and “% Complete” for all the predecessors of the current task.

        Please note that this code is a basic example, and you will have to use this code as a starting point for your own customization.

        Alexander

        • This topic was modified 9 years, 1 month ago by Alexander Bautz. Reason: Fixed an error in the code example
      • #8849
        Alexander Bautz
        Keymaster

          Here is an updated code to fix a bug with selecting one item from a multilookup column (thanks to Ferdi Lethen):

          function showPredesessors(){
          	var listTitle = "Tasks", pArr = spjs.dffs.beforeProperties.Predecessors, qb = [], cc, q, list, items, res = [];
          	if(pArr.length === 0){
          		$("#PutPredecessorsHere").html("No items found");
          		return;
          	}
          	cc = new SP.ClientContext.get_current();
          	list = cc.get_web().get_lists().getByTitle(listTitle);
          	qb.push("<View Scope='RecursiveAll'><Query>");
          		qb.push("<Where>");
          		qb.push("<In>");
          			qb.push("<FieldRef Name='FieldNo' />");
          				qb.push("<Values>");
          				if (pArr.length === 1){
          					qb.push("<Value Type='Text'>"+pArr[0]+"</Value>");
          				} else {
          					$.each(pArr,function(i,t){
          						qb.push("<Value Type='Text'>"+t+"</Value>");
          					});
          				}
          			qb.push("</Values>");
          		qb.push("</In>");
          		qb.push("</Where>"); 
          	qb.push("</Query></View>");
          	q = new SP.CamlQuery();
          	q.set_viewXml(qb.join(''));
          	items = list.getItems(q);
          	cc.load(items);
          	cc.executeQueryAsync(
          		function(sender, args){
          			var e = items.getEnumerator();
          			res.push("<table cellpadding='2' cellspacing='0'>");
          			res.push("<tr>");
          			res.push("<th valign='top'>Title</th>");
          			res.push("<th valign='top'>% Complete</th>");
          			res.push("</tr>");
          			while(e.moveNext()){
          				item = e.get_current();
          				res.push("<tr>");
          				res.push("<td valign='top'>"+item.get_item("Title")+"</td>");
          				res.push("<td valign='top'>"+(item.get_item("PercentComplete") !== null ? (item.get_item("PercentComplete") * 100) + " %" : "")+"</td>");
          				res.push("</tr>");					
          			}
          			res.push("</table>");
          			$("#PutPredecessorsHere").html(res.join(""));
          		},
          		function(sender, args){
          			alert(args.get_message());
          		}
          	);
          }
      Viewing 1 reply thread
      • You must be logged in to reply to this topic.