Pages

Monday 6 January 2014

How to get the selected Item from a webpart and pass it as a parameter in a query string

For this example we have 2 lists
1: Person - Fields: Document title (which is the name)
2: Details - Fields: Person (lookup to Document Title in Person List) and Phone No (text)

We have a page with both list viewer web parts and both are connected with filter values being passed from "Person" to "Details"



when you select an ID from the Person web part the details are populated, however, if you select the 2nd item from Person web part and would like to add a new number to under details you have to select the Person from the drop down. This is not a solution as it can get very labor intensive as the number of "persons" increase.


















An alternate approach would be to append the ID of the selected "Person" (which you can get from the selected item) to the query string, read this value and populate this value by default.

Here is the javascript to change the display text instead of default text "add new item"

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push('ChangeText');

function ChangeText()
{
var ttnA = document.getElementsByTagName('a');
var ttnCounter = 0;
for (var j=0; j<ttnA.length; j++)
{

  if (ttnA[j].id == 'idHomePageNewItem')
  {
  ttnCounter++;
  if(ttnCounter ==1) /* This is the webpart sequence, if it was the 3rd web part on the page the id would be 2 */
{

var selectedImg$ = $('img[alt^="Selected"]');
var elemID = selectedImg$.closest('tr').attr('iid').split(',')[1];
var linkText = selectedImg$.closest('tr').find('div#' + elemID).find('a').text();
//alert(linkText);
                var linkTextReplaced = linkText.replace("DEVDOMAIN\\","");

                $(ttnA[j]).attr("href","javascript:NewItem2(event,'http://dev/marketing/Lists/Details/NewForm.aspx?RootFolder=&IsDlg=1&Web=f939c7f8%2Dd6e0%2D4046%2D854d%2D070b31bce0cf&UserID="  + linkTextReplaced + "');");
       
 $(ttnA[j]).removeAttr("onclick");
                ttnA[j].innerHTML='Click to add a new phone number for DEVDOMAIN\\' + linkTextReplaced
        
       }
     }
   }
}</script>

No comments:

Post a Comment