* Fix setting index before dataset is open

This commit is contained in:
michael 2019-06-01 13:05:24 +00:00
parent e3ef3fc9aa
commit 81bf48df6b

View File

@ -282,6 +282,7 @@ type
procedure SetRows(AValue: TJSArray); procedure SetRows(AValue: TJSArray);
procedure SetRowType(AValue: TJSONRowType); procedure SetRowType(AValue: TJSONRowType);
protected protected
procedure ActivateIndex(Build : Boolean);
function ConvertDateTimeToNative(aField : TField; aValue : TDateTime) : JSValue; override; function ConvertDateTimeToNative(aField : TField; aValue : TDateTime) : JSValue; override;
// Determine filter value type based on field type // Determine filter value type based on field type
function FieldTypeToExpressionType(aDataType: TFieldType): TResultType; virtual; function FieldTypeToExpressionType(aDataType: TFieldType): TResultType; virtual;
@ -1031,32 +1032,35 @@ end;
procedure TBaseJSONDataSet.SetActiveIndex(AValue: String); procedure TBaseJSONDataSet.SetActiveIndex(AValue: String);
begin
if FActiveIndex=AValue then Exit;
FActiveIndex:=AValue;
if (csLoading in ComponentState) then
exit;
ActivateIndex(Active);
end;
procedure TBaseJSONDataSet.ActivateIndex(Build : Boolean);
Var Var
Idx : TJSONIndexDef; Idx : TJSONIndexDef;
begin begin
if FActiveIndex=AValue then Exit; if (FActiveIndex<>'') then
if (csLoading in ComponentState) then Idx:=FIndexes.Find(FActiveIndex) as TJSONIndexDef
FActiveIndex:=AValue else
Idx:=nil;
if Idx=Nil then
FCurrentIndex:=FDefaultIndex
else else
begin begin
if (AValue<>'') then if (Idx.Index=Nil) and Build then
Idx:=FIndexes.Find(aValue) as TJSONIndexDef Idx.BuildIndex(Self);
else FCurrentIndex:=Idx.Index;
Idx:=nil;
FActiveIndex:=AValue;
if Not (csLoading in ComponentState) then
if Idx=Nil then
FCurrentIndex:=FDefaultIndex
else
begin
if Idx.Index=Nil then
Idx.BuildIndex(Self);
FCurrentIndex:=Idx.Index;
end;
if Active then
Resync([rmCenter]);
end; end;
if Active then
Resync([rmCenter]);
end; end;
procedure TBaseJSONDataSet.AddToRows(AValue: TJSArray); procedure TBaseJSONDataSet.AddToRows(AValue: TJSArray);
@ -1167,6 +1171,8 @@ procedure TBaseJSONDataSet.AppendToIndexes;
begin begin
FDefaultIndex.AppendToIndex; FDefaultIndex.AppendToIndex;
if Assigned(FCurrentIndex) and (FCurrentIndex<>FDefaultIndex) then
FCurrentIndex.AppendToIndex;
end; end;
procedure TBaseJSONDataSet.CreateIndexes; procedure TBaseJSONDataSet.CreateIndexes;
@ -1174,7 +1180,8 @@ procedure TBaseJSONDataSet.CreateIndexes;
begin begin
FDefaultIndex:=TDefaultJSONIndex.Create(Self,FRows); FDefaultIndex:=TDefaultJSONIndex.Create(Self,FRows);
AppendToIndexes; AppendToIndexes;
FCurrentIndex:=FDefaultIndex; if FCurrentIndex=Nil then
FCurrentIndex:=FDefaultIndex;
end; end;
function TBaseJSONDataSet.FilterExpressionClass : TFPExpressionParserClass; function TBaseJSONDataSet.FilterExpressionClass : TFPExpressionParserClass;
@ -1428,6 +1435,8 @@ begin
CreateFields; CreateFields;
BindFields (True); BindFields (True);
InitDateTimeFields; InitDateTimeFields;
if FActiveIndex<>'' then
ActivateIndex(True);
FCurrent := -1; FCurrent := -1;
end; end;