Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Created May 21, 2011 21:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Mark-H/984931 to your computer and use it in GitHub Desktop.
Save Mark-H/984931 to your computer and use it in GitHub Desktop.
Fancy MODX / ExtJS combo-box that searches while typing and adds new entries if they don't exist.
// Call the form with the unique xtype
items: [{
xtype: 'campermgmt-newcamper-form-brandscombo',
fieldLabel: 'Merknaam',
name: 'brand',
id: 'brand',
allowBlank: false,
vtype: 'alphanum'
}
<...>
// Declare the actual xtype
CamperMgmt.formGeneral.BrandsCombo = function(config) {
config = config || {};
Ext.applyIf(config,{
url: CamperMgmt.config.connectorUrl,
baseParams: {
action: 'mgr/index/getbrands'
},
fields: ['id','name'],
// Editabel makes you able of typing in the combobox
editable: true,
// typeAhead sends posts for every character you type in (starting at minChars) to search for existing matches.
typeAhead: true,
minChars: 1,
// disabling forceSelection allows you to use a non-existent value
forceSelection: false
});
CamperMgmt.formGeneral.BrandsCombo.superclass.constructor.call(this,config);
};
Ext.extend(CamperMgmt.formGeneral.BrandsCombo,MODx.combo.ComboBox);
Ext.reg('campermgmt-newcamper-form-brandscombo',CamperMgmt.formGeneral.BrandsCombo);
// This file is posted to when the entire form gets submitted, and does the magic of adding the field if it doesn't exist.
// Find the object based on the "brand" posted value
$brandObj = $modx->getObject('cmBrand',array('name' => $scriptProperties['brand']));
// If it is not empty (ie it exists), simply add it to $c, which is the camper record
if (!empty($brandObj)) {
$c->addOne($brandObj);
}
// If it doesn't exist, create it and add it to the camper record.
else {
$brandObj = $modx->newObject('cmBrand');
$brandObj->set('name',$scriptProperties['brand']);
$brandObj->save();
$c->addOne($brandObj);
}
// This file actually searches for the search term, and is being posted to on every character typed in
// Get the search query. Note that I put it in $search cause I already used $query as the xPDOQuery object
$search = $modx->getOption('query',$scriptProperties,'');
$query = $modx->newQuery('cmBrand');
$query->sortby($sort,$dir);
// If there's a search request, tell modx to find it
if ($search !== '') {
$query->where(array(
'name:LIKE' => '%'.$search.'%'
));
}
//Output results as JSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment