Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Search Text field with Dropdown. [SOLVED]


Go to End


4 Posts   3765 Views

Avatar
SamTheJarvis

Community Member, 24 Posts

23 June 2011 at 10:07pm

Edited: 23/06/2011 11:10pm

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!

Avatar
SamTheJarvis

Community Member, 24 Posts

24 June 2011 at 12:20am

Edited: 24/06/2011 12:21am

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.

Avatar
flipsidenz

Community Member, 49 Posts

20 September 2011 at 5:44pm

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?

Avatar
Craftnet

Community Member, 58 Posts

30 December 2012 at 4:25pm

@flipsidenz - Were you able to solve the problem?