Search code examples
phpprado

Show values in TDropDownList in PRADO


I m new to PRADO, I have a file Home.page with code:

<%@ Title="Contact List" %>

<h1>Contact List</h1>

<a href="<%= $this->Service->constructUrl('insert')%>">Create New Contact</a>
<br/>
<com:TForm>
 <com:TDropDownList ID="personInfo">
  <com:TListItem Value="value 1" Text="item 1" />
  <com:TListItem Value="value 2" Text="item 2" Selected="true" />
  <com:TListItem Value="value 3" Text="item 3" />
  <com:TListItem Value="value 4" Text="item 4" />
 </com:TDropDownList>
</com:TForm>

& Home.php with code

<?php
class Home extends TPage
{
    /**
     * Populates the datagrid with user lists.
     * This method is invoked by the framework when initializing the page
     * @param mixed event parameter
     */
    public function onInit($param)
    {
        parent::onInit($param);
        // fetches all data account information
        $rec = ContactRecord::finder()->findAll();
    }
}
?>

the $rec contain array of all values.

Now I want to show all name in Dropdown list. I tried my best but fail. Can anyone help me? Thanks


Solution

  • You can populate the DataTextField and DataValueField of the dropdownlist with the database table column names to use as the TListItem's text and value respectively:

    <?php
    class Home extends TPage
    {
        /**
         * Populates the datagrid with user lists.
         * This method is invoked by the framework when initializing the page
         * @param mixed event parameter
         */
        public function onInit($param)
        {
            parent::onInit($param);
            // fetches all data account information
            $rec = ContactRecord::finder()->findAll();
            $this->personInfo->DataSource = $rec;
            $this->personInfo->DataTextField = "columnNameToUseAsText";
            $this->personInfo->DataValueField = "columnNameToUseAsValue";
            $this->personInfo->DataBind();
        }
    }
    ?>
    

    Alternatively, you can do it in the HTML front-end:

    <com:TDropDownList ID="personInfo" DataTextField="columnNameToUseAsText" DataValueField="columnNameToUseAsValue" />
    

    This way, you only need to specify the DataSource property and call the DataBind() method in your back-end code.