Page 1 of 1
Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 2:37 pm
by lohill
I wanted a function to hilite the first row of a data grid that contained any characters the user entered and I wanted it to be a case sensitive search. After hiliting the row, I would then like the the function to continue with the next row as long as the search argument is not changed. I do not care that the function find a second occurrence in the same row, only another row. If the argument is changed the search should start again at the top and if the find is not successful then the hilite should return to the top with a beep. Here is what I have:
Code: Select all
function FieldFind tSearch
global gToFind
global gLastLine
set the caseSensitive to true
if tSearch <> gToFind then
put 0 into gLastLine
put tSearch into gToFind
end if
put the dgText of group "DataGrid 1" into tText
add 1 to gLastLine
put the number of lines of tText into tNumLines
repeat with i=gLastLine to tNumLines
put line i of tText into tLine
put offset(gToFind,tLine) into nPos
if nPos > 0 then
put i into gLastLine
exit repeat
end if
end repeat
if i >= tNumLines then
put 0 into gLastLine
beep 1
end if
return gLastLine
end FieldFind
This function seems to work find as long as the characters I want to find are near the top pf the data grid. As I search for things later down the grid, the line that gets hilited is one or two lines below the line that should be hilited. What am I doing wrong? ( I won't dismiss the fact that LiveCode could be having a problem since when I drug down the left side if the IDE to select the code to display here, one of the lines did not select and was missing from what I wanted to paste to this forum. But that is another rant...)
Larry
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 2:52 pm
by Klaus
Hi Larry,
looks OK, can you please also post the calling script?
Best
Klaus
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 3:15 pm
by lohill
Here it is Klaus. It is from my edit menu.
Code: Select all
on menuPick pWhich
switch pWhich
case "Cut"
cut
break
case "Copy"
copy
break
case "Paste"
If the clipboard is "text" then paste
break
case "Clear"
delete
break
case "Undo"
undo
break
case "Find"
global gToFind
global gLastLine
put the focusedObject into tFocused
ask question "What would you like to find?" with gToFind
put it into tAnswer
if answer is empty then exit menuPick
put 0 into glastLine
put FieldFind(tAnswer) into tLine
If tLine <> 0 then
set the dghilitedLines of group "DataGrid 1" to tLine
else
set the dghilitedLines of group "DataGrid 1" to 1
beep 1
end if
break
case "Find Next"
global gToFind
global gLastLine
put FieldFind(gToFind) into tLine
If tLine <> 0 then
set the dghilitedLines of group "DataGrid 1" to tLine
else
set the dghilitedLines of group "DataGrid 1" to 1
end if
break
case "Preferences"
global gRadiDBName
set the dialogData to gRadiDBName
modal stack "Preferences Dialog"
set the dialogdata to ""
break
end switch
end menuPick
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 3:25 pm
by Klaus
OK, thanks.
Do both items "Find" and "Find gain" show the same problems you decsribe?
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 3:52 pm
by lohill
I have been concentrating on the Find but a quick test of the Find Next seems to display the same thing.
Thanks Klaus,
Larry
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 6:01 pm
by Klaus
Hi Larry,
aha, as I said, looks OK!?
Could you eventually post a (stripped down) stack here?
Best
Klaus
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 6:35 pm
by Dixie
Try using 'lineOffset' to find the line you are looking for in the dgText
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 6:37 pm
by lohill
Klaus,
I'll give it a try. Thanks .
Larry
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 7:22 pm
by lohill
Klaus,
You are so smart. In trying to get you a 'stripped down' stack, I have discovered the error of my ways. (You knew that was going to happen!) It seems that my dataGrid has a few rows that have a column where there is more than 1 line in the value, thus the extra line offsets. I am not going to be able to use dgText but instead go to a bit more work and use dgData.
I'm sorry for making you do so much work but thanks again for being there for help.
Larry
Re: Function to Find in a Line of a DataGrid
Posted: Sun May 18, 2014 10:31 pm
by lohill
Well so much for dgData. The index of a line and the number of a line have no relation to each other so any lines found are not found sequently. I went back to dgText and have something pretty inefficient but LiveCode doesn't seem to blink at the inefficiency. I guess I could put the "compressed" text in an invisible field but then I would have to make sure it was always recalculated when any sorting was done.
Code: Select all
function FieldFind tSearch
global gToFind
global gLastLine
set the caseSensitive to true
set the itemdelimiter to tab
if tSearch <> gToFind then
put 0 into gLastLine
put tSearch into gToFind
end if
put the dgText of group "DataGrid 1" into tText
put empty into tCompressed
put the number of lines of tText into tLines
repeat with i=1 to tLines
put line i of tText into tLine
put item 1 of tLine into tKey
if isnumber(tKey) then
put tLine & return after tCompressed
else
put tab & tLine after the last line of tCompressed
end if
end repeat
add 1 to gLastLine
put the number of lines of tCompressed into tNumLines
repeat with i=gLastLine to tNumLines
put line i of tCompressed into tLine
put offset(gToFind,tLine) into nPos
if nPos > 0 then
put i into gLastLine
exit repeat
end if
end repeat
if i >= tNumLines then
put 0 into gLastLine
beep 1
end if
return gLastLine
end FieldFind
Re: Function to Find in a Line of a DataGrid
Posted: Tue Aug 12, 2014 12:51 pm
by sritcp
You can also search the DataGrid directly.
Put the following in the script of the DataGrid; this will highlight all the rows containing your search string.
Code: Select all
command searchMe tString
local tIndexList, tRecord, tHLIndexes
put the dgIndexes of me into tIndexList
repeat for each item tIndex in tIndexList
put getDataOfIndex(tIndex) into tRecord
repeat for each element tElement in tRecord
if tElement contains tString then
put tIndex & comma after tHLIndexes
exit repeat
end if
end repeat
end repeat
delete the last char of tHLIndexes
set the dgHilitedIndexes of me to tHLIndexes
end searchMe
You can trigger it with a button whose code could be
Code: Select all
on mouseUp
local tSearchString
ask "Search Term?"
if it is not empty then put it into tSearchString
dispatch "searchMe" to group "DataGrid1" with tSearchString
end mouseUp
Regards,
Sri
Re: Function to Find in a Line of a DataGrid
Posted: Mon Aug 18, 2014 6:47 pm
by lohill
Thanks for this Sri. Sorry it took me so long to get back to it.
Regards,
Larry
Re: Function to Find in a Line of a DataGrid
Posted: Mon Jun 29, 2015 9:39 pm
by sphere
Thank you Sri!
This works perfect and super fast !
Cheers, Sphere