SearchField.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. * we did some changes based on extjs's code
  7. */
  8. Ext.ns('Ext.ux.form');
  9. Ext.ux.form.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
  10. initComponent : function(){
  11. Ext.ux.form.SearchField.superclass.initComponent.call(this);
  12. this.on('specialkey', function(f, e){
  13. if(e.getKey() == e.ENTER){
  14. this.onTrigger2Click();
  15. }
  16. }, this);
  17. this.addEvents(
  18. 'beforesearch',
  19. 'search'
  20. );
  21. },
  22. /*
  23. * the fields to search, if undefined or null, then use all fields
  24. */
  25. //searchFields:{'name':true},
  26. mode:'remote',
  27. validationEvent:false,
  28. validateOnBlur:false,
  29. trigger1Class:'x-form-clear-trigger',
  30. trigger2Class:'x-form-search-trigger',
  31. hideTrigger1:true,
  32. width:180,
  33. hasSearch : false,
  34. paramName : 'query',
  35. onTrigger1Click : function(){
  36. if(this.hasSearch){
  37. this.el.dom.value = '';
  38. if(false !== this.fireEvent('beforesearch', null, this.store, this)){
  39. if('local' != this.mode){
  40. var o = {start: 0};
  41. if (this.limit) {
  42. o = {start: 0, limit: this.limit};
  43. }
  44. this.store.baseParams = this.store.baseParams || {};
  45. this.store.baseParams[this.paramName] = '';
  46. this.store.reload({params:o});
  47. }else{
  48. this.store.clearFilter();
  49. }
  50. this.fireEvent('search', null, this.store, this);
  51. }
  52. this.triggers[0].hide();
  53. this.hasSearch = false;
  54. }
  55. },
  56. onTrigger2Click : function(){
  57. var v = this.getRawValue();
  58. if(v.length < 1){
  59. this.onTrigger1Click();
  60. return;
  61. }
  62. if(false !== this.fireEvent('beforesearch', v, this.store, this)){
  63. if('local' != this.mode){
  64. var o = {start: 0};
  65. if (this.limit) {
  66. o = {start: 0, limit: this.limit};
  67. }
  68. this.store.baseParams = this.store.baseParams || {};
  69. this.store.baseParams[this.paramName] = v;
  70. this.store.reload({params:o});
  71. }else{
  72. var er = Ext.escapeRe;
  73. var reg = new RegExp('^' + er(String(v)), 'i');
  74. this.store.filterBy(function(rd, id){
  75. var fields = rd.fields;
  76. var match = false;
  77. fields.each(function(it){
  78. if(!this.searchFields || this.searchFields[it.name]){
  79. if(reg.test(rd.data[it.name])){
  80. match = true;
  81. return false;
  82. }
  83. }
  84. }, this);
  85. return match;
  86. }, this);
  87. }
  88. this.fireEvent('search', v, this.store, this);
  89. }
  90. this.hasSearch = true;
  91. this.triggers[0].show();
  92. }
  93. });