Après windows pour les nuls, voici PB pour les bons (ou presque).

Le forum (ô combien francophone) des utilisateurs de Powerbuilder.

Recherche rapide

Annonce

Certaines rubriques, dont des cours, sont uniquement visibles par les membres du forum ^^.
Dans la rubrique Liens & Références, vous avez accès à un sommaire de téléchargement, profitez-en !
Il existe maintenant un nouveau TOPIC "Votre CV en Ligne" accessible uniquement par demande.

#1 18-09-2008 13:33:05

Nyphel  
Membre Power Geek
Lieu: Grenoble
Date d'inscription: 06-05-2008
Messages: 253
Pépites: 12
Banque: 529,705,333,097,693

[SOURCE] Repositionner les colonnes d'une datawindow - Combler les trous

Bonjour,

J'utilise une datawindow avec 20 colonnes. Suivant l'action de l'utilisateur, je masque certaines colonnes, ce qui me créée des "trous" vides au milieu de ma datawindow. J'ai donc écrit ce petit script qui - tout en conservant l'ordre actuel des colonnes - se charge de les repositionner les unes à côté des autres pour combler les trous.

Je me suis dit que ça pourrait servir à d'autres, alors le voili le voilà :

Code: pb

// This function should be called to adjust columns positions.
// When columns are hidden, big white spaces may appear between 2 visible columns.
// The goal of this function is to avoid such white spaces.


long ll_free_space = 15   // White space inserted between each column

long ll_nb_cols
long ll_i, ll_k

string ls_colonnes // Columns as a string
string ls_col[] // Columns as a table of strings (1 cell for each datawindow object)

string ls_old_col[]
long ll_old_x[]
long ll_old_width[]
long ll_old_tables_index = 0

// Generate the table of columns (All objects of the datawindow, including computed fields)
uo_base_string luo_string
ls_colonnes = dw_suspect_tr.Object.DataWindow.Objects
luo_string.of_parsetoarray(ls_colonnes, '~t', ls_col)

// Give a look at each column of the datawindow
FOR ll_i = 1 TO UpperBound(ls_col)
  
  // If the object is not an header, it may be a column or a computed field (nothing else has been defined in this DW)
  IF right(ls_col[ll_i],2) <> '_t' THEN
    
    // If the column is visible
    IF long(dw_suspect_tr.describe(ls_col[ll_i] + ".visible")) = 1 THEN
      
      // Get name and position attributes of the column
      ll_old_tables_index ++
      ls_old_col[ll_old_tables_index]    = string(dw_suspect_tr.describe(ls_col[ll_i] + ".name"))
      ll_old_x[ll_old_tables_index]       = long(dw_suspect_tr.describe(ls_col[ll_i] + ".x"))
      ll_old_width[ll_old_tables_index]   = long(dw_suspect_tr.describe(ls_col[ll_i] + ".width"))
      
    END IF
  END IF
NEXT      

// ls_old_col   now contains all the visibles columns names
// ll_old_x     now contains all the visibles columns x coordinates
// ll_old_width   now contains all the visibles columns widths

/*
// Display the current old tables state : name - x - width
string ls_temp
FOR ll_i = 1 TO UpperBound(ll_old_x)
  ls_temp += string(ls_old_col[ll_i]) + '   -   '
  ls_temp += string(ll_old_x[ll_i]) + '   -   '
  ls_temp += string(ll_old_width[ll_i]) + char(10)
NEXT
messagebox('', ls_temp)
*/

// The describe function does not give the objects in original order : they are not really sorted
// We want to manage columns in the order visible by the user
// So, we need to sort old tables in new tables, using x coordinate as reference
string ls_new_col[]
long ll_new_x[]
long ll_new_width[]
long ll_min_x_index
long ll_min_x

