Move line up/down in datagrid

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Move line up/down in datagrid

Post by tusa » Wed Feb 08, 2017 11:35 am

Hello

Is it possible to move a line in a datagrid up/down by pressing a button?
Udklip.PNG
Can somebody tell me how?

Brg Tue

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10350
Joined: Wed May 06, 2009 2:28 pm

Re: Move line up/down in datagrid

Post by dunbarx » Wed Feb 08, 2017 2:48 pm

Hi.

Use the "dgHiliteLine" property. Check it out in the DataGrid use manual.

Craig Newman

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Wed Feb 08, 2017 2:55 pm

Craig being gentle and taking on the role of coach lol.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10350
Joined: Wed May 06, 2009 2:28 pm

Re: Move line up/down in datagrid

Post by dunbarx » Wed Feb 08, 2017 6:39 pm

Mike.

Yes and no. I never take anything for granted with DG's. They scare me.

Craig

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Wed Feb 08, 2017 8:11 pm

WHAT? omg lol

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: Move line up/down in datagrid

Post by tusa » Thu Feb 09, 2017 10:32 pm

dunbarx wrote:Hi.

Use the "dgHiliteLine" property. Check it out in the DataGrid use manual.

Craig Newman
I've tried that, then the highlighted line moves, but not the data.
Do you have an example?

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Thu Feb 09, 2017 10:42 pm

What he means is after you figure out which line is hilited you can use the other properties of the data grid to rearrange it.

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Move line up/down in datagrid

Post by SparkOut » Thu Feb 09, 2017 11:16 pm

You mean to change the position of a row of data, rather than the position of the selection. Hmm. The datagrid will have multiple sort options by column. What's to stop a user resorting the grid by a different column value and spoiling your new view? (Ok there are things that can be done to fix this, but what is it that you are trying to achieve?)

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Thu Feb 09, 2017 11:26 pm

It doesn't matter. He wants to use the arrow buttons that he created to move a selected line up or down in the list. The answer is yes you can, but we're trying to help him to learn how to figure it out rather than just writing the code for him.
Last edited by Mikey on Fri Feb 10, 2017 3:15 am, edited 1 time in total.

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: Move line up/down in datagrid

Post by tusa » Fri Feb 10, 2017 3:07 am

SparkOut wrote:You mean to change the position of a row of data, rather than the position of the selection. Hmm. The datagrid will have multiple sort options by column. What's to stop a user resorting the grid by a different column value and spoiling your new view? (Ok there are things that can be done to fix this, but what is it that you are trying to achieve?)
I want to achieve that I can move the line from one position in the datagrid to another.
It's a running order.
It's only an example, and only in this example, it's sorted by number, but that coul'd be different order, so I can't use any column to order.

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: Move line up/down in datagrid

Post by tusa » Fri Feb 10, 2017 3:15 am

Right now I can find the line wich is highlighted...

Code: Select all

on mouseUp
   put the dgHilitedLines of group "seqGrid" into theLine
end mouseUp
How do I get further, wich command can I use to move it up or down?

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Fri Feb 10, 2017 3:26 am

Have you read anything about dgtext?

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Fri Feb 10, 2017 3:33 am

There is a whole section in the manual on the API for the datagrid. There are at least three different ways, using commands and functions described in that chapter, to do this.

tusa
Posts: 36
Joined: Sun Jan 31, 2016 10:30 pm

Re: Move line up/down in datagrid

Post by tusa » Sun Feb 12, 2017 4:23 am

SOLVED :wink:

I've finally solved this by reading som different links in the Data Grid API

I've selected the line I want to move
Udklip 1.PNG
Press the up arrow

Code: Select all

