Autor Beitrag
Morduk666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 07.05.03 05:23 
hoi,

hab hier das forum schon abgesucht aber auch nix passendes gefunden. suche ne Lösung wie ich meine listview sortieren kann per klick auf den spaltenkopf. und zwar am besten auf/absteigend.

wenn da einer ne idee hat bin ich dankbar
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 07.05.03 06:53 
Hilfe schon gefunden? da ist sogar ein Beispiel drin. Für den Fall, dass deine Hilfe kaputt ist:
Zitat:
This example shows how to use the OnColumnClick and OnCompare events of a list view to let users sort the columns in a report-style list view by clicking on the column headers. This requires a global variable to keep track of the column that was clicked:

ausblenden Delphi-Quelltext
1:
var ColumnToSort: Integer;					


Zitat:
The OnColumnClick event handler sets the global variable to indicate the column to sort and calls AlphaSort:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);

begin
  ColumnToSort := Column.Index;
  (Sender as TCustomListView).AlphaSort;
end;


Zitat:
The OnCompare event handler causes the list view to sort on the selected column:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
var
  ix: Integer;
begin
  if ColumnToSort = 0 then
    Compare := CompareText(Item1.Caption,Item2.Caption)
  else begin
   ix := ColumnToSort - 1;
   Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
  end;

end;


Zitat:
Note: This OnCompare event handler uses the global CompareText function. An application may want to use AnsiCompareText, CompareStr, or AnsiCompareStr instead, depending on whether the comparison should be case-sensitive and whether the locale should be considered.

Aich wenn das aus der Hilfe für den VCL-Listview ist, sollte es genauso beim CLX-Listview gehen.

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 07.05.03 14:34 
hmm. danke ersma. der code funzt recht gut. leider kann man nur in eine richtung sortieren. geht das auch dass er bei nem klick auf/absteigend sortiert ? also sortierreichenfolge umdreht?

