SearchField.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. store = me.store,
  44. proxy = store.getProxy(),
  45. value = me.getValue();
  46. if (value.length > 0) {
  47. // Param name is ignored here since we use custom encoding in the proxy.
  48. // id is used by the Store to replace any previous filter
  49. me.store.filter({
  50. id: me.paramName,
  51. property: me.paramName,
  52. value: value
  53. });
  54. me.hasSearch = true;
  55. me.triggerCell.item(0).setDisplayed(true);
  56. me.updateLayout();
  57. }
  58. }
  59. });