I am more than just a survey tool : Project List dropdown

In this article you will discover how you can make an HTML dropdown of all the projects on your server that you are allowed to access. You might ask, why would you need this, but think about it. In our last set of articles i showed you how to make a list of the data in your survey appear in a web page. In the code we manually specified the project, it would be far better if you could first select the project you wanted to query.

First off, let’s make the dropdown. Create a holder for the dropdown, in our case it is going to be a text item,

DropDown "{DropDownHTML}" style(
            Width = "15em",
            Control(
                Type = "SingleLineEdit"
            )
        )
        labelstyle(
            Align = "Right",
            Width = "8em"
        )
    text [1..50];

Next we need ( in our routing ) the code to loop the projects. You would have seen some of this before in our login code,


Dim dpmAgent,bIsLoggedIn
Dim gLogin, gPassword,sProjectDropDownHtml

gLogin = "Login"
gPassword = "Password"


Set dpmAgent = CreateObject("SPSSMR.DPM.Security.Login.Agent2")
	dpmAgent.ConnectToDPMServer("localhost")
	bIsLoggedIn = dpmAgent.Login(gLogin,gPassword,Null)

	sProjectDropDownHtml = MakeProjectDropdown(dpmAgent,bIsLoggedIn)
	
	DropDown.Label.Inserts["DropDownHTML"] = sProjectDropDownHtml
	DropDown.Ask()
	
dpmAgent.Logout()

The bit you have not seen is the getting the projects list. We put this code in a function called MakeProjectDropdown


Function MakeProjectDropdown(dpmAgent,bIsLoggedIn)
Dim sHTML,oProject

	If ( bIsLoggedIn ) Then 
		sHTML = ""
	Else
		sHTML = "Not Connected to DPM"
		
	End If
	MakeProjectDropdown = sHTML 
End Function

From the dpmAgent object , we can get a list of all the projects and once we have that it is very easy to loop them and render the HTML code for the dropdown. When you have typed all this in , you end up with something like this.

DropDown

What we need next is to make it so that when the dropdown is changes we capture the value. This can be done with a few lines of JavaScript. First off we need to change our function that renders the html code,

Function MakeProjectDropdown(dpmAgent,bIsLoggedIn,sQuestionID)
Dim sHTML,oProject

	If ( bIsLoggedIn ) Then 
		sHTML = ""
	Else
		sHTML = "Not Connected to DPM"
		
	End If
	MakeProjectDropdown = sHTML 
End Function

Then we need to change the way we call the function.

sProjectDropDownHtml = MakeProjectDropdown(dpmAgent,bIsLoggedIn,"_Q0")

You will notice that we pass in the ID of the text box that we want to populate. We found out what the id was , by doing a view source, but you could work it out , it’s the first object on the page. So with all this code in place when you select something from the dropdown it populates the answer in the text box, this now means that you can get the value of the dropdown into your survey and do with it as you so wish.

Leave a Comment

%d bloggers like this: