Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dropdown rendering speed #10838

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Forms/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\Map;
use ArrayAccess;
use SilverStripe\Core\Convert;

/**
* Represents a field that allows users to select one or more items from a list
*/
abstract class SelectField extends FormField
{
private static $casting = [
'OptionsHTML' => 'HTMLFragment',
];

/**
* Associative or numeric array of all dropdown items,
Expand Down Expand Up @@ -271,4 +275,33 @@ public function castedCopy($classOrCopy)
}
return $field;
}

/**
* Provides <option> list for the template
* This allows for a faster rendering of dropdowns with lots of options
*
* @return string
*/
public function OptionsHTML(): string
{
// Some methods only exists for single selects
$source = $this->hasMethod('getSourceEmpty') ? $this->getSourceEmpty() : $this->getSource();
$emptyString = $this->hasMethod('getEmptyString') ? $this->getEmptyString() : '';

$currentValue = $this->Value();
foreach ($source as $value => $title) {
$selected = '';
if ($this->isSelectedValue($value, $currentValue)) {
$selected = ' selected="selected"';
}
$disabled = '';
if ($this->isDisabledValue($value) && $title != $emptyString) {
$disabled = ' disabled="disabled"';
}
$item = '<option value="' . Convert::raw2xml($value) . '"' . $selected . $disabled . '>' . Convert::raw2xml($title) . '</option>';
$options[] = $item;
}

return implode("\n", $options);
}
}
8 changes: 1 addition & 7 deletions templates/SilverStripe/Forms/DropdownField.ss
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<select $AttributesHTML>
<% loop $Options %>
<option value="$Value.XML"
<% if $Selected %> selected="selected"<% end_if %>
<% if $Disabled %> disabled="disabled"<% end_if %>
><% if $Title.exists %>$Title.XML<% else %>&nbsp;<% end_if %>
</option>
<% end_loop %>
$OptionsHTML
</select>
Loading