check box selection in data grid is wrong

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
keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

check box selection in data grid is wrong

Post by keram » Fri Mar 07, 2014 6:00 pm

Hello,

I have a strange problem with toggling of checkboxes in a data grid but only in a specific view. This is different from what I posted previously (different from this one: 'check box selection in data grid and on a separate card').

In the attached stack each text line is assigned to a category; Short, Medium or Long.
You can select its checkbox and also view the entire text line in 1-line view by clicking on the line.
Toggling the checkboxes in All Lines view and My Categories view and their corresponding 1-line views is working properly. But not in My Categories view and its 1-line view.

Follow these steps to see how it works and how it does not work:

1. In the Categories select Long and Short
2. Click on All Lines button
3. Select for example the checkboxes for these lines: 4, 6, 8, 12, 13, 14, 17
4. When you click on line 4 in All Lines view you'll go to 1-line view. In that view you can toggle the checkbox selection and the corresponding checkbox in All Line view will be toggled and it's working properly. You can see the Index and the Line nr in All Line view are the same.
5. Similarly the toggling of the checkbox between My Selection view and its 1-line view works OK. Here the Index and the Line nr will be different and this is how it should be since Line nr is counted from the top of the grid display.

6. But when you click on button My Categories the toggling of the checkbox does not work properly. In this display the Index and the Line nr are the same and I think it's wrong.

I figured out the faulty pattern of the checkbox selections in My Categories. Follow these steps assuming that you selected these lines: 4, 6, 8, 12, 13, 14, 17

1.in My Categories view click on line nr 13 (8th from top) to view it in 1-line view - it will show Index nr 8 (wrong)
2. still in 1-line view select the checkbox Toggle Selection
3. go back to My Categories and line nr 8 (4th from the top) will have its checkbox selected! To get this checkbox unselected click on line nr 13 again and unselect the checkbox there.

So it's clear that the index numbers and line numbers are messed up in My Categories view, but NOT in All Lines and My Selection views, which is very strange since the code in the Row Behavior of the data grid and the code in the Line field of Row Template is the same for all views.

Where is the fault in the code?
I'll appreciate any help.

keram
Attachments
DG-categories+selections.zip
(29.12 KiB) Downloaded 215 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: check box selection in data grid is wrong

Post by Zryip TheSlug » Fri Mar 07, 2014 11:31 pm

Hi Keram,
So it's clear that the index numbers and line numbers are messed up in My Categories view, but NOT in All Lines and My Selection views, which is very strange since the code in the Row Behavior of the data grid and the code in the Line field of Row Template is the same for all views.
The same? Not exactly:


In the My Selection button the code is:

Code: Select all

repeat for each line i in gAllLines
      add 1 to Counter
      if item 1 of i is among the items of gMySelectedLineNrs then 
         put item 1 of i into DataArray[Counter]["Num"]  --line 1 number of dgform
      end if
end repeat
As you are looping through the entire content of your data field, the tCounter keys collected are synchronized with the datagrid indexes.

Example if gMySelectedLineNrs is containing 6 and 51

dgData[6]["num"] -> 6
dgData[6]["text"] -> line 6 of data
dgData[51]["num"] -> 51
dgData[51]["text"] -> line 51 of data


In the My Categories button the code is:

Code: Select all

 repeat for each line i in LinesInMyCategories
      add 1 to Counter
      put item 1 of i into DataArray[Counter]["Num"]  --line 1 number of dgform
 end repeat
In this case, as you are looping to a partial selection of the data field, the counter keys are not synchronized with your datagrid indexes.


Example if gMySelectedLineNrs is LinesInMyCategories 6 and 51:

dgData[1]["num"] -> 6
dgData[1]["text"] -> line 6 of data
dgData[2]["num"] -> 51
dgData[2]["text"] -> line 51 of data


For solving this, synchronize your data with the datagrid indexes by passing the line number of your data field:

Code: Select all

repeat for each line i in LinesInMyCategories
      put item 1 of i into Counter
      put item 1 of i into DataArray[Counter]["Num"]  --line 1 number of dgform
 end repeat

Example if gMySelectedLineNrs is LinesInMyCategories 6 and 51:

dgData[6]["num"] -> 6
dgData[6]["text"] -> line 6 of data
dgData[51]["num"] -> 51
dgData[51]["text"] -> line 51 of data


