Le forum (ô combien francophone) des utilisateurs de Powerbuilder.
Pages: 1
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à :
// 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
000
Hors ligne
Hors ligne
précision : pour les DW tabular (pour les grids c'est automatique )
Hors ligne
Pages: 1