vor allem hab ich ein prob mit der sortierung bei zahlenwerten. er sortiert mir z.b. in der reihenfolge: 3, 4, 500, 6, 7, 83, ... statt 3, 4, 6, 7, 83, 500 :((


was issn da zu machen ?freue mich über jede antwort
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Mi 07.05.03 14:49 
Hallo

onlinehilfe hat folgendes geschrieben:
Die Ereignisbehandlungsroutine für OnCompare vergleicht zwei Listenelemente, die in den Parametern Item1 und Item2 übergeben werden. Weisen Sie dem Parameter Compare 0 zu, wenn Item1 und Item2 in der Sortierreihenfolge gleichrangig sind. Weisen Sie ihm einen Wert unter 0 zu, wenn Item1 kleiner als Item2 ist, und einen Wert über 0, wenn Item1 größer als Item2 ist.

nimm eine weitere Variable columnclicks. Prüfe bei oncolumclick, ob du die gleiche Spalte geklickt hast, wenn ja ändere Columnslicks, wenn nicht setzt die Variable wieder zurück.

bei oncompare prüfst du dann columsclicks, und sortierst entsprechend.
um eine andere sortierreihenfolge zu erhalten, negier den Wert compare

in etwa so (nich großartig getestet)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
var ix: Integer;
begin
  if ColumnToSort = 0 then
    Compare := not (CompareText(Item1.Caption,Item2.Caption))
  else begin
   ix := ColumnToSort - 1;
   Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
  end;
  if columnclicks mod 2 = 0 then compare:=not compare;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  if ColumnToSort<>column.Index then columnclicks:=1
                                else inc(columnclicks);
  ColumnToSort := Column.Index;
  (Sender as TCustomListView).AlphaSort;
end;

Zahlen sortieren: comparetext vergleichtalphabetisch, du mußt bei oncompare das ganze in Zahlen (Strtoint - Achtung wenn es keine zahlen sind, wegen der exception) wandeln und die vergleichen. Du kannst ja columntosort abfragen und entscheiden, ob diese Spalte text oder zahlen enthält.

Mfg Frank

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 07.05.03 15:17 
kewl kewl danke. nun funzts genau so wie ich wollte.
so hab ich nun zahlen sortiert:
ausblenden Delphi-Quelltext
1:
Compare := CompareValue(strtoint(Item1.SubItems[i]), strtoint(Item2.SubItems[i]));					

nur dreht er mir bei den zahlenspalten die sortierreihenfolge ned um. bei stringspalten funzt es einwandfrei. finde aber ned den fehler.

könnt mir da nochmal wer helfen ?

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Mi 07.05.03 15:23 
die sortierreihenfolge habe ich ganz unten bei oncompare umgedreht. (compare := not compare)
wenn du deine zeile alleine hast, dann nimm compare := - comparevalue...
un bau ein try / except mit ein, damit du keine exception erhälst, wenn das subitem keine zahl enthält.

edit: nimmst du jetzt die CLX oder duoch die VCL, weil mit CLX hat das hier irgendwie nix zu tun ... :roll:

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 07.05.03 15:56 
Zitat:
edit: nimmst du jetzt die CLX oder duoch die VCL, weil mit CLX hat das hier irgendwie nix zu tun


keine ahnung ob CLX oder VCL. weis nedma was das is :p

aber meine zahlenspalten dreht er immer noch ned um. obwohl ich den selben code für string und integer spalten hab :
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
  
if ColumnToSort = 9 then
  begin
    i := ColumnToSort -1;
    Compare := CompareValue(strtoint(Item1.SubItems[i]), strtoint(Item2.SubItems[i]));
  end;

  if ColumnToSort = 10 then
  begin
    i := ColumnToSort -1;
   Compare := CompareText(Item1.SubItems[i],Item2.SubItems[i])  ;
  end;


if columnclicks mod 2 = 0 then compare:=not compare;

also spalte 10 geht auf und absteigend. aber die 9er mitden zahlen drinn ned.

bin ich blind ?? :(

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Mi 07.05.03 17:44 
wenn du für Windows entwickelts ist es die VCL, für Linux die CLX.

denk dran, die spalten beginnen von 0 an zu zählen, nicht von 1.
d.h deine Spalte ganz links hat den "index" 0 und die 10. Saplte hat den index 9. setzt mal einen haltepunkt beim sortieren mit zahlen rein und gugg mal, ob der haltepunkt erreicht wird.

mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Mi 07.05.03 18:54 
also ich mach das auf windows plattform.

Zitat:
setzt mal einen haltepunkt beim sortieren mit zahlen rein und gugg mal, ob der haltepunkt erreicht wird


also er kommt dort vorbei im code. er sortiert auch. aber nur absteigend. nie annersrum. und das betrifft nur die spalten mit integer werten. die string spalten funzen 1A
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 08.05.03 09:27 
Morduk666 hat folgendes geschrieben:
also ich mach das auf windows plattform.

dann bist du in der falschen Forums-Sparte...

Zitat:
also er kommt dort vorbei im code. er sortiert auch. aber nur absteigend. nie annersrum. und das betrifft nur die spalten mit integer werten. die string spalten funzen 1A


also comparevalue scheint es unter d5 nicht zu geben.
habs so gemacht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
  if zahlenspalte dann:
     try
      compare:= strtoint(Item1.SubItems[ix])-strtoint(Item2.SubItems[ix])
     except
      //im fehlerfalle, wenn die subitems keine zahl enthält oder dann comparetext wieder nehmen
      compare:=0;
     end;

und er hat mir die Spalten auf/absteigend sortiert.

Mfg Frank

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Do 08.05.03 14:01 
ey kewl super. nun funzts. vielen dank nochma für die mühe.

Zitat:
dann bist du in der falschen Forums-Sparte...


.. naja is n fall für den board admin ;p
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: Sa 04.10.03 16:29 
Hallo,

wenn ich Progs wie KaZaA oder so angucke, dann ist oben im ColumnHeader immer ein Dreieck, dass nach oben (absteigend sortiert) oder unten (aufsteigend sortiert) zeigt. Muss ich diese Dreieck-Grafik selber coden oder stellt Delphi da eine Lösung bereit?

_________________
Life is a bad adventure, but the graphic is really good!
Morduk666 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20



BeitragVerfasst: Sa 04.10.03 17:09 
da gibts eine kompo. ElTree. die hat das und is free. damit geht listview und treeview. is ne feine kompo nutz ich immer ;)
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: Sa 04.10.03 17:35 
Vielen Dank, werd's mal austesten :D

_________________
Life is a bad adventure, but the graphic is really good!
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Sa 04.10.03 18:06 
Andreas Pfau hat folgendes geschrieben:
Hallo,

wenn ich Progs wie KaZaA oder so angucke, dann ist oben im ColumnHeader immer ein Dreieck, dass nach oben (absteigend sortiert) oder unten (aufsteigend sortiert) zeigt. Muss ich diese Dreieck-Grafik selber coden oder stellt Delphi da eine Lösung bereit?


smartimages im Columnheader verwenden oder den Header selberzeichnen.

Wenn du dir die Winapi-tuts von Luckie anguggst, ist da auch was bei den Demos (commonC/Listviwe) dabei.

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: Sa 04.10.03 21:25 
Hallo,

wow, das ist ja noch viel genialer... Danke! :D Wusste gar net, dass so eine Property existiert, aber das ist echt die ideale Lösung!

_________________
Life is a bad adventure, but the graphic is really good!
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: So 05.10.03 09:19 
Andreas Pfau hat folgendes geschrieben:
Hallo,

wow, das ist ja noch viel genialer... Danke! :D Wusste gar net, dass so eine Property existiert, aber das ist echt die ideale Lösung!

naja, ideal finde ich sie persönlich nicht, da die Smallimages ja immer links sind und das sortiersymbol ist damit nicht rechts, wie man es von anderen Progs gewohnt ist. Einfach(er) ist es aber in jedem Fall :wink:

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: So 05.10.03 09:57 
Hallo,

ja, das fiel mir dann doch unangenehm auf. Aber, was soll's. Es muss ja nicht 110%ig sein, ist ja nur so 'ne Spielerei.

_________________
Life is a bad adventure, but the graphic is really good!
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 06.10.03 09:25 
Morduk666 hat folgendes geschrieben:
.. naja is n fall für den board admin ;p

Oder ein Fall für einen Moderator. Oder besser vorher noch mal überlegen damit uns die Arbeit erspart bleibt. :wink:
TRomano
Hält's aus hier
Beiträge: 9

Win95 Win98 Win2000 WinXP Win2003
D6 Prof., D7 Enterprises, D8 Architect
BeitragVerfasst: Mo 06.10.03 10:05 
Unter www.lischke-online.de/ gibt es eine extrem schnelle und gute Compo namens "Virtual TreeView". Das Anschauen der sehr umfangreichen Quelltexte lohnt sich ...