// This sort is not optimised (NxN) but the number of columns is small (less that 20)
// and an easier understanding code should be prefered to a better algorithm for maintenance
// A better algorithm could be : http://www.dailly.info/algorithmes-de-tri/casier.php
FOR ll_i = 1 TO UpperBound(ll_old_x)
  ll_min_x = 9999999999
  
  // Get the current minimal x value in the table
  FOR ll_k = 1 TO UpperBound(ll_old_x)
    IF ll_old_x[ll_k] >= 0 THEN
      IF ll_old_x[ll_k] < ll_min_x THEN
        ll_min_x_index = ll_k
        ll_min_x = ll_old_x[ll_k]
      END IF
    END IF
  NEXT

  // The minimal value is added at the end of the new tables
  ls_new_col[ll_i]     = ls_old_col[ll_min_x_index]
  ll_new_x[ll_i]     = ll_old_x[ll_min_x_index]
  ll_new_width[ll_i]   = ll_old_width[ll_min_x_index]
  
  ll_old_x[ll_min_x_index] = -1   // Do not consider this value in next research
NEXT

// ls_new_col     now contains all the visibles columns names sorted by x position
// ll_new_x       now contains all the visibles columns x coordinates sorted by x position
// ll_new_width   now contains all the visibles columns widths sorted by x position

/*
/ Display the current new tables state : name - x - width
ls_temp = ''
FOR ll_i = 1 TO UpperBound(ll_new_x)
  ls_temp += string(ls_new_col[ll_i]) + '   -   '
  ls_temp += string(ll_new_x[ll_i]) + '   -   '
  ls_temp += string(ll_new_width[ll_i]) + char(10)
NEXT
messagebox('', ls_temp)
*/

// Now we have the table representing the visible columns and sorted on x position
// In other words, we have a table representing exactly what user can see
// Like many columns may have been hidden in post_open() event, just move columns - keeping order - to avoid thoses white spaces
FOR ll_i = 1 TO UpperBound(ll_new_x)
  IF ll_i = 1 THEN
    // If first column visible, set it the most on the left possible
    dw_suspect_tr.modify(ls_new_col[ll_i] + ".x = " + string(ll_free_space))
    dw_suspect_tr.modify(ls_new_col[ll_i] + "_t" + ".x = " + string(ll_free_space))
    // Store the new x position, that will be used by next column
    ll_new_x[ll_i] = ll_free_space
  ELSE
    // Move the column, using the previous visible column x position and with
    dw_suspect_tr.modify(ls_new_col[ll_i] + ".x = " + string(ll_new_x[ll_i - 1] + ll_new_width[ll_i - 1] + ll_free_space))
    dw_suspect_tr.modify(ls_new_col[ll_i] + "_t" + ".x = " + string(ll_new_x[ll_i - 1] + ll_new_width[ll_i - 1] + ll_free_space))
    // Store the new x position, that will be used by next column
    ll_new_x[ll_i] = ll_new_x[ll_i - 1] + ll_new_width[ll_i - 1] + ll_free_space
  END IF
NEXT


Pour qu'il fonctionne il faut que :
- les en-têtes de colonnes portent le nom de la colonne + _t (Exemple : account_number et account_number_t)
- que la largeur des en-têtes de colonnes soient égaux aux largeurs des colonnes

Le [luo_string.of_parsetoarray()] provient des PFC et permet de splitter une chaine de caractères dans un tableau de chaines de caractères.

Dernière modification par Nyphel (18-09-2008 13:39:55)

Hors ligne

 

#2 18-09-2008 13:41:52

JCZ  
Builder Power
Award: bf
Lieu: 75019 paris
Date d'inscription: 21-05-2007
Messages: 1724
Pépites: 496,453,703,213
Banque: 9,223,372,036,854,776,000

Re: [SOURCE] Repositionner les colonnes d'une datawindow - Combler les trous

000


Face à l'agression, la puissance de l'intelligence

Hors ligne

 

#3 18-09-2008 14:33:33

nico  
Modérateur
Award: bf
Lieu: Plélan le grand
Date d'inscription: 08-02-2007
Messages: 273
Pépites: 13
Banque: 9,223,372,036,854,776,000

Re: [SOURCE] Repositionner les colonnes d'une datawindow - Combler les trous

Hors ligne

 

#4 18-09-2008 15:19:20

erasorz  
Admin
Lieu: Babylone
Date d'inscription: 23-11-2006
Messages: 5121
Pépites: 97,197
Banque: 2,147,483,647

Re: [SOURCE] Repositionner les colonnes d'une datawindow - Combler les trous



précision : pour les DW tabular (pour les grids c'est automatique )


N'envoyez jamais un humain faire le travail d'un programme.

Hors ligne

 

Pied de page des forums

Propulsé par FluxBB 1.2.22