[Solved] Datagrid Table Checkbox

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
Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

[Solved] Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 10:50 am

EDIT:
The following issue has been resolved. As many people have been having the same issues, I am writing a tutorial on this and a few other elements to go into DG's and will post here for comment so hopefully there will be a one-stop place for all this.
XdM

Hi Brainstrust.
I must be missing something again. I have searched the forum and it seems like I am not the only one who has had this trouble, so I don't feel too bad tonight!
I am writing a stack for some building data for energy efficiency ratings. At the start, users will enter data about the construction of rooms (wall, floor, roof) into a datagrid and then on each subsequent card selected parts of that data will be recalled, and entered into datagrids on each card. So, when doing stuff with walls, the room name and wall data will appear, but not floor and roof data. That data will then be used to determine how much insulation etc is required based on Construction Code regulations.
So far, functionality is running very well. Datagrids being populated with correct data, compared to references and calculations are working perfectly.
Now, I am doing a section on skylights (roof windows).
I have a datagrid, and I am using it as a table (not a form). It will import the room name and room area, create a column with a checkbox to say if that room has a skylight that is "required by code", and 2 columns that will be populated with calculated data.
My problem seems to be with the checkbox column, as others have too.
I have managed to fumble through the DG documentation and lessons, and found how to change the column template to add a checkbox. This has worked. Currently, I have only the lines with associated data (Room Name, Room Area) as shown in the image below:
RL Datagrid.jpg
by using the following code in the column behaviour:

Code: Select all

on FillInData pData
   set the label of button "check" of me to pData
end FillInData
The checkboxes work, but I can't seem to find a way to check if the checkbox on that line is checked or not.
If I use the following code as per the lesson on Datagrid FORMS (not the table that I am using)

Code: Select all

on FillInData pDataArray
   set the label of button "Check" of me to pDataArray["label"]
   set the hilited of button "Check" of me to pDataArray["checked"]
end FillInData
I get 2 issues:
a) All of the lines of the column get checkboxes, and;
b) I get the following error message:
RL Datagrid-2.jpg
Firstly... what am I doing wrong? It has to be something simple...
Secondly... when I get that sorted out, how to I code to call up whether the checkbox is ticked or not?
I'll be using code something like this:

Code: Select all

on mousedown
   repeat with x = 1 to the dgnumberoflines of group "rooflightdata"
      put the dgDataofline[x] of group "RoofLightData" into DataArray
      if DataArray["NCC 3.8 Req"] is true then --I need to check if the hilited of button "Check" in this column is true or false--
         do something
      end if
   end repeat
end mousedown
Any help would be much appreciated.
Thanks in advance.
XdM
Livecode Community, Windows 10.
Last edited by Xero on Wed Aug 18, 2021 12:04 am, edited 1 time in total.

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 11:07 am

Hi Xero,

Not sure I understand fully but be aware that the hilite of the button 'checkbox' is a boolean, whereas pDataArray["checked"] is likely a string.

therefore, in the fillInData handler, you would say something like

Code: Select all

if pDataArray["checked"] is 1 then // or "true" or whatever value is being stored
  set the hilite of button "check" of me to true
else 
  set the hilite of button "check" of me to false
end if
or some such anyway - you're post was a bit long, sorry if I didn't fully read and misunderstood...

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 11:13 am

