Jump to content

UniDBGrid with Column Templates, powered by Mustache


Recommended Posts

Hi everyone,

 

With new uniGUI, GetCellData event became virtual and i just made a sample how to use it with a MVC like template system. You can learn (and download) about mustache and figure a way how to use it. This is just a basic sample.

 

You must download mustache Delphi library from https://mustache.github.io/

 

I made a custom DBGrid, and names as TenDBGrid for this sample, enjoy.

 

Best regards.

post-574-0-94881200-1526026125_thumb.png

post-574-0-61709400-1526026132_thumb.png

post-574-0-13971300-1526026142_thumb.png

TemplateSample.rar

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Hmm, DBGrid in her kolonu ayrı cell olarak yaratıldığı için mümkün değil. Bunu sadece örnek olarak koydum, belki ileride daha kapsamlı bir testeği unigui bize sağlayabilir. Ben kendi projelerimde seninle aynı şekilde bir yapıya ihtiyaç duyduğumdan kendimce bir yöntem geliştirdim. Grid üzerinde  tek bir kolon bırakıyorum mesela ID kolonu. GetCellData event i içerisnde oluşturduğum json a bütün dataset sahalarını tanımlıyorum ve bu kolon un template inde kullanıyorum. Kendi kullandığım event kodunu göstereyim.

function GetCellData(const ColNo: Integer; var HasAttr: Boolean): TUniCellRecord;
var template : TaidCrossTemplate;
  mustache: TSynMustache;

  function ParseStrToHtml(AHtml : RawUTF8) : RawUTF8;
  begin
    Result := StringReplace(AHtml, '"', '\"', [rfReplaceAll]);
    Result := StringReplace(StringReplace(Result, #10, ' ', [rfReplaceAll]), #13, ' ', [rfReplaceAll]);
  end;

  function RecordToJSON(ADataSet : TDataSet) : RawUTF8;
  var I : Integer;
  begin
    Result := '';

    if ADataSet.Active then begin
      Result := '{ ';
      for I := 0 to ADataSet.Fields.Count - 1 do begin
        Result := Result + '"' + ADataSet.FieldDefs[I].Name + '": "' + StringReplace(ADataSet.Fields[I].AsString, '"', '\"', [rfReplaceAll]) + '", '
      end;
      Result := LeftStr(Result, Length(Result) - 2) + ' ';
      Result := Result + ' }';
    end;
  end;

begin
  Result := inherited GetCellData(ColNo, HasAttr);

  if FContextNo = 0 then begin
    FContextNo := DataSet.RecNo;
    FContext := RecordToJSON(DataSet);
  end;

  if FContextNo <> DataSet.RecNo then begin
    FContext := RecordToJSON(DataSet);
  end;

  template := TemplateByName(Columns[ColNo].FieldName);
  if template <> nil then begin
    mustache := TSynMustache.Parse(ParseStrToHtml(template.HTML.Text));
    Result.Value := '"' + mustache.RenderJSON(FContext) + '"';
  end;

  if FContextNo = DataSet.RecordCount then FContextNo := 0;
end;

Burada RecordToJSON ile oluşan yapı ile template içerisinde bütün dataset i Field isimleri ile kullanabilirsin.

<b>{{Id}}</b><br /><span>{{TextFieldName}}, {{TimeFieldName}}</span>

gibi.

 

Umarım yardımcı olur.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...