Skip to content

Instantly share code, notes, and snippets.

@kihashi
Last active December 10, 2015 15:28
Show Gist options
  • Save kihashi/4454099 to your computer and use it in GitHub Desktop.
Save kihashi/4454099 to your computer and use it in GitHub Desktop.
A Progress 8 procedure that can be used to swap the position of 2 elements in a browse.
/*------------------------------------------------------------------------------
Purpose:
Parameters:
swap_direction int This parameter indicates whether the selected
element in the browse should be moved up or down.
Passing 1 moves the selected element down in the
browse. Passing -1 moves it up in the browse.
Other input values cause the procedure to return.
brw browse The browse that contains the elements to be swapped
Notes:
This procedure assumes that the database table with which the browse is
associated has a field called `cntr_seq` that represents the cntr_sequence that the
elements in the browse are sorted by. For temp tables, it might be prudent
to have the primary index of the table involve `cntr_seq`.
This procedure also assumes that the `cntr_seq` number used is 1 based.
0 is used as a swap space in this implementation.
------------------------------------------------------------------------------*/
&Scoped-define table tt_cntr /* Change this to the name of your table */
&Scoped-define brw_refresh {&OPEN-QUERY-brw-cntr} /* Change this to the name of your
browse. */
def input parameter swap_direction as int no-undo.
def input parameter brw as widget-handle no-undo.
/* Record the cntr_seq number of the item currently in case a roll-back is needed. */
def var current_cntr_seq as int no-undo.
def var new_cntr_seq as int no-undo. /* The updates cntr_seq number */
def var this_row as int no-undo. /* The number of the row that is selected */
/* Remember the row that the browse has highlighted. */
this_row = brw:focused-row.
/* This line is needed to keep the same highlighting after a browse refresh. */
brw:set-repositioned-row(this_row,"conditional":U).
current_cntr_seq = {&table}.cntr_seq.
/* Check the input for errors. */
if swap_direction ne 1 and swap_direction ne -1 then
return.
if current_cntr_seq = 1 and swap_direction = -1 then
return.
{&table}.cntr_seq = 0.
/* Find the record before or after the highlighted one. */
if swap_direction = 1 then
find first {&table}
where {&table}.cntr_seq gt current_cntr_seq
no-lock no-error.
else
find last {&table}
where {&table}.cntr_seq lt current_cntr_seq
no-lock no-error.
/* Swap the records using cntr_seq #0 as a placeholder. */
if available {&table} then
do:
new_cntr_seq = {&table}.cntr_seq.
{&table}.cntr_seq = current_cntr_seq.
find first {&table}
where {&table}.cntr_seq = 0.
{&table}.cntr_seq = new_cntr_seq.
end.
/* There is no element before or after the highlighted one,
so undo our changes.
*/
else do:
find first {&table}
where {&table}.cntr_seq = 0.
{&table}.cntr_seq = current_cntr_seq.
end.
/* Refresh our browse view and move the highlight to the new position. */
{&brw_refresh}
reposition brw to rowid cell_rowid
no-error.
END PROCEDURE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment