SearchField.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Ext.define('Ext.ux.form.SearchField', {
  2. extend: 'Ext.form.field.Trigger',
  3. alias: 'widget.searchfield',
  4. trigger1Cls: Ext.baseCSSPrefix + 'form-clear-trigger',
  5. trigger2Cls: Ext.baseCSSPrefix + 'form-search-trigger',
  6. hasSearch : false,
  7. paramName : 'query',
  8. initComponent: function() {
  9. var me = this;
  10. me.callParent(arguments);
  11. me.on('specialkey', function(f, e){
  12. if (e.getKey() == e.ENTER) {
  13. me.onTrigger2Click();
  14. }
  15. });
  16. // We're going to use filtering
  17. me.store.remoteFilter = true;
  18. // Set up the proxy to encode the filter in the simplest way as a name/value pair
  19. // If the Store has not been *configured* with a filterParam property, then use our filter parameter name
  20. if (!me.store.proxy.hasOwnProperty('filterParam')) {
  21. me.store.proxy.filterParam = me.paramName;
  22. }
  23. me.store.proxy.encodeFilters = function(filters) {
  24. return filters[0].value;
  25. }
  26. },
  27. afterRender: function(){
  28. this.callParent();
  29. this.triggerCell.item(0).setDisplayed(false);
  30. },
  31. onTrigger1Click : function(){
  32. var me = this;
  33. if (me.hasSearch) {
  34. me.setValue('');
  35. me.store.clearFilter();
  36. me.hasSearch = false;
  37. me.triggerCell.item(0).setDisplayed(false);
  38. me.updateLayout();
  39. }
  40. },
  41. onTrigger2Click : function(){
  42. var me = this,
  43. value = me.getValue();
  44. if (value.length > 0) {
  45. // Param name is ignored here since we use custom encoding in the proxy.
  46. // id is used by the Store to replace any previous filter
  47. me.store.filter({
  48. id: me.paramName,
  49. property: me.paramName,
  50. value: value
  51. });
  52. me.hasSearch = true;
  53. me.triggerCell.item(0).setDisplayed(true);
  54. me.updateLayout();
  55. }
  56. }
  57. });