Dipen Shah Dipen Shah - 4 months ago 65
Javascript Question

How can I get the selected search filter in onCellSelect in free jqGrid?

I have a grid in which I am providing search when the user highlights the text they want to filter.

The

onCellSelect
looks like this:

onCellSelect: function(row, col, content, event) {
var cm = grid.jqGrid("getGridParam", "colModel");
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
selectionColumn = cm[col].name;
selection.toString() !== '' && $("#gs_"+selectionColumn).val(selection.toString());
console.log($("a.soptclass[data-colname='"+selectionColumn+"']").attr('data-soper'));
if(selection.toString() != '')
{
grid[0].triggerToolbar();
}

}


Now I have some search operators which I have customized and using it in the grid:

searchoptions:{sopt:["cn",'mc','mn',"eq","ne","lt","le","gt","ge","bw","ew","nc"]}


The
mc
and
mn
are a part of
customSortOperations
.

Now what I want is when the user selects some text inside a specific cell inside a grid, I want to detect which search filter was used. For example by default the search filter is
cn
.

I have tried this:

$("a.soptclass[data-colname='"+selectionColumn+"']").attr('data-soper')


but it gives me the default
cn
everytime.

I can get the text inside the link which will give me a symbolic name like
~
for
cn
,
==
for
eq
with

$("a.soptclass[data-colname='"+selectionColumn+"']").text()


However is there a jqgrid way of rather getting the exact search operator selected? i.e.
cn
,
eq
,
ne
,
le
, etc

Please let me know if a working demo is required and I will update the question.

UPDATE: DEMO.

On line 659 and 660 I am using this callback
$("a.soptclass[data-colname='"+selectionColumn+"']").text()


In other words I want the selected search operator inside
onCellSelect

Answer

I still not full understand what exact behavior you want to implement, but it seems that you can start the onCellSelect code with the following:

onCellSelect: function(row, col, content, event) {
    var p = $(this).jqGrid("getGridParam");
    var hDiv = p.frozenColumns === true && p.colModel[col].frozen === true ?
                this.grid.fhDiv : this.grid.hDiv;
    var $elem = $(hDiv).find("#gs_" + $.jgrid.jqID(p.id + "_" + p.colModel[col].name));
    var oper = $elem.parent().prev().children("a").data("soper");
    ...
}

The $elem uses the standard id behavior of the current free jqGrid implementation (no idMode option of filterToolbar is specified). The element $elem is the <input> or <select> element in the filter toolbar. You can use $elem(selection) to change the value. The oper variable contains the currently chosen searching operation. One should use .data("soper") instead of .attr("data-soper") to access the data.

I hope it's what you are missing currently.