How to replace/Update a group in a substack?

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
3d-swiss
Posts: 39
Joined: Thu Nov 06, 2008 9:11 pm

How to replace/Update a group in a substack?

Post by 3d-swiss » Fri Apr 13, 2012 4:54 pm

Hello again.

In http://forums.runrev.com/phpBB2/viewtop ... =7&t=11635 I learned how to copy a group to a substack.
Now I have a update problem. When I chance an object or add a new object to the group in the mainstack, the cards in the substack will not be updated.

I tried to use a button to do an update:

Code: Select all

on mouseUp
   -- for every card in the substack 'daten' ...
   set the defaultStack to "Daten" 
   put number of cards into tAnzahl
   go to first card
   repeat with i = 1 to tAnzahl
      get the long ID of this card
      put it into tNewCard
      -- make an update of the group:
      set the defaultStack to "Test_DB_1" 
      copy group "bkgMasterCard" of card "VorlageDaten" to tNewCard # (1) Here is the problem!
      
      set the defaultStack to "Daten" 
      go to next card
   end repeat
end mouseUp
(1) Here is the problem:
copy group makes a second group to the card. I can delete the old group, but than I will lose all entries in the fields.

Is there a command for an update of group?
If not: I have to write a script, that will copy all entries of fields from the old group to the new group, before I delete the old group.

Thanks for reading and have a nice weekend.
Jens

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

Re: How to replace/Update a group in a substack?

Post by dunbarx » Fri Apr 13, 2012 5:54 pm

Interesting,

If you copy a group of fields (or even just a field) that contain text to the same card, all text is retained. But if you copy to another card or to another stack, that information is lost.

In HC, you could hold down the shift key, and the data would travel with the field.

You may have to save the data and reload at the destination. Not too bad, really. You can put such a tool into a library and reuse it at will.

An anomaly in the dictionary, under the "Copy" command: "If a stack is specified, the copied object must be a card."

It is certainly possible to copy a control to another stack. Is this just an old definition?

Craig Newman

3d-swiss
Posts: 39
Joined: Thu Nov 06, 2008 9:11 pm

Re: How to replace/Update a group in a substack?

Post by 3d-swiss » Fri Apr 13, 2012 10:53 pm

I have created a solution only for fields. I hope you will like it. The update will only run, when there are no fields with the same name.

Code: Select all

on mouseUp
   local tArray
   lock screen
   
   ## Update one background to all cards of a substack 
   
   -- Check the name of all fields in background in the mainstack:
   set the defaultStack to "Daten" 
   go invisible to card "VorlageDaten" # Name of the card with the background
   put the number of fields of group "bkgMasterCard" into tFields
   repeat with j = 1 to tFields
      put name of field j of  group "bkgMasterCard" into tIndex
      add 1 to tArray[tIndex]
   end repeat
   put empty into tWarnung
   repeat for each line tIndex in the keys of tArray
      if (tArray[tIndex] > 1) then
         put "Field: " & tIndex & " was found " & tArray[tIndex] & " times." & return after tWarnung
         put "Update stopped!" after tWarnung
      end if
   end repeat
   if (tWarnung is not empty) then 
      beep
      put tWarnung into message box
      delete local tArray
      unlock screen
      pass mouseUp ## Stop Script here!
   end if
   delete local tArray ## distroy this array
   
   -- For each card of the substack ...
   set the defaultStack to "Daten" 
   put number of cards into tAnzahl
   go to first card
   repeat with i = 1 to tAnzahl
      set the defaultStack to "Daten" 
      get the long ID of this card # (Use 'it' for the current card)
      put it into tNewCard # (Save ID of current card)
      -- ... use background from mainstack:
      --- (1) for each field copy the data into an array
      if there is a group "bkgMasterCard" then -- {if#1}
         put the number of fields of group "bkgMasterCard" into tFields
         repeat with j = 1 to tFields
            put name of field j of  group "bkgMasterCard" into tIndex
            put field j of group "bkgMasterCard" into tArray[tIndex]
         end repeat
         --- (2) delete old background
         delete group "bkgMasterCard"
      end if -- {if#1}
      --- (3) delete background from mainstack to current card 
      set the defaultStack to "Test_DB_1" 
      copy group "bkgMasterCard" of card "VorlageDaten" to tNewCard # (instead of 'place')
      --- (4) for each field get the data from the array
      set the defaultStack to "Daten" 
      repeat for each line tIndex in the keys of tArray
         if there is a field tIndex then -- Check, if there is still that field
            put tArray[tIndex] into field tIndex
         end if
      end repeat
      --- (5) go to next card of substack
      set the defaultStack to "Daten" 
      go to next card
   end repeat
   
   -- delete array & unlock screen
   delete local tArray
   unlock screen
   
end mouseUp
Perhabs, there is still a better way to do this, isn't it?

Jens

Post Reply