3.2.0

need help with filter based on input value

I have a 2.5.1 application working and seek to add a button and input box under grid. Below is the code being used. Something is wrong.

In this simple app I set the variable text1 to a 3 char string. when the user clicks button1, I want the grid to display only rows where the value of text1 -- as entered by the user -- appear in the grid.

I have something out of sequence? Can someone help?

actually my interest is to then generalize this --
table.getData(4, i).substr(0,3) == text1.substr(0,3)
so that the full string table.getData(4, i) is searched for the exact characters text1. -- for example the user enter NV and rows are displayed that have the string NV anywhere in a cell in column 4.

Thank you.

======>
var text1 =" Al";

function showselected1(){
var i, rows = [];
var max = table.getCount();

for (i=0; i<max; i++){
if (table.getData(4, i).substr(0,3) == text1.substr(0,3)){
rows.push(i);
}
}
obj.clearRowModel();
obj.clearSortModel();
obj.clearScrollModel();

obj.setRowCount(rows.length);
obj.setRowIndices(rows);
}

var button1 = new AW.UI.Button;
button1.setControlText("Find in Name:");
button1.onClick = showselected1;
document.write(button1);

var input1 = new AW.UI.Input;
input1.setControlText(text1);
document.write(input1);
Dave
April 10,
You just have a "few" lines wrong:

- replace the line
if (table.getData(4, i).substr(0,3) == text1.substr(0,3)){
with this one as Alex already suggested:
if (table.getData(4, i).indexOf(text1) > -1){


- I would also use:
button1.onControlClicked = function() {showselected1() };
instead of:
button1.onClick = showselected1;

-You might also need some of:
var text1 input1.getControlText();

-declare "max" variable outside the function to avoid loosing the initial rowcount.

-And apply obj.setRowIndices(''); if need to filter again ( or reset filtering).

the three lines with obj.clearXXXModel(); are only neccesary in case you change data-source or the number of columns variation, so unless doing any of it is not needed.

So your sample code should look like:
var max = table.getCount();
function showselected1(searchcriteria){
var i, rows = [];
if(searchcriteria==''){
obj.setRowCount(max);
obj.setRowIndices('');
}
else{
for (i=0; i<max; i++){
if (table.getData(4, i).indexOf(searchcriteria) > -1){
rows.push(i);
}
}
obj.setRowCount(rows.length);
obj.setRowIndices(rows);
}
}

var button1 = new AW.UI.Button;
button1.setControlText("Find in Name:");
button1.onControlClicked = function(){showselected1(input1.getControlText())};
document.write(button1);

var input1 = new AW.UI.Input;
input1.setControlText(text1);
document.write(input1);

HTH
Carlos
April 10,
Thank you.

When the Find button is clicked, there are no matches and all rows go away. How is searchcriteria getting set? What am I missing here?
Dave
April 10,
Here is where this stands now; after trying several things, it is almost identical to your code. When Find is clicked, the grid empties.

var text1 = '';
var searchcriteria = '';

var max = table.getCount();
function showselected1(searchcriteria){
var i, rows = [];
if(searchcriteria==''){
obj.setRowCount(max);
obj.setRowIndices('');
}
else{
for (i=0; i<max; i++){
if (table.getData(4, i).indexOf(searchcriteria) > -1){
rows.push(i);
}
}
obj.setRowCount(rows.length);
obj.setRowIndices(rows);
}
}

var button1 = new AW.UI.Button;
button1.setControlText("Find in Name:");
button1.onControlClicked = function(){showselected1(input1.getControlText())};
document.write(button1);

var input1 = new AW.UI.Input;
input1.setControlText(text1);
document.write(input1);
Dave
April 10,
Uppss , sorry Dave, did not test the above code, I just change a few
things from an identical array-Data-Model working sample.
I'll try to find a good one for you.

-I found a mistake ( but is not affecting global functionality)
input1.setControlText(text1);
should be :
input1.setControlText('');
or :
input1.setControlText(" Al";);

-searchcriteria is set by this call:
button1.onControlClicked = function(){showselected1(input1.getControlText())};
so is equal to current value of input1.getControlText()
so not need to declare it.
Carlos
April 10,
I fixed these and no difference. If the text1 entered is City (literally those 4 characters) and one row contains the text Dodge City in that column, that row should display, yes?

Thank you.
Dave
April 10,
Of course, it should.
I found it, the mistaken line is:
if (table.getData(4, i).indexOf(searchcriteria) > -1){
and should be:
if (table.getCellValue(4, i).indexOf(searchcriteria) > -1){

You can find a complete sample in:
http://www.activewidgets.com/javascript.forum.8149.8/filtering-rows-example.html
HTH
April 10,
You can also use (depending on how recordsets and/or formated arriving datasets) table.getCellData (Cell missed by Alex)
Carlos
April 10,
thank you. it still does not work. here is the present code (it is col 3, not 4, but I have been testing on many all along):

how can I put a debugging step or two in this code.?

your example (other ref) is similar but different. there must be some tiny thing here that I cannot see.

var text1 = '';

var max = table.getCount();
function showselected1(searchcriteria){
var i, rows = [];
if(searchcriteria==''){
obj.setRowCount(max);
obj.setRowIndices('');
}
else{
for (i=0; i<max; i++){
if (table.getcellvalue(3, i).indexOf(searchcriteria) > -1){
rows.push(i);
}
}
obj.setRowCount(rows.length);
obj.setRowIndices(rows);
}
}

var button1 = new AW.UI.Button;
button1.setControlText("Find in Name:");
button1.onControlClicked = function(){showselected1(input1.getControlText())};
document.write(button1);

var input1 = new AW.UI.Input;
input1.setControlText('Al');
document.write(input1);




Dave
April 10,
Hmmm,
table.getCellValue(
instead of:
table.getcellvalue(

capitals matters (sometimes) ;-)
April 10,
Alex,
Trying to "reset" the filter applyed in your sample :
/javascript.forum.8149.8/filtering-rows-example.html
I added the code below, and notice that a second variable (max2) is needed to get the grid back to its original state (unfiltered).
(just because max var maintains the previous filter rows-count)

Is there another way to treat this?
Thanks
var max = table.getCount();
var max2 = max;

function filter2(searchcriteria){ 
    var i, rows = [], max = obj.getRowCount(); 
if(searchcriteria==''){
   obj.setRowCount(max2); 
   obj.setRowIndices(''); 
}
if(searchcriteria!=''){
    for (i=0; i<max; i++){ 
  if (obj.getCellValue(4, i).indexOf(searchcriteria) >-1 ){ 
            rows.push(i); 
        } 
    } 
    obj.setRowCount(rows.length); 
    obj.setRowIndices(rows); 
} 
}

var button1 = new AW.UI.Button;
button1.setControlText("Find in Name:");
button1.onControlClicked = function(){ filter2( input1.getControlText()); }
document.write(button1);

var input1 = new AW.UI.Input;
input1.setControlText("2");
document.write(input1);
Carlos
April 11,
Hooray!! Thank you. That does it. You are a genius!
Dave
April 11,

This topic is archived.

See also:


Back to support forum