Search.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. define("dojox/grid/enhanced/plugins/Search", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/lang",
  4. "dojo/_base/declare",
  5. "dojo/_base/array",
  6. "dojo/data/util/filter",
  7. "../../EnhancedGrid",
  8. "../_Plugin"
  9. ], function(dojo, lang, declare, array, dFilter, EnhancedGrid, _Plugin){
  10. var Search = declare("dojox.grid.enhanced.plugins.Search", _Plugin, {
  11. // summary:
  12. // Search the grid using wildcard string or Regular Expression.
  13. // name: String
  14. // plugin name
  15. name: "search",
  16. constructor: function(grid, args){
  17. this.grid = grid;
  18. args = (args && lang.isObject(args)) ? args : {};
  19. this._cacheSize = args.cacheSize || -1;
  20. grid.searchRow = lang.hitch(this, "searchRow");
  21. },
  22. searchRow: function(/* Object|RegExp|String */searchArgs, /* function(Integer, item) */onSearched){
  23. if(!lang.isFunction(onSearched)){ return; }
  24. if(lang.isString(searchArgs)){
  25. searchArgs = dFilter.patternToRegExp(searchArgs);
  26. }
  27. var isGlobal = false;
  28. if(searchArgs instanceof RegExp){
  29. isGlobal = true;
  30. }else if(lang.isObject(searchArgs)){
  31. var isEmpty = true;
  32. for(var field in searchArgs){
  33. if(lang.isString(searchArgs[field])){
  34. searchArgs[field] = dFilter.patternToRegExp(searchArgs[field]);
  35. }
  36. isEmpty = false;
  37. }
  38. if(isEmpty){ return; }
  39. }else{
  40. return;
  41. }
  42. this._search(searchArgs, 0, onSearched, isGlobal);
  43. },
  44. _search: function(/* Object|RegExp */searchArgs, /* Integer */start, /* function(Integer, item) */onSearched, /* Boolean */isGlobal){
  45. var _this = this,
  46. cnt = this._cacheSize,
  47. args = {
  48. start: start,
  49. query: this.grid.query,
  50. sort: this.grid.getSortProps(),
  51. queryOptions: this.grid.queryOptions,
  52. onBegin: function(size){
  53. _this._storeSize = size;
  54. },
  55. onComplete: function(items){
  56. if(!array.some(items, function(item, i){
  57. if(_this._checkRow(item, searchArgs, isGlobal)){
  58. onSearched(start + i, item);
  59. return true;
  60. }
  61. return false;
  62. })){
  63. if(cnt > 0 && start + cnt < _this._storeSize){
  64. _this._search(searchArgs, start + cnt, onSearched, isGlobal);
  65. }else{
  66. onSearched(-1, null);
  67. }
  68. }
  69. }
  70. };
  71. if(cnt > 0){
  72. args.count = cnt;
  73. }
  74. this.grid._storeLayerFetch(args);
  75. },
  76. _checkRow: function(/* store item */item, /* Object|RegExp */searchArgs, /* Boolean */isGlobal){
  77. var g = this.grid, s = g.store, i, field,
  78. cells = array.filter(g.layout.cells, function(cell){
  79. return !cell.hidden;
  80. });
  81. if(isGlobal){
  82. return array.some(cells, function(cell){
  83. try{
  84. if(cell.field){
  85. return String(s.getValue(item, cell.field)).search(searchArgs) >= 0;
  86. }
  87. }catch(e){
  88. console.log("Search._checkRow() error: ", e);
  89. }
  90. return false;
  91. });
  92. }else{
  93. for(field in searchArgs){
  94. if(searchArgs[field] instanceof RegExp){
  95. for(i = cells.length - 1; i >= 0; --i){
  96. if(cells[i].field == field){
  97. try{
  98. if(String(s.getValue(item, field)).search(searchArgs[field]) < 0){
  99. return false;
  100. }
  101. break;
  102. }catch(e){
  103. return false;
  104. }
  105. }
  106. }
  107. if(i < 0){ return false; }
  108. }
  109. }
  110. return true;
  111. }
  112. }
  113. });
  114. EnhancedGrid.registerPlugin(Search/*name:'search'*/);
  115. return Search;
  116. });