Best Regards,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: check box selection in data grid is wrong

Post by keram » Sat Mar 08, 2014 11:51 am

Hi Zryip,

Thanks a lot! It works now :)
The way you explain the reasoning behind and show the solution is very clear and obvious. But I would not be able to find it myself - it's great that you are around!

I added now another button for searching the text. Initially the same problem came up as above but with your solution -
put item 4 of i into Counter instead of add 1 to Counter, I was able to correct it right away.

Now the only thing that does NOT work with the Search button is again Toggle Selection in the 1-line view and the Search result view.

Try this:
click on All Lines button
then Search button
then click on one of the lines in the Search view
select Toggle Selection in 1-line view
go Back
then the hilite state of the checkbox in the Search view does not correspond to the hilite state in the 1-line view.
This time is a "one-way fault" that the selection made in the 1-line view is not properly set into Search view.

Any idea where is the fault?
I'm attaching the stack with your corrections above and the additional Search button.

keram
Attachments
DG-categories+selections+search.zip
(25.45 KiB) Downloaded 210 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: check box selection in data grid is wrong

Post by Zryip TheSlug » Sat Mar 08, 2014 11:23 pm

Hi Keram,
Where is the fault in the code?
As far I have understood your code, going back after a search is the only case you not refreshing the data in the datagrid.

Remark about the toggle selection:
In my opinion refreshing all the datagrid for changing the state of only one check box is not totally "cost effective", especially if you have the plan for a datagrid with more than 100 rows.

For changing the value of only one checkbox inside the datagrid, we can use the "SetDataOfIndex" command from the datagrid API's:
http://lessons.runrev.com/s/lessons/m/d ... a-grid-api

This command requires 3 parameters:
- the index of the row to refresh: gCurentIndex
- the name of our check box key inside the datagrid: BtnCheck
- the state of the toggle checkbox: true or false depending if the check box is hilited or not

Code: Select all

dispatch "SetDataOfIndex" to grp "datagrid 1" of cd "main" with gCurentIndex, "BtnCheck", true 
And in the context of the "Toggle selection" button:

Code: Select all

on mouseUp
   set the itemDel to tab
   
   if the hilite of me is true then
      put "true" into item 2 of line gCurentIndex of gMySelection
      put "true" into item 2 of line gCurentIndex of gAllLines
   else 
      put "false" into item 2 of line gCurentIndex of gMySelection
      put "false" into item 2 of line gCurentIndex of gAllLines
   end if
   
   dispatch "SetDataOfIndex" to grp "datagrid 1" of cd "main" with gCurentIndex, "BtnCheck", the hilite of me 
   
   RefreshAllLines
end mouseUp

Best Regards,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: check box selection in data grid is wrong

Post by keram » Sun Mar 09, 2014 1:38 am

Hi Zryip,
Zryip TheSlug wrote:As far I have understood your code, going back after a search is the only case you not refreshing the data in the datagrid.
Yes, that's correct and the reason why I did not put refreshing after the search was because I did not want to get the ask dialog again or to loose the search result.

Your solution works much better.

Thanks again! :)

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: check box selection in data grid is wrong

Post by keram » Mon Mar 17, 2014 3:59 am

Hi Zryip,

I have now added navigation buttons to the 1-line view - that was easy.

But now the syncing of the checkbox toggling between the 1-line view AFTER having pressed any of these navigation buttons does not work. What is selected is the line who's Index is shown in the 1-line view.

Since the checkbox button has this code:
dispatch "SetDataOfIndex" to grp "datagrid 1" of cd "main" with gCurentIndex, "BtnCheck", the hilite of me
I assume that I have to get the gCurentIndex of the actual line that is visible in 1-line view.

How can I get that gCurentIndex and get this working properly?
I'm attaching the modified stack.

Thanks for any help.

keram
Attachments
DG-next-prev etc.zip
(60.79 KiB) Downloaded 227 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: check box selection in data grid is wrong

Post by keram » Tue Mar 18, 2014 4:13 am

Looks like I fixed it myself.
The solution was to use the number from the "Num" field instead of the Index so I replaced gCurentIndex with:
item 4 of line gCurrentLine of gCurrentView
I'll keep on testing it few more times just to see if it continues to behave properly.

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

Post Reply