on mouseUp
   ## find the line no. of the selcted line, put it in af variable called theLine
   put the dgHilitedLines of group "seqGrid" into theLine
   put the dgDataOfLine[theLine] of group "seqGrid" into theSelectedRowData
   ## theDataA is now an array variable.
   put the dgDataOfLine[theLine-1] of group "seqGrid" into theSelectedRowAboveData
   ## theDataC is now an array variable with data from row above.
   
   ## Create a nested array.
   ## dictate values for individual columns
   put theSelectedRowData["Nr."] into theRowAboveData["Nr."]
   put theSelectedRowData["Data felt 1"] into theRowAboveData["Data felt 1"]
   put theSelectedRowData["Data felt 2"] into theRowAboveData["Data felt 2"]
   set the dgDataOfIndex[ the dgHilitedIndex of group "seqGrid" - 1 ] of group "seqGrid" to theRowAboveData
   
   ## Create a nested array.
   ## dictate values for individual columns
   put theSelectedRowAboveData["Nr."] into theSelectedRowNewData["Nr."]
   put theSelectedRowAboveData["Data felt 1"] into theSelectedRowNewData["Data felt 1"]
   put theSelectedRowAboveData["Data felt 2"] into theSelectedRowNewData["Data felt 2"]
   set the dgDataOfIndex[ the dgHilitedIndex of group "seqGrid" ] of group "seqGrid" to theSelectedRowNewData
   
   ## Select the line above.
   set the dgHilitedLines of group "seqGrid" to theLine-1
end mouseUp
The selected line and the line above change place
Udklip 3.PNG
Udklip 3.PNG (3.38 KiB) Viewed 8534 times
Now pressing the down button

Code: Select all

on mouseUp
   ## find the line no. of the selcted line, put it in af variable called theLine
   put the dgHilitedLines of group "seqGrid" into theLine
   put the dgDataOfLine[theLine] of group "seqGrid" into theSelectedRowData
   ## theDataA is now an array variable.
   put the dgDataOfLine[theLine+1] of group "seqGrid" into theSelectedRowUnderData
   ## theDataC is now an array variable with data from row above.
   
   ## Create a nested array.
   ## dictate values for individual columns
   put theSelectedRowData["Nr."] into theRowUnderData["Nr."]
   put theSelectedRowData["Data felt 1"] into theRowUnderData["Data felt 1"]
   put theSelectedRowData["Data felt 2"] into theRowUnderData["Data felt 2"]
   set the dgDataOfIndex[ the dgHilitedIndex of group "seqGrid" + 1 ] of group "seqGrid" to theRowUnderData
   
   ## Create a nested array.
   ## dictate values for individual columns
   put theSelectedRowUnderData["Nr."] into theSelectedRowNewData["Nr."]
   put theSelectedRowUnderData["Data felt 1"] into theSelectedRowNewData["Data felt 1"]
   put theSelectedRowUnderData["Data felt 2"] into theSelectedRowNewData["Data felt 2"]
   set the dgDataOfIndex[ the dgHilitedIndex of group "seqGrid" ] of group "seqGrid" to theSelectedRowNewData
   
   ## Select the line under.
   set the dgHilitedLines of group "seqGrid" to theLine+1
end mouseUp
The selected line and the line under change place
Udklip 5.PNG
Udklip 5.PNG (3.53 KiB) Viewed 8534 times

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Move line up/down in datagrid

Post by Mikey » Sun Feb 12, 2017 5:27 am

Good job figuring it out! Now, a couple of things:
1) Don't mix index code and line code. They are not quite the same thing, and I would hate for you to have some bizarre bug come up later because you mixed them.
2) Click on the bottom row and hit the down arrow. That is called an "edge case". You have to either disable the down button when the user clicks on the bottom row, OR exit the handler for the down button if the bottom row is the row that is selected (or I suppose you could throw up a message to the user, but that isn't very nice). You need to do the same thing for the top row with the up button.
3) Similarly, if the user doesn't select a row, they can still hit the up and down buttons. That edge case also needs to be handled. You can start with the buttons disabled, and only enable them if the user clicks on a row in the DG, or you can just have them not do anything if no row is selected.
4) Your code is more complicated than it needs to be because you are creating an array and then assigning it, when you already have arrays for both rows. Pull the arrays for both rows, then put them into their new positions in the DG array. No new array creation needed.
5) Extra credit: How else could you achieve this, using commands from the API? One of the nifty things about LC is that there are so many ways to write and do the same thing that it is easy to develop your own intuitive style.
6) Extra credit: Did you know that there are other controls, both from LC and from third parties, that you can use to build this sort of list? After you get this working, you should investigate, because depending on what you are trying to do in the end, maybe the DG isn't the ideal way to do it (and maybe it is).

Post Reply