Or you can shorten this even further.
If pDataArray["checked"] stores the value "true" when ticked (or replace this with whatever you're using) then you can as use the boolean that is the outcome of the equality comparison:

Code: Select all

(pDataArray["checked"] = "true")
will evaluate to either true or false.

therefore you can shorten the above to:

Code: Select all

set the hilite of button "check" to (pDataArray["checked"] = "true")
hope that helps,
Stam

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 11:26 am

Thanks Stam,
Sorry about the long post, but there's a bit going on in the stack...
OK... I added the FillInData code:

Code: Select all

   set the hilite of button "check" to (pData["checked"] = "true")
into the column behaviour, and I no longer get the error messages, and also get the right amount of check boxes.
So I think this has fixed it, thanks a lot!

The rest of my question is... how do I run through each line of the DG and see if the checkbox is checked or not?
I will have a button that will go through each line and check if the box is checked (true) or not (false), and will then do some stuff...
This is the code sample I am using...

Code: Select all

on mousedown
   repeat with x = 1 to the dgnumberoflines of group "rooflightdata"
      put the dgDataofline[x] of group "RoofLightData" into DataArray
      if DataArray["NCC 3.8 Req"] is true then --I need to check if the hilited of button "Check" in this column is true or false--
         do something
      end if
   end repeat
end mousedown
Any help?
Thanks in advance.
XdM

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 11:41 am

I would say that if you need to work on data in a Datagrids, don't think of this like a spreadsheet - so don't iterate lines.
Instead, keep in mind that the entire data of the grid is stored both as text (the dgText of group "grid") and as an array (the dgData of group "grid"). It's much quicker and easier to work with one of those - my preference is with arrays.

The other thing I would mention is that not all repeat loops are equal.

if you have a large number of elements then repeat for performs much faster than repeat with x = 1 to...
There are situations where I need an index anyway. If I can use the repeat for loop, I also include a viable x and start the loop with 'add 1 to x' (if x has no value to start with, it will become 1 and increment). But it doesn't look like you need an index?

From what I can see, I would say:

Code: Select all

on mouseUp
  local tDataA -- I always use strict compilation mode, so declare variables at top, helps with typos etc
  put the dgData of group "rooflightdata" into tDataA -- now you can work with the data of a the Datagrid directly as an array

  repeat for each element tElement in tDataA -- tElement now contains the the data corresponding to the index (not line)
    if tElement["NCC 3.8 Req"] then -- check if tElement["checked"] = "true" (rather than the highlighted of the button)
       -- do something
    end if
  end repeat
end mouseUp
You can do something similar with dgText as well If you prefer. Lots of ways to skin this particular cat...

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 12:09 pm

Thanks for the advice Stam.
I am more familiar with the idea of a spreadsheet, and this one is acting a lot like a spreadsheet.
If the checkbox is ticked (true), I will be adjusting data on the same line only, so it makes sense to me to stay with the dgdataofline mode.
If the box is ticked, there will be one set of data entered into the final 2 columns of the same line. I don't want the checkbox on one line to affect columns on another line...
Anyway, I have used your code, and also a similar method in mine, and neither code creates an output of any kind.
There's still something missing...

Code: Select all

on FillInData pData
   set the label of button "check" of me to pData
   set the hilite of button "check" to (pData["checked"] = "true")
     --is this pData["Checked"] the issue? Are we not referencing it? Above, it's just pData, but here, it's pData["checked"]...
end FillInData
XdM

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 12:25 pm

Sorry Xero, not sure I'm understanding what the problem you're trying to solve is.

If you require the the acting on the checkbox hilite to modify the last 2 columns, you would not do this in a separate button (although you can). In this case you would use the fillInData handler rather than a mouseDown or mouseUp handler.

Now if you want to affect the last two columns of that line you would
a) make sure you have created a behaviour for those columns and
b) edit the fillInData of those behaviours, by referencing pDataArray -- this cannot affect other lines. eg for each of the last 2 columns you would have edit the behaviour:

Code: Select all

on fillInData
  ...
  if pDataArray["checked"] = "true" then 
      // do stuff
    else
      // do other stuff
  end if
  ...
end fillInData
If you are editing the checkbox and wish to update this behaviour you can

Code: Select all

dispatch "refreshLine" to group "rooflightdata" with tLine
where tLine is the line number (eg. the dgHilitedLine of group "rooflightdata").

Again apologies if I've misunderstood the problem you're trying to solve...

PS: It's not a massive mental leap to think of the array as a spreadsheet - working with the dgText or dgData of the grid is almost invariable faster and less glitchy. In dgData, the 'line' is the index and the 'columns' are the keys. In dgText, the line is the line and the columns are tab-separated. It's all a tabular format.

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 12:53 pm

Yeah, I think we're on different pages Stam! Ha ha ha.
I see where you're going, but I will have a separate button outside of the DG that will fill in data. There is a lot of dependant states, and if some data is filled in but not others, and it's done in a different order, there will be trouble. It's easiest for me to update the whole DG by button.
I can do that easily though, I am doing it elsewhere with an established, working code.
My problem is that no matter what I do, when the checkbox is ticked, I can't get the checkbox to tell me it's ticked...
In the DG lesson (for form), there's a section on a mouseup event:

Code: Select all

on mouseUp pMouseBtnNum
	if pMouseBtnNum is 1 then
		## did they click on the checkbox?
		if the short name of the target is "Check" then
			## Update internal value in data grid
			SetDataOfLine the dgLine of me, "checked", the hilite of the target
		end if
	end if
end mouseUp
I gather this is where we assign the value of the checkbox to the pDataArray["checked"]? I am out of my league here!!!
Do we need to define this as Index rather than Line? "setDataofIndex the dgIndex of me"?
Even with this, I can't get any value from the checkbox...!?!
I feel pretty dumb right now! I am sure it's something simple!
XdM

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 1:11 pm

Ok I see what you mean - I think! I personally would create a global array as a data model for this and data hasn't been filled in just put it down as empty and use this to populate data grids etc. But that's just me!

If you want to use the code you quote, assuming the button's short name is 'check' then you can easily test to see if you can trap the changes but just adding some code after using setDataOfLine:

Code: Select all

on mouseUp pMouseBtnNum
	if pMouseBtnNum is 1 then
		## did they click on the checkbox?
		if the short name of the target is "Check" then
			## Update internal value in data grid
			SetDataOfLine the dgLine of me, "checked", the hilite of the target
			
			put the dataOfLine[the dgLine of me] into tData
			if tData["checked"] = true then 
			  answer "Checked"
			else
			  answer "Not Checked"
		        end if 
			 
		end if
	end if
end mouseUp

Not tested, so may well not work ;)
Assuming this works, you can replace that little if/else/end if statement with code you need

Hope this helps
S.

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 1:29 pm

Code didn't work, but this did:

Code: Select all

on mouseUp pMouseBtnNum
   if pMouseBtnNum is 1 then
      if the short name of the target is "Check" then
         SetDataOfLine the dgLine of me, "checked", the hilite of the target
      end if
   end if
   
   put the dgdataOfLine of me into tData
   if tData["checked"] = true then 
      answer "Checked"
   else
      answer "Not Checked"
   end if 
end mouseUp
But always responds "Not Checked" regardless of checked status of box. It's not capturing the checking of the box... troublesome.
XdM

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Sun Aug 15, 2021 1:56 pm

Hi Xero,

where did you put that handler?

I put the following handler in the behaviour script for a cell that contains the checkbox:

Code: Select all

on mouseUp
    if the short name of the target = "check" then 
        if the hilite of button "Check" of me then
            answer "checked"
        else
            answer "not checked"
        end if
    end if
end mouseUp
This works as expected every time I click on the checkbox.

But keep in mind this probably won't work when setting the checkbox by code, instead of clicking on it

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Sun Aug 15, 2021 2:04 pm

I have been working off this lesson:
https://lessons.livecode.com/m/datagrid ... -data-grid
But have been using a Table, not a form...
I have put the handler in the Column Behaviour, as per the section of the lesson called "Update Data Grid Row Value When User Changes Checkbox State". The column containes the checkboxes, named "Check", names exactly as the lesson...
XdM

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Datagrid Table Checkbox

Post by Xero » Mon Aug 16, 2021 8:40 am

So, what I want to do is this:
https://lessons.livecode.com/m/4068/l/4 ... n-a-column
But this is set up for the DataGrid Helper plugin that I don't have, and it builds the script automatically and doesn't show me what's in the script.
I am still having trouble getting the checkbox to pass it's status (true/false) into the array for the Datagrid...
I think the script in the lesson would be what I want.
Can anyone help?
XdM

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Datagrid Table Checkbox

Post by stam » Mon Aug 16, 2021 4:24 pm

I have given advice on what i would do, more or less... but that's me and there are may ways to achieve the same thing.

I'll admit i purchased the DG helper some time ago, before i properly got into LiveCode because i didn't quite understand the datagrid. In the last year i've been using LC a heck of a lot more, and the datagrid makes much more sense. I pretty much never use that plugin (and when i do have the time it seems to introduce instabilities, so i actively avoid tbh).

It might be helpful to you to see a small stack in action. I attach a stack that may do what you want. It will set the last column to a value calculated by the tick box. You can add new rows ticked or unticked and if you tick on the tick box it changes the last row respectively.

It's NOT the most elegant code as threw this together in a couple of minutes to illustrate a point. Probably can be refined a whole lot more, but shows one way to get stuff done with a tick box in the data grid.

If you're still struggling, perhaps post a stack hilighting your particular problem?

Anyway, hope you find this helpful
Stam

PS: The useful code resides in the behaviour scripts of columns 'check' and 'calc'. Button scripts are in the buttons.
Attachments
tickbox in data grid.livecode.zip
(8.49 KiB) Downloaded 120 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”