Have you ever read the article I wrote about how to hide or change the "Please Select" words with your own words including its value? Well, the following trick will show you how to remove the "Please Select" option from the Select Option in websites that created with PHPMaker, instead of to replace the "Please Select" words and its value.
This trick is very useful whenever you want to remove the "Please Select" option completely from the Select Option (Combobox) without having to modify the generated script files. I repeat: without having to modify the generated script files. Now let's take a look at the following example. In the Add page, you want to limit the options that belongs to the UserID field, by displaying the current logged UserID, and then by default you only want to display the only UserID. In order to automatically select the only UserID in the Combobox, then we should remove the "Please Select" option. If we do not remove that "Please Select" option, then by default it will automatically select the first option ("Please Select"), so your user has to change the selected option with another one you have in it. After removing that first option, now we only have the only option (UserID), and it will automatically be selected when you load that Add page.
As I mentioned before, we will not modify the generated script files at all. We only use the Row_Rendered() function from the Server Events that provided by PHPMaker for this purpose, and it works perfectly as we want!
Doesn't it sound great? Curious? 😉
[hidepost]
- Open your PHPMaker project (.pmp) file by using PHPMaker.
-
From the Database setup, click one of your desired table, and then click on the Server Events/Client Scripts tab. Go to Server Events -> Table-Specific -> Common -> Row_Rendered, and then insert the following code to your Row_Rendered() function that located in the editor code at the right pane of your PHPMaker:
if (CurrentPageID() != "list" && CurrentPageID() != "search") { array_shift($this->iduser->EditValue); // Remove the first option using PHP function: array_shift }If you have never modified that Row_Rendered() function, then the complete code would be like this:
// Row Rendered event function Row_Rendered() { // To view properties of field class, use: //var_dump($this-><FieldName>); if (CurrentPageID() != "list" && CurrentPageID() != "search") { array_shift($this->iduser->EditValue); } }Please note that if, from the example above, iduser field is one of your primary key so this field will become read-only while your user is editing the record, then change this code:
if (CurrentPageID() != "list" && CurrentPageID() != "search") {become the following:
if (CurrentPageID() != "list" && CurrentPageID() != "search" && CurrentPageID() != "edit") {In other words, this will prevent an error/warning message "array_shift() expects parameter 1 to be array, string given". Hope that helps!
- Save the project, and then re-generate your script files using PHPMaker as usual.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.