SearchField.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. Ext.define('erp.view.core.trigger.SearchField', {
  2. extend : 'Ext.form.field.Trigger',
  3. alias : 'widget.searchfield',
  4. triggerCls: 'x-form-search-trigger',
  5. hasSearch : false,
  6. paramName : 'query',
  7. fieldStyle: 'color:#B0C4DE;background: #eeeeee;border: 1px solid #cccccc;-moz-box-shadow:1px 1px 2px rgba(0,0,0,.2); -webkit-box-shadow:1px 1px 2px rgba(0,0,0,.2); box-shadow:1px 1px 2px rgba(0,0,0,.2); -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;',
  8. focusCls: 'x-form-field-cir',
  9. emptyText: $I18N.common.main.quickSearch,
  10. enableKeyEvents : true,
  11. listeners : {
  12. specialkey : function(field, e){
  13. if(e.getKey() == Ext.EventObject.ENTER){
  14. this.onTriggerClick();
  15. }
  16. }
  17. },
  18. autoShow:true,
  19. initComponent : function() {
  20. this.callParent(arguments);
  21. },
  22. onTriggerClick: function(){
  23. var f = this, tree = Ext.getCmp('tree-panel');
  24. if(f.value == '' || f.value == null){
  25. tree.getTreeRootNode(0);
  26. return;
  27. }
  28. tree.setLoading(true, tree.body);
  29. Ext.Ajax.request({//拿到tree数据
  30. url : basePath + 'common/searchTree.action',
  31. params: {
  32. search: f.value
  33. },
  34. callback : function(options,success,response){
  35. tree.setLoading(false);
  36. var res = new Ext.decode(response.responseText);
  37. if(res.tree){
  38. var searchKeys = Ext.util.Cookies.get('searchKeys');
  39. if(searchKeys == null || searchKeys.length == 0){
  40. //searchKeys = new Array();//cookie存放数组很麻烦,就用字符串,中间用||隔开
  41. searchKeys = f.value;
  42. } else {
  43. var bool = true;
  44. Ext.Array.each(searchKeys.split('||'), function(k){
  45. if(k == f.value){//已存在
  46. bool = false;
  47. }
  48. });
  49. if(bool){
  50. searchKeys = searchKeys + "||" + f.value;
  51. }
  52. }
  53. //Ext.util.Cookies.set('searchKeys', searchKeys);//把用户的搜索条件作为关键字存入cookie
  54. var root = tree.getRootNode();
  55. root.removeAll();
  56. var fn = function(node, ch) {
  57. for(var i in ch) {
  58. var n = ch[i], chs = n.children;
  59. n.children = [];
  60. n.expanded = true;
  61. var d = node.appendChild(n);
  62. if(d && chs && chs.length > 0 && String(chs) != '[]') {
  63. fn(d, chs);
  64. }
  65. }
  66. };
  67. fn(root, res.tree);
  68. } else if(res.redirectUrl){
  69. window.location.href = res.redirectUrl;
  70. } else if(res.exceptionInfo){
  71. Ext.Msg.alert("ERROR:" + res.exceptionInfo);
  72. }
  73. }
  74. });
  75. }
  76. });