Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Search Text field with Dropdown. [SOLVED]

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1553 Views
  • SamTheJarvis
    Avatar
    Community Member
    24 Posts

    Search Text field with Dropdown. [SOLVED] Link to this post

    Hi,

    How would I do the above, I have a text field which is an amalgamation of several other fields.

    I want to be able to search the TextField with my custom search context with a dropdown.

    I have a feeling it's something to do with scaffolding.. but not quite sure.

       public function getCustomSearchContext() {
    $fields = $this->scaffoldSearchFields(array(
    'restrictFields' => array('Type', 'Regions', 'Budget')
    ));
    $filters = array(
             'Type' => new ExactMatchFilter('Type'),
             'Budget' => new ExactMatchFilter('Budget'),
             'Regions' => new PartialMatchFilter('Regions')
    );
    return new SearchContext(
    $this->class,
    $fields,
    $filters
    );
    }

    Regions is defined as a Textfield in the DB, and so it appears as a input textbox on the frontend, but I would like to use a select with specified options, as it is consists of a number of fields which are set with dropdowns.

    Is there anything I can do to $fields or something to change the type? I want to change the Textfield into a DropdownField in the front end.

    Cheers, would appreciate any help!

  • SamTheJarvis
    Avatar
    Community Member
    24 Posts

    Re: Search Text field with Dropdown. [SOLVED] Link to this post

    Solution: define field type in $searchable_fields.

       static $searchable_fields = array(
          'Type' => array(
             'title' => 'I`m looking for...',
             'filter' => 'ExactMatchfilter'),
          'Budget' => array(
             'title' => 'Within a budget of...',
             'filter' => 'ExactMatchfilter'),
          'Regions' => array(
             'title' => 'To perform in...',
             'field' => 'DropdownField',
             'filter' => 'PartialMatchfilter')
       );

    And set source/empty fields in getCustomSearchContext() {

          $fields->dataFieldByName('Regions')->setSource($this->dbObject('Region6')->enumValues());
          $fields->dataFieldByName('Regions')->setEmptyString('(Any)');

    I love silverstripe.

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Search Text field with Dropdown. [SOLVED] Link to this post

    I'm trying to do something similar here, but am having trouble implementing this solution above. Can anyone help?

    My 'Patients' DataObject contains a database field called 'Doctor'. 'Doctor' is an INT field which stores the ID of a member of the 'Doctor' group.

    In my Patient ModelAdmin, I want a searchable_field which is a dropdown where I can filter the results by the doctor.

    Ideally, the doctor will have the ID as the value of each option, and display the doctor's firstname and surname in the dropdown.

    How do I create this searchable_field dropdown box which pulls a groups members and lets you choose a doctor to restrict the results by?

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: Search Text field with Dropdown. [SOLVED] Link to this post

    @flipsidenz - Were you able to solve the problem?

    1553 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.