How to Reindex a datagrid

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
gurusonwheels
Posts: 31
Joined: Sat Apr 01, 2017 5:15 pm

How to Reindex a datagrid

Post by gurusonwheels » Wed May 24, 2017 5:13 pm

i have a datagrid that i have entered data . then i used a script to sort the data like i want .

however even after sorting the data when i use this code
the data does not get saved in my new sorted order .
i assume this is becasue the index for each item is what it goes by when saving

Code: Select all

/////////  SAVE DATA FUNCTION ////////////////////////
    command save_data
       local tFile
       ## Save content of datagrid to file:
put the dgtext of grp  "DataGrid" into tVariable

## Create allowed pathname:
put specialFolderPath("Documents") & "/saved_data.text" into tFile
put tVariable into url("file:" & tFile)

## the result is EMPTY on success!
if the result <> EMPTY then
  answer "Error:" && the result
end if


    end save_data
    /////////////////////////////////////////////////////////////

how do i reindex my newly sorted data so it can be exported the same .

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

Re: How to Reindex a datagrid

Post by dunbarx » Wed May 24, 2017 5:58 pm

Hi.

I do not see the sorting.

When you extract the dgText, you get the entire contents of the DG in an ordinary (not an array) variable, tab delimited field/return delimited record. You can sort this directly using all of the tools within LC. You can then restore the DG or send that information wherever.

What are you seeing?

Craig Newman

gurusonwheels
Posts: 31
Joined: Sat Apr 01, 2017 5:15 pm

Re: How to Reindex a datagrid

Post by gurusonwheels » Wed May 24, 2017 6:17 pm

i did not post the sorting a its not relevant to my question as it works fine .

basicly if i click one of the colloums header and sort by that column...

that code that i posted is how im saving the data from the datagrid .

works great .
but i want to save in the order of what im seeing on the screen
not by the original datagrid index.


how ?

gurusonwheels
Posts: 31
Joined: Sat Apr 01, 2017 5:15 pm

Re: How to Reindex a datagrid

Post by gurusonwheels » Wed May 24, 2017 6:23 pm

this is my sorting that i do if it help you understand ..

works fine but slow .

Code: Select all

on mouseUp
   local tFileName, tFileContents,tColumnTitles,tData
   
   show group sorting
   
   put the dgtext of grp "DataGrid" into tList
   ## tList can of course also be any text file or other data!
   
   // set the dgtext of grp "DataGridhidden" to tList
   
   ## We set the itemdelimiter to TAB
   set itemdel to TAB
   
   ## Now we go through any line and check if item 3 or 4 = empty
   repeat for each line tLine in tList
      
      ## Both BOX and LOOSE are empty
      if item 3 of tLine = EMPTY AND item 4 of tLine = EMPTY then
         next repeat
      end if
      
      ## One of them HAS in fact content, so we go ahead in the loop
      ## and create a new list, since "repeat for each..." is fast but also READ only
      ## means we cannot modify the cntent of tLine
      put tLine & CR after tNewList
   end repeat
   
   ## Delete trailing CR
   delete char -1 of tNewList
   set the dgtext of grp "DataGrid" to tNewList
   
   
   ## We set the itemdelimiter to TAB
   set itemdel to TAB
   
   ## Now we go through any line and check if item 3 or 4 = empty
   repeat for each line tLine in tList
      
      ## Both BOX and LOOSE are empty
      if item 3 of tLine > EMPTY OR item 4 of tLine >EMPTY then
         next repeat
      end if
      
      ## One of them HAS in fact content, so we go ahead in the loop
      ## and create a new list, since "repeat for each..." is fast but also READ only
      ## means we cannot modify the cntent of tLine
     dispatch "AddLine" to group "DataGrid" with tLine
   end repeat
   
   save_data
   
   hide group sorting
   
   
   
end mouseUp

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

Re: How to Reindex a datagrid

Post by dunbarx » Wed May 24, 2017 6:45 pm

Have you tried to "dispatch reFreshList" to the DG group?

Craig

gurusonwheels
Posts: 31
Joined: Sat Apr 01, 2017 5:15 pm

Re: How to Reindex a datagrid

Post by gurusonwheels » Wed May 24, 2017 7:25 pm

thats seems to work , well kinda .

now when i export the data it is indeed saved in the order of the sorted data...
Hurray ! :D

however if i switch cards and come back the data order reverts to the original order .
i have this in my on opencard however it still loads in original order for some reason even after verifying that the file is saved correctly

Code: Select all

on opencard
   /////////   start open file ////////////////////////
   local tFile
   ## Read file on disk and set the content of your datagrid to the content of that file
   ## First create correct pathname:
   put specialFolderPath("Documents") & "/saved_data.text" into tFile
   ## We are the programmers, so we need to check everything :-)
   ## No file, no need to progress!
   if there is NOT a file tFIle then
      // command to save datagrid data since it is not found //
      save_data
      //////////////////////////////////////////////////////////////
      exit to top
   end if
   put url("file:" & tFile) into tVariable
   set the dgtext of grp "DataGrid" to tVariable
   ////////////  end open file ///////////////////////
end opencard
im switching cards using
go to card "cardnamehere"


thanks for your help

gurusonwheels
Posts: 31
Joined: Sat Apr 01, 2017 5:15 pm

Re: How to Reindex a datagrid

Post by gurusonwheels » Wed May 24, 2017 7:36 pm

figured that out .
i have one of the columns sorting by that name ,
unchecked and all is well ,

thank you

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to Reindex a datagrid

Post by jacque » Thu May 25, 2017 11:11 pm

I only read through your sorting handler without actually running it, but it looks to me like the second repeat loop is unnecessary. The first one removes all entries where the third and fourth items are empty.

What is left are entries that have data in either or both the third and fourth columns, so I'm not sure why you'd need to check for that again and then add them back into the datagrid. There shouldn't be any lines that meet that criteria. But I'm not sure what your data contains.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”