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 8 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
40 changes: 40 additions & 0 deletions src/Forms/DropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace SilverStripe\Forms;

use SilverStripe\Core\Convert;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\ORM\FieldType\DBHTMLText;

/**
* Dropdown field, created from a select tag.
Expand Down Expand Up @@ -124,6 +126,39 @@ public function getHasEmptyDefault()
return parent::getHasEmptyDefault() || $this->Required();
}

/**
* Provides <option> list for the template
* This allows for a faster rendering of dropdowns with lots of options
*
* @return string
*/
public function renderOptionsHTML(): string
lekoala marked this conversation as resolved.
Show resolved Hide resolved
{
// 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) {
// It's an opt group, ignore since it has its own template
if (is_array($title)) {
continue;
}
$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);
}

/**
* @param array $properties
* @return string
Expand All @@ -141,6 +176,11 @@ public function Field($properties = [])
'Options' => new ArrayList($options)
]);

// Without this, changing the source will render the previous list due to cache
$OptionsHTML = new DBHTMLText('Options');
$OptionsHTML->setValue($this->renderOptionsHTML());
$properties['OptionsHTML'] = $OptionsHTML;

return parent::Field($properties);
}
}
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>