Skip to content

Instantly share code, notes, and snippets.

@snetty
Created January 16, 2013 13:12
Show Gist options
  • Save snetty/4547051 to your computer and use it in GitHub Desktop.
Save snetty/4547051 to your computer and use it in GitHub Desktop.
add search to frm_checkboxlist's
<?
$dbName = $this->formGetFieldDbName($form_field->dbName, $this->viewData['form_model']);
$options = $this->formFieldGetOptions($form_field->dbName, $this->viewData['form_model']);
$list_search_prompt = isset($form_field->list_search_prompt) ? $form_field->list_search_prompt : 'Search ' . strtolower($dbName) . '...';
$list_load_indicator = isset($form_field->list_load_indicator) ? $form_field->list_load_indicator : 'phproad/resources/images/form_load_50x50.gif';
$container_id = $dbName.'_cb_container';
?>
<? if (count($options) > 10): ?>
<? if (isset($form_field->list_search_enabled) && $form_field->list_search_enabled): ?>
<div class="listSettings">
<p class="last">Select: <a href="#" onclick="$('<?= $container_id ?>').getElements('input.checkbox').each(function(cb){cb.cb_check();}); return false;">all</a>, <a href="#" onclick="$('<?= $container_id ?>').getElements('input.checkbox').each(function(cb){cb.cb_uncheck()}); return false;">none</a></p>
<div class="listSearchArea">
<div class="searchControl inactive" id="<?= $container_id ?>_search_field_ctrl">
<input type="text" name="search_field" class="search" placeholder="<?= h($list_search_prompt) ?>" value="" id="<?= $container_id ?>_search_field"/>
</div>
</div>
<script type="text/javascript">
jQuery('#<?= $container_id ?>_search_field').keypress(function(e){
if(e.which == 13){
var search_term = jQuery(this).val();
jQuery('#<?= $container_id ?> div.option').show();
if(search_term != ''){
jQuery('#<?= $container_id ?> div.option').each(function(){
var regex = new RegExp(search_term, 'i');
if(!regex.test(jQuery(this).find('label.choice span.name').text())) jQuery(this).hide();
});
}
e.preventDefault();
}
});
</script>
<div class="clear"></div>
</div>
<div class="clear"></div>
<? endif ?>
<div class="scrollableList form" id="<?= $container_id ?>">
<? endif ?>
<?
$fieldName = $form_field->dbName;
$index = 0;
foreach ($options as $value=>$name):
$index++;
$description = null;
if (is_array($name))
{
$keys = array_keys($name);
$keyNum = count($keys);
$originalName = $name;
if ($keyNum == 4)
{
$name = $originalName[0];
$description = $originalName[1];
$level = $originalName[2];
} else
{
$name = !$keyNum ? $value : $keys[0];
$description = !$keyNum ? null : $originalName[$keys[0]];
if ($keyNum == 2)
$level = $originalName[$keys[1]];
}
}
$fieldId = $index > 1 ? $form_model_class.'_'.$dbName.'_'.$index : $form_model_class.'_'.$dbName;
$fieldId = $this->formGetElementId($fieldId);
$checked = $this->formOptionState($form_field->dbName, $value, $this->viewData['form_model']);
$level = isset($level) ? $level : 0;
?>
<div class="option" style="<?= $level > 0 ? 'padding-left:'.(10*$level).'px' : null ?>">
<input <?= Phpr_Form::checkboxState($checked) ?> type="checkbox" id="<?= $fieldId ?>" name="<?= $form_model_class ?>[<?= $dbName ?>][]" value="<?= h($value) ?>" class="checkbox"/>
<label class="choice" for="<?= $fieldId ?>">
<span class="name"><?= $form_field->optionsHtmlEncode ? h($name) : $name ?></span>
<? if (strlen($description)): ?><span class="description"><?= h($description) ?></span><? endif ?>
</label>
</div>
<? endforeach ?>
<? if (count($options) > 10): ?>
</div>
<? endif ?>
<? if (!count($options) && $form_field->noOptions): ?>
<p class="last"><?= h($form_field->noOptions) ?></p>
<? endif ?>
.listSettings{
float: right;
clear: both;
margin: 3px 0 5px;
}
.listSettings p.last{
margin-top: 5px;
float: left;
}
.listSettings .listSearchArea{
float: right;
width: 50%;
margin: -5px 10px 0 0;
}
$field = $this->add_form_field('field');
$field->list_search_enabled = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment