[How-To] Creating a DropDown for values in a table then filtering

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
Royalridge
Posts: 2
Joined: 2015-04-14 10:02

[How-To] Creating a DropDown for values in a table then filtering

Post by Royalridge » 2015-04-24 08:43

There may be times when you'd like a select drop-down that's populated from the table being displayed.

For example, I've a single table (playlist) that contains a number of text fields, one of which is the "Artist". I want to be able to see all the Artist names that are in the table and pick one from a drop-down and then filter the data based on the selected value.

See attachment for screenshot showing what I mean.
F80sFilter.png
F80sFilter.png (58.72 KiB) Viewed 5645 times
This is such a common filter that I would have thought that it would be a tickable option when generating the code.... maybe in the next version? ;)

However it can also be achieved fairly easily with the hook system.

In this case the hook we're interested in is the _header / tableview hook.

Normally this is:

Code: Select all

case 'tableview':
    break;
So we'll add our new code here to create a drop-down and filter.

Code: Select all

case 'tableview':
    $header='<%%HEADER%%>';

    // Add the text that says "Select"
    $header .= "<p>Select</p>";

    // Drop down to filter by artist

    // If we've requested an artist by the drop-down then we'll get the artist name back and store it so we
    // can show it in the list as already selected
    $showingartist=$_REQUEST['FilterValue'][1];

    // If we've cleared filters then we'll make the selected name blank
    If (isset($_REQUEST[NoFilter_x]) && $_REQUEST[NoFilter_x] = 1 ) $showingartist="";

    // The code to create a form (I'm using the full domain/path to the file)
    $header .= '<div class="btn btn-default"><form method="post" action="http://www.forgotten80sclub.com/playlist/playlist_view.php">
                     <input name="FilterAnd[1]" value="" type="hidden">
                     <input name="FilterField[1]" value="3" type="hidden">
                     <input name="FilterOperator[1]" value="<=>" type="hidden">
                     <i class="glyphicon glyphicon-filter"></i>Artist:
                      <select name="FilterValue[1]">
                      <option value="">All</option>';

    // Now we get the "distinct" different values for the artist field from the table.
    $res = sql( "SELECT DISTINCT(artist) FROM playlist ORDER BY artist", $eo);

    // Now if we've any artists we need to add them to the dropdown as an <option>"
    // Note that if the artist is the same as the one that's currently selected we mark it as such.
    while($row = db_fetch_assoc($res)){
        $myartist=$row['artist'];
        $header .= "<option ".($myartist == $showingartist ? " selected " : "" )." value=\"".$myartist."\">".$myartist."</option>\n";
      }
    // Close the dropdown off.
    $header .= "</select>\n";
    // Make a submit button for the form
    $header .= '<button type="submit" class="btn btn-default">Select</button></form>';   
    $header .= "</div>";
     break;
And with that we're done.

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: [How-To] Creating a DropDown for values in a table then filtering

Post by a.gneady » 2015-04-26 20:04

Thanks for sharing :D
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

patsd102
Veteran Member
Posts: 142
Joined: 2013-01-15 19:59

Re: [How-To] Creating a DropDown for values in a table then filtering

Post by patsd102 » 2015-04-29 20:16

Cant get it working
23.17

Post Reply