Ext.define('saas.util.QueryUtil', { BaseUtil: Ext.create('saas.util.BaseUtil'), // 请求页面组件接口模板 baseUrl: 'http://192.168.0.181:8560/api/ui/co_view/config?name={xtype}', // 模板替换正则 urlRe: /(.*){xtype}(.*)/g, /** * 获得form的字段配置 * @param form: form组件 */ setItems: function(form) { var me = this; debuggre; Ext.each(form.items.items, function(f){ if(f.xtype=='core-query-queryformpanel'){ }else if(f.xtype=='core-query-gridpanel'){ } }); // xtype = form.xtype, // url = me.baseUrl.replace(me.urlRe, '$1' + xtype); // me.BaseUtil.request({url}) // .then(function(response) { // var res = Ext.decode(response.responseText); // if(res.success) { // var config = res.data, items = []; // if(config) { // items = config.items; // //_baseItems // // _BaseColumn: '', // // _RelativeColumn: '', // form.add(items); // } // form.fireEvent('afterSetItems', form, items); // } // }) // .catch(function(response) { // console.error(response); // }); }, /** * 获得form的字段查询条件 * @param form: form组件 * @returns Mode: 字段模式,默认MAIN 当f.fieldMode = "DETAIL",Mode->"DETAIL" */ getStoreMode: function(form){ var Mode = "MAIN"; Ext.each(form.items.items, function(f){ if(!Ext.isEmpty(f.value)&&f.fieldMode == "DETAIL") { Mode = "DETAIL"; break; } }); return Mode; }, /** * 获得form的字段查询条件 * @param form: form组件 */ getFormCondition: function(form){ var condition = "", me = this; Ext.each(form.items.items, function(f){ if((f.xtype == 'checkbox' || f.xtype == 'radio')&&f.logic){ if(f.value == true) { if(condition == ''){ condition += "("+f.logic+")"; } else { condition += ' AND (' + f.logic+')'; } } } else if(f.xtype == 'datefield' && f.value != null && f.value != '' ){ var v = Ext.Date.format(new Date(f.value), 'Y-m-d'); if(condition == ''){ condition += "to_char("+f.name+",'yyyy-MM-dd')='"+v+"'"; } else { condition += " AND to_char("+f.name+",'yyyy-MM-dd')='"+v+"'"; } } else if(f.xtype == 'datetimefield' && f.value != null){ var v = Ext.Date.format(new Date(f.value), 'Y-m-d H:i:s'); if(condition == ''){ condition += f.name + "=to_date('" + v + "', 'yyyy-MM-dd HH24:mi:ss')"; } else { condition += ' AND ' + f.name + "=to_date('" + v + "', 'yyyy-MM-dd HH24:mi:ss')"; } } else if(f.xtype == 'numberfield' && f.value != null && f.value != ''){ var endChar = '='; if(condition == ''){ condition += f.name + endChar + f.value; } else { condition += ' AND ' + f.name + endChar + f.value; } } else if(f.xtype == 'combo' && f.value != '$ALL'){ if(f.store.data.length > 1) { if(condition == ''){ condition += '('; } else { condition += ' AND ('; } var _a = ''; f.store.each(function(d, idx){ if(d.data.value != '$ALL') { if(_a == ''){ _a += f.name +' '+ d.data.value+' ' ; } else { _a += ' OR ' + f.name +' '+ d.data.value +' '; } } }); condition += _a + ')'; } } else { if(!Ext.isEmpty(f.value)){ if(me.contains(f.value.toString(), 'BETWEEN', true) && me.contains(f.value.toString(), 'AND', true)){ if(condition == ''){ condition += f.name + " " + f.value; } else { condition += ' AND (' + f.name + " " + f.value + ")"; } } else if(me.contains(f.value.toString(), '||', true)){ var str = ''; Ext.each(f.value.split('||'), function(v){ if(v != null && v != ''){ if(str == ''){ str += f.name + "='" + v + "'"; } else { str += ' OR ' + f.name + "='" + v + "'"; } } }); if(condition == ''){ condition += "(" + str + ")"; } else { condition += ' AND (' + str + ")"; } } else if(f.value.toString().charAt(0) == '!'){ if(condition == ''){ condition += 'nvl(' + f.name + ",' ')<>'" + f.value.substr(1) + "'"; } else { condition += ' AND (nvl(' + f.name + ",' ')<>'" + f.value.substr(1) + "')"; } } else { if(f.value.toString().indexOf('%') >= 0) { if(condition == ''){ condition += f.name + " like '" + f.value + "'"; } else { condition += ' AND (' + f.name + " like '" + f.value + "')"; } } else { if(condition == ''){ condition += '('+f.name + "='" + f.value + "')"; } else { condition += ' AND (' + f.name + "='" + f.value + "')"; } } } } } }); return condition; }, /** * string:原始字符串 * substr:子字符串 * isIgnoreCase:忽略大小写 */ contains: function(string, substr, isIgnoreCase){ if (string == null || substr == null) return false; if (isIgnoreCase === undefined || isIgnoreCase === true) { string = string.toLowerCase(); substr = substr.toLowerCase(); } return string.indexOf(substr) > -1; }, /** * */ queryData: function(grid,condition){ var me = this; if(Ext.isEmpty(condition)){ condition =" 1=1 "; } var params = { keyword: condition,number:1,size:15 }; var dataRes = { url :grid.listUrl, async:false, params:params }; me.BaseUtil.request(dataRes) .then(function(response) { var res = Ext.decode(response.responseText); if(res.success) { var data = res.data; console.log(data); grid.getStore().loadData(data.list); grid.fireEvent('afterLoadData', grid, data.list); } }) .catch(function(response) { console.log(response); // something... }); }, onQuery: function(parentForm,queryMoreForm){ var me = this, grid = parentForm.down('grid'), queryForm = parentForm.down('form'), condition = me.getFormCondition(queryForm), Mode = me.getStoreMode(queryForm); if(queryMoreForm){//更多查询 if(!Ext.isEmpty(condition)){ condition = " and ( "+me.getStoreMode(queryMoreForm)+" ) "; }else{ condition = me.getStoreMode(queryMoreForm); } if(Mode=="MAIN"){ me.getStoreMode(queryMoreForm); } } console.log("condition:"+condition); condition = " 1=1 "; if(Mode=="MAIN"){ console.log("Mode:"+Mode+",查询当前列表"); grid.loadPage(1); }else{ console.log("Mode:"+Mode+",查询当前关联列表"); //若明细字段含明细字段注意切换数据源 grid.reconfigure(store, columns); //关联viewName = 关联viewName+"-RelativeGrid" grid.loadPage(1); } }, add10EmptyRow: function(grid) { var store = grid.getStore(), selectedRecord = grid.selModel.lastSelected, datas = []; Ext.Array.each(new Array(10), function() { datas.push({}); }) store.insert(store.indexOf(selectedRecord) + 1, datas); }, /** * 获取grid被选数据 * grid:原始字符串 */ getGridSelected:function(grid){ var items = grid.selModel.getSelection(), data = new Array(); Ext.each(items, function(item, index){ if(!Ext.isEmpty(item.data[grid._idField])&&!Ext.isEmpty(item.data[grid._codeField])){ var o = new Object(); if(grid._idField){ o['id'] = item.data[grid._idField]; } if(grid._codeField){ o['code'] = item.data[grid._codeField]; } data.push(o); } }); return data; }, /** * 发起批量操作请求 * grid:原始字符串 */ vastRes:function(url,params){ var me = this; me.BaseUtil.request({ url: url, params: JSON.stringify(params), method: 'POST', }) .then(function() { Ext.Msg.alert('提示','操作成功'); }) .catch(function(response) { console.log(response); Ext.Msg.alert('提示','操作失败'); }); } });