/*
*
* The filter uses JSON entities to hold filter rules and groups. Here is an example of a filter:
{ "groupOp": "AND",
"groups" : [
{ "groupOp": "OR",
"rules": [
{ "field": "name", "op": "eq", "data": "England" },
{ "field": "id", "op": "le", "data": "5"}
]
}
],
"rules": [
{ "field": "name", "op": "eq", "data": "Romania" },
{ "field": "id", "op": "le", "data": "1"}
]
}
*/
/*jshint eqeqeq:false, eqnull:true, devel:true */
/*global jQuery, define */
(function( factory ) {
"use strict";
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define([
"jquery",
"./grid.base",
"./grid.common"
], factory );
} else {
// Browser globals
factory( jQuery );
}
}(function( $ ) {
"use strict";
//module begin
$.fn.jqFilter = function( arg ) {
if (typeof arg === 'string') {
var fn = $.fn.jqFilter[arg];
if (!fn) {
throw ("jqFilter - No such method: " + arg);
}
var args = $.makeArray(arguments).slice(1);
return fn.apply(this,args);
}
var p = $.extend(true,{
filter: null,
columns: [],
sortStrategy: null,
onChange : null,
afterRedraw : null,
checkValues : null,
error: false,
errmsg : "",
errorcheck : true,
showQuery : true,
sopt : null,
ops : [],
operands : null,
numopts : ['eq','ne', 'lt', 'le', 'gt', 'ge', 'nu', 'nn', 'in', 'ni'],
stropts : ['eq', 'ne', 'bw', 'bn', 'ew', 'en', 'cn', 'nc', 'nu', 'nn', 'in', 'ni'],
strarr : ['text', 'string', 'blob'],
groupOps : [{ op: "AND", text: "AND" }, { op: "OR", text: "OR" }],
groupButton : true,
ruleButtons : true,
uniqueSearchFields : false,
direction : "ltr",
addsubgrup : "Add subgroup",
addrule : "Add rule",
delgroup : "Delete group",
delrule : "Delete rule",
autoencode : false
}, $.jgrid.filter, arg || {});
return this.each( function() {
if (this.filter) {return;}
this.p = p;
// setup filter in case if they is not defined
if (this.p.filter === null || this.p.filter === undefined) {
this.p.filter = {
groupOp: this.p.groupOps[0].op,
rules: [],
groups: []
};
}
// Sort the columns if the sort strategy is provided.
if (this.p.sortStrategy != null && $.isFunction(this.p.sortStrategy)) {
this.p.columns.sort(this.p.sortStrategy);
}
var i, len = this.p.columns.length, cl,
isIE = /msie/i.test(navigator.userAgent) && !window.opera;
// translating the options
this.p.initFilter = $.extend(true,{},this.p.filter);
// set default values for the columns if they are not set
if( !len ) {return;}
for(i=0; i < len; i++) {
cl = this.p.columns[i];
if( cl.stype ) {
// grid compatibility
cl.inputtype = cl.stype;
} else if(!cl.inputtype) {
cl.inputtype = 'text';
}
if( cl.sorttype ) {
// grid compatibility
cl.searchtype = cl.sorttype;
} else if (!cl.searchtype) {
cl.searchtype = 'string';
}
if(cl.hidden === undefined) {
// jqGrid compatibility
cl.hidden = false;
}
if(!cl.label) {
cl.label = cl.name;
}
if(cl.index) {
cl.name = cl.index;
}
if(!cl.hasOwnProperty('searchoptions')) {
cl.searchoptions = {};
}
if(!cl.hasOwnProperty('searchrules')) {
cl.searchrules = {};
}
if(cl.search === undefined) {
cl.inlist = true;
} else {
cl.inlist = cl.search;
}
}
var getGrid = function () {
return $("#" + $.jgrid.jqID(p.id))[0] || null;
},
$tg = getGrid(),
classes = $.jgrid.styleUI[($tg.p.styleUI || 'jQueryUI')].filter,
common = $.jgrid.styleUI[($tg.p.styleUI || 'jQueryUI')].common;
if(this.p.showQuery) {
$(this).append("
");
}
/*
*Perform checking.
*
*/
var checkData = function(val, colModelItem) {
var ret = [true,""], $t = getGrid();
if($.isFunction(colModelItem.searchrules)) {
ret = colModelItem.searchrules.call($t, val, colModelItem);
} else if($.jgrid && $.jgrid.checkValues) {
try {
ret = $.jgrid.checkValues.call($t, val, -1, colModelItem.searchrules, colModelItem.label);
} catch (e) {}
}
if(ret && ret.length && ret[0] === false) {
p.error = !ret[0];
p.errmsg = ret[1];
}
};
/* moving to common
randId = function() {
return Math.floor(Math.random()*10000).toString();
};
*/
this.onchange = function ( ){
// clear any error
this.p.error = false;
this.p.errmsg="";
return $.isFunction(this.p.onChange) ? this.p.onChange.call( this, this.p ) : false;
};
/*
* Redraw the filter every time when new field is added/deleted
* and field is changed
*/
this.reDraw = function() {
$("table.group:first",this).remove();
var t = this.createTableForGroup(p.filter, null);
$(this).append(t);
if($.isFunction(this.p.afterRedraw) ) {
this.p.afterRedraw.call(this, this.p);
}
};
/*
* Creates a grouping data for the filter
* @param group - object
* @param parentgroup - object
*/
this.createTableForGroup = function(group, parentgroup) {
var that = this, i;
// this table will hold all the group (tables) and rules (rows)
var table = $("
");
table.append(tr);
// this header will hold the group operator type and group action buttons for
// creating subgroup "+ {}", creating rule "+" or deleting the group "-"
var th = $("
");
tr.append(th);
if(this.p.ruleButtons === true) {
// dropdown for: choosing group operator type
var groupOpSelect = $("");
th.append(groupOpSelect);
// populate dropdown with all posible group operators: or, and
var str= "", selected;
for (i = 0; i < p.groupOps.length; i++) {
selected = group.groupOp === that.p.groupOps[i].op ? " selected='selected'" :"";
str += "";
}
groupOpSelect
.append(str)
.on('change',function() {
group.groupOp = $(groupOpSelect).val();
that.onchange(); // signals that the filter has changed
});
}
// button for adding a new subgroup
var inputAddSubgroup ="";
if(this.p.groupButton) {
inputAddSubgroup = $("");
inputAddSubgroup.on('click',function() {
if (group.groups === undefined ) {
group.groups = [];
}
group.groups.push({
groupOp: p.groupOps[0].op,
rules: [],
groups: []
}); // adding a new group
that.reDraw(); // the html has changed, force reDraw
that.onchange(); // signals that the filter has changed
return false;
});
}
th.append(inputAddSubgroup);
if(this.p.ruleButtons === true) {
// button for adding a new rule
var inputAddRule = $(""), cm;
inputAddRule.on('click',function() {
//if(!group) { group = {};}
if (group.rules === undefined) {
group.rules = [];
}
for (i = 0; i < that.p.columns.length; i++) {
// but show only serchable and serchhidden = true fields
var searchable = (that.p.columns[i].search === undefined) ? true: that.p.columns[i].search,
hidden = (that.p.columns[i].hidden === true),
ignoreHiding = (that.p.columns[i].searchoptions.searchhidden === true);
if ((ignoreHiding && searchable) || (searchable && !hidden)) {
cm = that.p.columns[i];
break;
}
}
if( !cm ) {
return false;
}
var opr;
if( cm.searchoptions.sopt ) {opr = cm.searchoptions.sopt;}
else if(that.p.sopt) { opr= that.p.sopt; }
else if ( $.inArray(cm.searchtype, that.p.strarr) !== -1 ) {opr = that.p.stropts;}
else {opr = that.p.numopts;}
group.rules.push({
field: cm.name,
op: opr[0],
data: ""
}); // adding a new rule
that.reDraw(); // the html has changed, force reDraw
// for the moment no change have been made to the rule, so
// this will not trigger onchange event
return false;
});
th.append(inputAddRule);
}
// button for delete the group
if (parentgroup !== null) { // ignore the first group
var inputDeleteGroup = $("");
th.append(inputDeleteGroup);
inputDeleteGroup.on('click',function() {
// remove group from parent
for (i = 0; i < parentgroup.groups.length; i++) {
if (parentgroup.groups[i] === group) {
parentgroup.groups.splice(i, 1);
break;
}
}
that.reDraw(); // the html has changed, force reDraw
that.onchange(); // signals that the filter has changed
return false;
});
}
// append subgroup rows
if (group.groups !== undefined) {
for (i = 0; i < group.groups.length; i++) {
var trHolderForSubgroup = $("
");
table.append(trHolderForSubgroup);
var tdFirstHolderForSubgroup = $("
");
trHolderForSubgroup.append(tdFirstHolderForSubgroup);
var tdMainHolderForSubgroup = $("
");
tdMainHolderForSubgroup.append(this.createTableForGroup(group.groups[i], group));
trHolderForSubgroup.append(tdMainHolderForSubgroup);
}
}
if(group.groupOp === undefined) {
group.groupOp = that.p.groupOps[0].op;
}
// append rules rows
var suni = that.p.ruleButtons && that.p.uniqueSearchFields, ii;
if( suni ) {
for ( ii = 0; ii < that.p.columns.length; ii++) {
if(that.p.columns[ii].inlist) {
that.p.columns[ii].search = true;
}
}
}
if (group.rules !== undefined) {
for (i = 0; i < group.rules.length; i++) {
table.append(
this.createTableRowForRule(group.rules[i], group)
);
if( suni ) {
var field = group.rules[i].field;
for ( ii = 0; ii < that.p.columns.length; ii++) {
if(field === that.p.columns[ii].name) {
that.p.columns[ii].search = false;
break;
}
}
}
}
}
return table;
};
/*
* Create the rule data for the filter
*/
this.createTableRowForRule = function(rule, group ) {
// save current entity in a variable so that it could
// be referenced in anonimous method calls
var that=this, $t = getGrid(), tr = $("
"),
//document.createElement("tr"),
// first column used for padding
//tdFirstHolderForRule = document.createElement("td"),
i, op, trpar, cm, str="", selected;
//tdFirstHolderForRule.setAttribute("class", "first");
tr.append("
");
// create field container
var ruleFieldTd = $("
");
tr.append(ruleFieldTd);
// dropdown for: choosing field
var ruleFieldSelect = $(""), ina, aoprs = [];
ruleFieldTd.append(ruleFieldSelect);
ruleFieldSelect.on('change',function() {
if( that.p.ruleButtons && that.p.uniqueSearchFields ) {
var prev = parseInt($(this).data('curr'),10),
curr = this.selectedIndex;
if(prev >= 0 ) {
that.p.columns[prev].search = true;
$(this).data('curr', curr);
that.p.columns[curr].search = false;
}
}
rule.field = $(ruleFieldSelect).val();
trpar = $(this).parents("tr:first");
$(".data",trpar).empty();
for (i=0;i"+that.p.ops[ina].text+"";
so++;
}
}
$(".selectopts",trpar).empty().append( s );
$(".selectopts",trpar)[0].selectedIndex = 0;
if( $.jgrid.msie() && $.jgrid.msiever() < 9) {
var sw = parseInt($("select.selectopts",trpar)[0].offsetWidth, 10) + 1;
$(".selectopts",trpar).width( sw );
$(".selectopts",trpar).css("width","auto");
}
// data
$(".data",trpar).append( elm );
$.jgrid.bindEv.call($t, elm, cm.searchoptions);
$(".input-elm",trpar).on('change',function( e ) {
var elem = e.target;
if( cm.inputtype === 'custom' && $.isFunction(cm.searchoptions.custom_value) ) {
rule.data = cm.searchoptions.custom_value.call($t, $(".customelement", this), 'get');
} else {
rule.data = $(elem).val();
}
if(cm.inputtype === 'select' && cm.searchoptions.multiple ) {
rule.data = rule.data.join(",");
}
that.onchange(); // signals that the filter has changed
});
setTimeout(function(){ //IE, Opera, Chrome
rule.data = $(elm).val();
that.onchange(); // signals that the filter has changed
}, 0);
});
// populate drop down with user provided column definitions
var j=0;
for (i = 0; i < that.p.columns.length; i++) {
// but show only serchable and serchhidden = true fields
var searchable = (that.p.columns[i].search === undefined) ? true: that.p.columns[i].search,
hidden = (that.p.columns[i].hidden === true),
ignoreHiding = (that.p.columns[i].searchoptions.searchhidden === true);
if ((ignoreHiding && searchable) || (searchable && !hidden)) {
selected = "";
if(rule.field === that.p.columns[i].name) {
selected = " selected='selected'";
j=i;
}
str += "";
}
}
ruleFieldSelect.append( str );
ruleFieldSelect.data('curr', j);
// create operator container
var ruleOperatorTd = $("
");
tr.append(ruleOperatorTd);
cm = p.columns[j];
// create it here so it can be referentiated in the onchange event
//var RD = that.createElement(rule, rule.data);
cm.searchoptions.id = $.jgrid.randId();
if(isIE && cm.inputtype === "text") {
if(!cm.searchoptions.size) {
cm.searchoptions.size = 10;
}
}
cm.searchoptions.name = rule.field;
cm.searchoptions.oper = 'filter';
var ruleDataInput = $.jgrid.createEl.call($t, cm.inputtype,cm.searchoptions, rule.data, true, that.p.ajaxSelectOptions || {}, true);
if(rule.op === 'nu' || rule.op === 'nn') {
$(ruleDataInput).attr('readonly','true');
$(ruleDataInput).attr('disabled','true');
} //retain the state of disabled text fields in case of null ops
// dropdown for: choosing operator
var ruleOperatorSelect = $("");
ruleOperatorTd.append(ruleOperatorSelect);
ruleOperatorSelect.on('change',function() {
rule.op = $(ruleOperatorSelect).val();
trpar = $(this).parents("tr:first");
var rd = $(".input-elm",trpar)[0];
if (rule.op === "nu" || rule.op === "nn") { // disable for operator "is null" and "is not null"
rule.data = "";
if(rd.tagName.toUpperCase() !== 'SELECT') { rd.value = ""; }
rd.setAttribute("readonly", "true");
rd.setAttribute("disabled", "true");
} else {
if(rd.tagName.toUpperCase() === 'SELECT') { rule.data = rd.value; }
rd.removeAttribute("readonly");
rd.removeAttribute("disabled");
}
that.onchange(); // signals that the filter has changed
});
// populate drop down with all available operators
if( cm.searchoptions.sopt ) {op = cm.searchoptions.sopt;}
else if(that.p.sopt) { op= that.p.sopt; }
else if ($.inArray(cm.searchtype, that.p.strarr) !== -1) {op = that.p.stropts;}
else {op = that.p.numopts;}
str="";
$.each(that.p.ops, function() { aoprs.push(this.oper); });
for ( i = 0; i < op.length; i++) {
ina = $.inArray(op[i],aoprs);
if(ina !== -1) {
selected = rule.op === that.p.ops[ina].oper ? " selected='selected'" : "";
str += "";
}
}
ruleOperatorSelect.append( str );
// create data container
var ruleDataTd = $("
");
tr.append(ruleDataTd);
// textbox for: data
// is created previously
//ruleDataInput.setAttribute("type", "text");
ruleDataTd.append(ruleDataInput);
$.jgrid.bindEv.call($t, ruleDataInput, cm.searchoptions);
$(ruleDataInput)
.addClass("input-elm " + classes.srInput )
.on('change', function() {
rule.data = cm.inputtype === 'custom' ? cm.searchoptions.custom_value.call($t, $(".customelement", this),'get') : $(this).val();
that.onchange(); // signals that the filter has changed
});
// create action container
var ruleDeleteTd = $("
");
tr.append(ruleDeleteTd);
// create button for: delete rule
if(this.p.ruleButtons === true) {
var ruleDeleteInput = $("");
ruleDeleteTd.append(ruleDeleteInput);
//$(ruleDeleteInput).html("").height(20).width(30).button({icons: { primary: "ui-icon-minus", text:false}});
ruleDeleteInput.on('click',function() {
// remove rule from group
for (i = 0; i < group.rules.length; i++) {
if (group.rules[i] === rule) {
group.rules.splice(i, 1);
break;
}
}
that.reDraw(); // the html has changed, force reDraw
that.onchange(); // signals that the filter has changed
return false;
});
}
return tr;
};
this.getStringForGroup = function(group) {
var s = "(", index;
if (group.groups !== undefined) {
for (index = 0; index < group.groups.length; index++) {
if (s.length > 1) {
s += " " + group.groupOp + " ";
}
try {
s += this.getStringForGroup(group.groups[index]);
} catch (eg) {alert(eg);}
}
}
if (group.rules !== undefined) {
try{
for (index = 0; index < group.rules.length; index++) {
if (s.length > 1) {
s += " " + group.groupOp + " ";
}
s += this.getStringForRule(group.rules[index]);
}
} catch (e) {alert(e);}
}
s += ")";
if (s === "()") {
return ""; // ignore groups that don't have rules
}
return s;
};
this.getStringForRule = function(rule) {
var opUF = "",opC="", i, cm, ret, val,
numtypes = ['int', 'integer', 'float', 'number', 'currency']; // jqGrid
for (i = 0; i < this.p.ops.length; i++) {
if (this.p.ops[i].oper === rule.op) {
opUF = this.p.operands.hasOwnProperty(rule.op) ? this.p.operands[rule.op] : "";
opC = this.p.ops[i].oper;
break;
}
}
for (i=0; i 1) {
if (group.groupOp === "OR") {
s += " || ";
}
else {
s += " && ";
}
}
s += getStringForGroup(group.groups[index]);
}
}
if (group.rules !== undefined) {
for (index = 0; index < group.rules.length; index++) {
if (s.length > 1) {
if (group.groupOp === "OR") {
s += " || ";
}
else {
s += " && ";
}
}
s += getStringRule(group.rules[index]);
}
}
s += ")";
if (s === "()") {
return ""; // ignore groups that don't have rules
}
return s;
}
return getStringForGroup(this.p.filter);
};
// Here we init the filter
this.reDraw();
if(this.p.showQuery) {
this.onchange();
}
// mark is as created so that it will not be created twice on this element
this.filter = true;
});
};
$.extend($.fn.jqFilter,{
/*
* Return SQL like string. Can be used directly
*/
toSQLString : function()
{
var s ="";
this.each(function(){
s = this.toUserFriendlyString();
});
return s;
},
/*
* Return filter data as object.
*/
filterData : function()
{
var s;
this.each(function(){
s = this.p.filter;
});
return s;
},
getParameter : function (param) {
if(param !== undefined) {
if (this.p.hasOwnProperty(param) ) {
return this.p[param];
}
}
return this.p;
},
resetFilter: function() {
return this.each(function(){
this.resetFilter();
});
},
addFilter: function (pfilter) {
if (typeof pfilter === "string") {
pfilter = $.jgrid.parse( pfilter );
}
this.each(function(){
this.p.filter = pfilter;
this.reDraw();
this.onchange();
});
}
});
$.extend($.jgrid,{
filterRefactor : function ( p ) {
/*ruleGroup : {}, ssfield:[], splitSelect:",", groupOpSelect:"OR"*/
var filters={} /*?*/, rules, k, rule, ssdata, group;
try {
filters = typeof p.ruleGroudp === "string" ? $.jgrid.parse(p.ruleGroup) : p.ruleGroup;
if(filters.rules && filters.rules.length) {
rules = filters.rules;
for(k=0; k < rules.length; k++) {
rule = rules[k];
if($.inArray(rule.filed, p.ssfield)) {
ssdata = rule.data.split(p.splitSelect);
if(ssdata.length > 1) {
if(filters.groups === undefined) {
filters.groups = [];
}
group = { groupOp: p.groupOpSelect, groups: [], rules: [] };
filters.groups.push(group);
$.each(ssdata,function(l) {
if (ssdata[l]) {
group.rules.push({ data: ssdata[l], op: rule.op, field: rule.field});
}
});
rules.splice(k, 1);
k--;
}
}
}
}
} catch(e) {}
return filters;
}
});
$.jgrid.extend({
filterToolbar : function(p){
var regional = $.jgrid.getRegional(this[0], 'search');
p = $.extend({
autosearch: true,
autosearchDelay: 500,
searchOnEnter : true,
beforeSearch: null,
afterSearch: null,
beforeClear: null,
afterClear: null,
onClearSearchValue : null,
url : '',
stringResult: false,
groupOp: 'AND',
defaultSearch : "bw",
searchOperators : false,
resetIcon : "x",
splitSelect : ",",
groupOpSelect : "OR",
operands : { "eq" :"==", "ne":"!","lt":"<","le":"<=","gt":">","ge":">=","bw":"^","bn":"!^","in":"=","ni":"!=","ew":"|","en":"!@","cn":"~","nc":"!~","nu":"#","nn":"!#", "bt":"..."}
}, regional , p || {});
return this.each(function(){
var $t = this;
if($t.p.filterToolbar) { return; }
if(!$($t).data('filterToolbar')) {
$($t).data('filterToolbar', p);
}
if($t.p.force_regional) {
p = $.extend(p, regional);
}
var classes = $.jgrid.styleUI[($t.p.styleUI || 'jQueryUI')].filter,
common = $.jgrid.styleUI[($t.p.styleUI || 'jQueryUI')].common,
base = $.jgrid.styleUI[($t.p.styleUI || 'jQueryUI')].base,
triggerToolbar = function() {
var sdata={}, j=0, v, nm, sopt={},so, ms = false, ssfield = [], bbt =false, sop;
$.each($t.p.colModel,function(){
var $elem = $("#gs_"+ $t.p.idPrefix + $.jgrid.jqID(this.name), (this.frozen===true && $t.p.frozenColumns === true) ? $t.grid.fhDiv : $t.grid.hDiv);
nm = this.index || this.name;
sop = this.searchoptions || {};
if(p.searchOperators && sop.searchOperMenu) {
so = $elem.parent().prev().children("a").attr("soper") || p.defaultSearch;
} else {
so = (sop.sopt) ? sop.sopt[0] : this.stype==='select' ? 'eq' : p.defaultSearch;
}
v = this.stype === "custom" && $.isFunction(sop.custom_value) && $elem.length > 0 ?
sop.custom_value.call($t, $elem, "get") :
$elem.val();
// detect multiselect
if(this.stype === 'select' && sop.multiple && $.isArray(v) && v.length) {
ms = true;
ssfield.push(nm);
v= v.length === 1 ? v[0] : v;
}
if(so==="bt") {
bbt = true;
}
if(v || so==="nu" || so==="nn") {
sdata[nm] = v;
sopt[nm] = so;
j++;
} else {
try {
delete $t.p.postData[nm];
} catch (z) {}
}
});
var sd = j>0 ? true : false;
if(p.stringResult === true || $t.p.datatype === "local" || p.searchOperators === true)
{
var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
var gi=0;
$.each(sdata,function(i,n){
if (gi > 0) {ruleGroup += ",";}
ruleGroup += "{\"field\":\"" + i + "\",";
ruleGroup += "\"op\":\"" + sopt[i] + "\",";
n+="";
ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
gi++;
});
ruleGroup += "]}";
// multiselect
var filters, rules, k,str, rule, ssdata, group;
if(ms) {
$.jgrid.filterRefactor({
ruleGroup : ruleGroup,
ssfield : ssfield,
splitSelect : p.splitSelect,
groupOpSelect : p.groupOpSelect
});
//ruleGroup = JSON.stringify( filters );
}
if(bbt) {
if(!$.isPlainObject(filters)) {
filters = $.jgrid.parse(ruleGroup);
}
if(filters.rules && filters.rules.length) {
rules = filters.rules;
for(k=0;k < rules.length; k++) {
rule = rules[k];
if(rule.op === "bt") {
ssdata = rule.data.split("...");
if(ssdata.length > 1) {
if(filters.groups === undefined) {
filters.groups = [];
}
group = { groupOp: 'AND', groups: [], rules: [] };
filters.groups.push(group);
$.each(ssdata,function(l) {
var btop = l === 0 ? 'ge' : 'le';
str = ssdata[l];
if(str) {
group.rules.push({ data: ssdata[l], op: btop, field: rule.field});
}
});
rules.splice(k, 1);
k--;
}
}
}
}
}
if(bbt || ms ) {
ruleGroup = JSON.stringify( filters );
}
$.extend($t.p.postData,{filters:ruleGroup});
$.each(['searchField', 'searchString', 'searchOper'], function(i, n){
if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
});
} else {
$.extend($t.p.postData,sdata);
}
var saveurl;
if(p.url) {
saveurl = $t.p.url;
$($t).jqGrid("setGridParam", { url: p.url });
}
var bsr = $($t).triggerHandler("jqGridToolbarBeforeSearch") === 'stop' ? true : false;
if(!bsr && $.isFunction(p.beforeSearch)){bsr = p.beforeSearch.call($t);}
if(!bsr) { $($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]); }
if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
$($t).triggerHandler("jqGridToolbarAfterSearch");
if($.isFunction(p.afterSearch)){p.afterSearch.call($t);}
},
clearToolbar = function(trigger){
var sdata={}, j=0, nm;
trigger = (typeof trigger !== 'boolean') ? true : trigger;
$.each($t.p.colModel,function(){
var v, $elem = $("#gs_"+$t.p.idPrefix+$.jgrid.jqID(this.name),(this.frozen===true && $t.p.frozenColumns === true) ? $t.grid.fhDiv : $t.grid.hDiv);
if(this.searchoptions && this.searchoptions.defaultValue !== undefined) {
v = this.searchoptions.defaultValue;
}
nm = this.index || this.name;
switch (this.stype) {
case 'select' :
$elem.find("option").each(function (i){
if(i===0) { this.selected = true; }
if ($(this).val() === v) {
this.selected = true;
return false;
}
});
if ( v !== undefined ) {
// post the key and not the text
sdata[nm] = v;
j++;
} else {
try {
delete $t.p.postData[nm];
} catch(e) {}
}
break;
case 'text':
$elem.val(v || "");
if(v !== undefined) {
sdata[nm] = v;
j++;
} else {
try {
delete $t.p.postData[nm];
} catch (y){}
}
break;
case 'custom':
if ($.isFunction(this.searchoptions.custom_value) && $elem.length > 0 ) {
this.searchoptions.custom_value.call($t, $elem, "set", v || "");
}
break;
}
});
var sd = j>0 ? true : false;
$t.p.resetsearch = true;
if(p.stringResult === true || $t.p.datatype === "local") {
var ruleGroup = "{\"groupOp\":\"" + p.groupOp + "\",\"rules\":[";
var gi=0;
$.each(sdata,function(i,n){
if (gi > 0) {ruleGroup += ",";}
ruleGroup += "{\"field\":\"" + i + "\",";
ruleGroup += "\"op\":\"" + "eq" + "\",";
n+="";
ruleGroup += "\"data\":\"" + n.replace(/\\/g,'\\\\').replace(/\"/g,'\\"') + "\"}";
gi++;
});
ruleGroup += "]}";
$.extend($t.p.postData,{filters:ruleGroup});
$.each(['searchField', 'searchString', 'searchOper'], function(i, n){
if($t.p.postData.hasOwnProperty(n)) { delete $t.p.postData[n];}
});
} else {
$.extend($t.p.postData,sdata);
}
var saveurl;
if(p.url) {
saveurl = $t.p.url;
$($t).jqGrid("setGridParam",{url:p.url});
}
var bcv = $($t).triggerHandler("jqGridToolbarBeforeClear") === 'stop' ? true : false;
if(!bcv && $.isFunction(p.beforeClear)){bcv = p.beforeClear.call($t);}
if(!bcv) {
if(trigger) {
$($t).jqGrid("setGridParam",{search:sd}).trigger("reloadGrid",[{page:1}]);
}
}
if(saveurl) {$($t).jqGrid("setGridParam",{url:saveurl});}
$($t).triggerHandler("jqGridToolbarAfterClear");
if($.isFunction(p.afterClear)){p.afterClear();}
},
toggleToolbar = function(){
var trow = $("tr.ui-search-toolbar",$t.grid.hDiv);
if($t.p.frozenColumns === true) {
$($t).jqGrid('destroyFrozenColumns');
}
if(trow.css("display") === 'none') {
trow.show();
} else {
trow.hide();
}
if($t.p.frozenColumns === true) {
$($t).jqGrid("setFrozenColumns");
}
},
buildRuleMenu = function( elem, left, top ){
$("#sopt_menu").remove();
left=parseInt(left,10);
top=parseInt(top,10) + 18;
var fs = $('.ui-jqgrid-view').css('font-size') || '11px';
var str = '