Set behavior of another object - detect highlighted line

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Set behavior of another object - detect highlighted line

Post by cusingerBUSCw5N » Tue Aug 07, 2012 7:31 pm

I am getting data remotely and populating a datagrid. Once the datagrid appears, I want the ability to click on a line and get a link address (which is one of the hidden data elements in my datagrid).

The code to create the datagrid works. Because the datagrid is created when you click a button on the fly, the script for that object is blank. So, I have to figure out how to add the functionality of clicking on a line within the datagrid to get a link address.

After looking through the forum, I set up another button (which will be invisible) - and put the code for clicking on items within my datagrid in it. Then I added "set the behavior of group "my DataGrid" to the long ID of btn "hidden button"" to the code that creates my datagrid.

The great news is that when you click on a line in my datagrid it goes to the hidden button! The bad news is that it doesn't pick up any information from the datagrid - returning a blank when I put in an answer line.

So...here is the code for the hidden button:

on mouseUp pBtnNum
if pBtnNum is 1 then
put the dgHilitedLines of group "my Datagrid" into theLine
answer theLine
put the dgDataOfLine[theLine] of group "my Datagrid" into theDataA
## theDataA is now an array variable.
## In the case of the Data Grid pictured above the keys of the array are Link Address and Topic

## This answer dialog will display: Link Address
answer theDataA["Link Address"]
end if
end mouseUp

You can see where I put in "answer theLine" and that's where it is blank...so because of that, it returns a blank on the second "answer theDataA".

I think I'm close - but can't figure out how to recognize the highlighted line in group "my Datagrid".

Thanks

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Set behavior of another object - detect highlighted line

Post by dunbarx » Tue Aug 07, 2012 8:21 pm

The dgHilitedLines does not have any array issues, much confusion resulting from the fact that trying to read an array directly gives an empty result.

So is there a line that is actually hilited when the script runs? If so, there is no reason that you do not get an integer when calling this property. The dg does not even need to have focus. I assume that the conditional is met.

Something silly and simple is going on here. I am an expert at dealing with that.

You know that you can set the script of any object, right? So you could load a script into your newly formed datagrid, perhaps from a custom property of the stack, and have it run from there. Might this be more compact?

You have to be careful how you determine which datagrid is to have its script set. Normally the best way to do something like that is to use the "last" keyword, but this does not work with groups, and a datagrid is nothing if not a group.

Read the note in the dictionary, appended here:

---
The "last" keyword is not stable when referring to groups. So if one creates several groups, referencing the "last" group may not return the group actual ly created last. Using the "templateGroup" is a workaround, since when creating a new group one can, for example, set the name of the templateGroup to something unique, and be able to find the last group by name. Also, trapping the "newGroup" message with an appropriate script can be used to find the last group.

This is not an issue with other objects,
---

Craig Newman

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Set behavior of another object - detect highlighted line

Post by cusingerBUSCw5N » Tue Aug 07, 2012 9:55 pm

There is definitely a line being selected when the code runs - because it only fires when I have selected a line in my DataGrid.

I'm thinking that even though my datagrid appears to be OK...that something is wrong - because it is generating a Data Grid Template every time a button is pushed... so in my application browser I currently have over 35 Data Grid Templates. So, my guess is that it is quite confused when asked to find "my DataGrid".

So - if that is the problem, I'm not sure why I have the problem - because I have code that is supposed to delete prior groups. Here is the code: (relevant part is in bold)

put url ("data from my website") into theData
replace "<br>" with return in theData
replace "usetab" with tab in theData
lock screen --when doing visual stuff, this helps to speed things up
--make sure the group does not exist already
if there is a group "my Datagrid" then
delete group "my Datagrid"
end if

--copy the template group
copy group "DataGrid" of group "Templates" of stack "revDataGridLibrary" to this card
set the name of it to "my Datagrid"
set the dgProp["style"] of group "my Datagrid" to "table"

but this is deleting a group. Perhaps I also should be deleting the grid Template???? If so, is there an example of the code?

---- with regards to your suggestion of "So you could load a script into your newly formed datagrid, perhaps from a custom property of the stack, and have it run from there."....I thought that was what I was doing by connecting it with a hidden button. I want this to go "on the fly" - so I can't manually insert a custom property - if I do, it just disappears when it creates its next datagrid.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Set behavior of another object - detect highlighted line

Post by dunbarx » Tue Aug 07, 2012 10:16 pm

But you can load the script to any and all dataGrids as you create them. There is no limit to this in any sense. It seems to me that it is more compact to have the script in the datagrid than in an external button.

For the silly thing, create a new dg from scratch, make a few columns and get some data in it. Click on a line. Now can you get a button script to yield the dgHilitedlines?

Craig

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Set behavior of another object - detect highlighted line

Post by cusingerBUSCw5N » Tue Aug 07, 2012 10:47 pm

"But you can load the script to any and all dataGrids as you create them. There is no limit to this in any sense. It seems to me that it is more compact to have the script in the datagrid than in an external button."

How?

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Set behavior of another object - detect highlighted line

Post by cusingerBUSCw5N » Tue Aug 07, 2012 10:50 pm

To clarify - I have been able to use the script successfully when I manually enter it on a card. The issue is how to do it "on the fly" - where it creates it's own dataGrid.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Set behavior of another object - detect highlighted line

Post by mwieder » Tue Aug 07, 2012 11:07 pm

I'm coming into this late, but... wow... it's not clear to me why you're trying to create a datagrid on the fly, but your technique won't work.
You not only have to copy the datagrid to the stack, you have to have the datagrid library stack available and set the behavior of the datagrid to the datagrid behavior button in that stack on the fly. Otherwise there's no chance of the datagrid actually doing anything.

But why not just have a datagrid on the card in the first place instead of trying to create a new one each time? Am I missing something significant? Once you've got the datagrid on the card you can edit its behavior with the code you want.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Set behavior of another object - detect highlighted line

Post by dunbarx » Tue Aug 07, 2012 11:21 pm

What Mark is essentially saying, why not just load the data, not the dataGrid.

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Set behavior of another object - detect highlighted line

Post by cusingerBUSCw5N » Wed Aug 08, 2012 1:27 am

You are absolutely right. I copied the code - but now that I understand the code a bit more, I didn't need the vast majority of it.
I have created a datagrid, named it, put my column names in it, and then used this code:

on mouseUp
put url ("my website with data in it only") into theData
replace "<br>" with return in theData
replace "usetab" with tab in theData
--add data
put false into firstLineAreNames
set the dgText[firstLineAreNames] of group "DataGrid 2" to theData
put the width of this stack into tStackWidth
set the dgColumnWidth["topic"] of group "DataGrid 2" to (tStackWidth - 20)
set the dgColumnIsVisible["link address"] of grp "DataGrid 2" to false
end mouseUp

Works great! Now I'm going to add the code to do the link in DataGrid 2's object script.

Thank you!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”