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.

Data Model Questions /

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

$searchable_fields with a default value


Go to End
Reply


8 Posts   833 Views

Avatar
sajok

Community Member, 82 Posts

18 May 2013 at 1:19am

Edited: 18/05/2013 1:20am

Hello,

I have ModelAdmin to manage "Manager" dataobject records. I have a list of $searchable_fields to filter my data object result. One of the search filters is a Status dropdown menu with "active' and "Inactive" values. By default it shows all dataobjects. What I want to see by default is only records that have active status. So far I have this code:

static $searchable_fields = array(
        'FirstName' =>array( 'title' => 'First Name' ),
        'Surname' =>array( 'title' => 'Surname' ),
        'Status' =>array( 'title' => 'Status' )
    );

any help?

Avatar
dacar

Community Member, 168 Posts

7 June 2013 at 12:04am

hi, i am also looking for a way to filter the searchresults by default. I am using the subsites module and i want to list the news of the current subsite.

Hope that someone can help, Carsten.

Avatar
zz

Community Member, 7 Posts

30 January 2015 at 9:39pm

"By default it shows all dataobjects. What I want to see by default is only records that have active status."
Bumping this thread.
Is this somehow possible? I did a lot of search, but with no success. So can I have my list of dataobjects filtered by default or apply where, without using filter?

Avatar
swaiba

Forum Moderator, 1826 Posts

2 February 2015 at 3:06am

Firstly for "Status dropdown menu with "active' and "Inactive" values" you'd better use an exact match filter because a partial match will include "Inactive" for "%active%"

	'Status'	=> array ('type' => 'ExactMatchFilter'),

Then something like this to effect a change to teh scaffolded search fields
the change being, if nothing filtered for, to default to using Active

function scaffoldSearchFields($_params = null){
	$fields = parent::scaffoldSearchFields($_params);

	$fields->fieldByName('Status')->setValue($
		isset($_REQUEST['q']) && isset($_REQUEST['q']['Status']) ? $_REQUEST['q']['Status'] : 'Active'
	);

	$this->extend('updateScaffoldSearchFields', $fields);
	return $fields;
}

Avatar
zz

Community Member, 7 Posts

3 February 2015 at 12:02am

Edited: 03/02/2015 12:03am

Thanks for reply, but this will just set a default value and you still need to use a filter. I (sajok probably too) was looking for something to get filtered list by default.

Avatar
swaiba

Forum Moderator, 1826 Posts

3 February 2015 at 12:21am

The above *does* filter by default, unless you define a filter and then it uses that (even if blank).

Avatar
zz

Community Member, 7 Posts

3 February 2015 at 4:41am

I do something wrong probably.. I used your code below with changed datafield name

$fields->fieldByName('Name')->setValue(
   isset($_REQUEST['q']) && isset($_REQUEST['q']['Name']) ? $_REQUEST['q']['Name'] : 'example'
);

but I just get 'Name: example' in a filter and all my dataobject records unfiltered, I still need to click Apply Filter to make a effect.

Avatar
swaiba

Forum Moderator, 1826 Posts

3 February 2015 at 6:36am

The code was a cut/paste/modify from product code that does as discussed. There might be something I've missed, most likely the actual field type as I've cut this from my creating a new field to replace existing with the same logic to set the value provided there has been a submission (i.e. the request).

If you want it to be done at a lower level then create an extension of SearchContext and override... (again from production code, but trimmed down to just the issue. This custom search context needs returning from the dataobject in question.

public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) {
	$dataList = parent::getQuery($searchParams, $sort, $limit, $existingQuery);

	$query->where("DataObjectName.Status = 'Active'");

	return $dataList->setDataQuery($query);;
}

I prefer to change the form as this gives more control and makes more logical sense to the user.