Browse Source

* Fix setting index before dataset is open

michael 6 years ago
parent
commit
81bf48df6b
1 changed files with 29 additions and 20 deletions
  1. 29 20
      packages/fcl-db/jsondataset.pas

+ 29 - 20
packages/fcl-db/jsondataset.pas

@@ -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);
 
 
-Var
-  Idx : TJSONIndexDef;
 
 
 begin
 begin
   if FActiveIndex=AValue then Exit;
   if FActiveIndex=AValue then Exit;
+  FActiveIndex:=AValue;
   if (csLoading in ComponentState) then
   if (csLoading in ComponentState) then
-    FActiveIndex:=AValue
+    exit;
+  ActivateIndex(Active);
+end;
+
+procedure TBaseJSONDataSet.ActivateIndex(Build : Boolean);
+
+Var
+  Idx : TJSONIndexDef;
+
+begin
+  if (FActiveIndex<>'') then
+    Idx:=FIndexes.Find(FActiveIndex) as TJSONIndexDef
+  else
+    Idx:=nil;
+  if Idx=Nil then
+    FCurrentIndex:=FDefaultIndex
   else
   else
     begin
     begin
-    if (AValue<>'') then
-      Idx:=FIndexes.Find(aValue) as TJSONIndexDef
-    else
-      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]);
+    if (Idx.Index=Nil) and Build then
+      Idx.BuildIndex(Self);
+    FCurrentIndex:=Idx.Index;
     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;