This is a migrated thread and some comments may be shown as answers.
DataForm dynamic fields
10 Answers 127 Views
This is a migrated thread and some comments may be shown as answers.
roy
Top achievements
Rank 1
roy asked on 01 Sep 2016, 02:12 PM

Hi

I am trying to create a dynamic dataform with dynamic questions, using a json retrieved from a database.

These questions can be different types (text, int, picker, date, etc.).

As far as i understand the only data source you can assign to the dataform is a class of type NotifyPropertyChangedBase. Each property declared in the class is a question in the dataform. 

How can i dynamically create these properties and set the proper editor based on the type? The json contains the question text and type (among other things but they are not relevant for my problem).

Thanks for any advice!

Roy

10 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 05 Sep 2016, 08:56 AM
Hello Roy,

Currently, our DataForm support only binding to CLR objects. What it currently lack is a suitable extension point to plug the custom generation logic as it currently it requires PropertyInfo properties. However, with the upcoming release (in the mid of Septemeber) we will add an extension point.

With the updated version you would be able to use the following snippet:

public class JSONMetadataProvider : PropertyMetadataProviderBase
  {
      private List<EntityProperty> entityProperties = new List<EntityProperty>();
 
      public override List<EntityProperty> EntityProperties
      {
          get
          {
              return entityProperties;
          }
      }
 
      public override void Initialize(object source)
      {
          var data = JObject.Parse(source.ToString());
 
          foreach (JObject item in data["questions"])
          {
              foreach (var jProperty in item.Properties())
              {
                  var metadata = new EntityPropertyMetadata()
                  {
                      Group = ...,
                      Header = ...,
                      PropertyType = ...,
                      PropertyType = ...,
                      ...
                  };
                  var property = new JsonEntityProperty(jProperty, item, metadata);
              }
          }
      }
  }
 
  public class JsonEntityProperty : EntityProperty
  {
      private JProperty property;
      public JsonEntityProperty(JProperty property, object item, EntityPropertyMetadata metadata) : base(property, item, metadata)
      {
          this.property = property;
      }
 
      public override object OriginalValue
      {
          get
          {
              return property.Value;
          }
      }
 
      public override void Commit()
      {
          //TODO: implement
          throw new NotImplementedException();
      }
  }


The setup of the DataForm will look like that:
dataForm.MetadataProvider = new JSONMetadataProvider();
dataForm.Source = jsonData;


We will also issue an article describing the specific case of binding the json.

Thank you for bringing the use case to our attention. I have updated your telerik points acordingly

Regards,
Tsvyatko
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
roy
Top achievements
Rank 1
answered on 08 Sep 2016, 01:35 PM

Hey and thanks for the reply

we have since moved on to using Monotouch.Dialog and are too far in develoment to changing back. But this might come in handy for the next project! ;)

0
Riaz
Top achievements
Rank 1
answered on 01 Apr 2017, 06:58 AM

Hi Tsvyatko, is this feature available now? Can you please point me to the article mentioned "We will also issue an article describing the specific case of binding the json."

Thanks in advance

 

0
Tsvyatko
Telerik team
answered on 06 Apr 2017, 07:19 AM
Hello Roy,

Thank you for getting back to us!

You can check this article how to apply custom datasource to dataform including scenario where JSON is used to supply the data. You can also check the code in our demo app shipped with suite installation - C:\Program Files (x86)\Telerik\UI for Xamarin R1 2017\Examples\XamarinForms 

Regards,
Tsvyatko
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Riaz
Top achievements
Rank 1
answered on 06 Apr 2017, 07:53 AM
Awesome, thank you Tsvyatko.
0
Riaz
Top achievements
Rank 1
answered on 26 Oct 2017, 09:47 PM

When building the forms at runtime (where we cannot populate classes with fields upfront) how is the forms GUI field type also dynamically defined? (e.g. DateEditor vs PickerEditor etc). I can see some attributes referring to Type but they appear to be the type of the custom metadata properties of the field, or the type of the field values being committed,  rather than the GUI type of the field itself.  

Thanks,

0
Lance | Manager Technical Support
Telerik team
answered on 27 Oct 2017, 05:42 PM
Hi Riaz,

Normally, you're correct and the best practices approach would be to use the type's member to set the editor, for example:

dataForm.RegisterEditor(nameof(EmployeeModel.Name), EditorType.TextEditor);


However you can pass a plain string instead:

dataForm.RegisterEditor(nameof("Name"), EditorType.TextEditor);


This does require that you need to know what that field's name is going to be. So either you already know what the member names are, or you will need to check the object graph to see what those properties will be first, then set up your editors.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Omid
Top achievements
Rank 1
answered on 29 Oct 2017, 12:57 PM

Hi
I used this sample code in this path:
C:\Program Files (x86)\Telerik\UI for Xamarin R1 2017\Examples\XamarinForms

Unfortunately, I do not have access to the propertytype atribiute in the initialize method in the CustomMetadataProvider class. I want to create a dynamic dataform with dynamic editor, using a json retrieved from a website. And the dataforms of this program each have different editors  (text, , picker, date, check box…), But since I do not have access to type of editors, I can’t do this. In this Sample, you can only dynamically add a simple texteditor, How can I have a dynamic dataform so that I can dynamically add fields like DateEditor; PickerEditor or CheckBoxEditor?

[quote]Tsvyatko said:Hello Roy,

Currently, our DataForm support only binding to CLR objects. What it currently lack is a suitable extension point to plug the custom generation logic as it currently it requires PropertyInfo properties. However, with the upcoming release (in the mid of Septemeber) we will add an extension point.

With the updated version you would be able to use the following snippet:

public class JSONMetadataProvider : PropertyMetadataProviderBase
  {
      private List<EntityProperty> entityProperties = new List<EntityProperty>();
 
      public override List<EntityProperty> EntityProperties
      {
          get
          {
              return entityProperties;
          }
      }
 
      public override void Initialize(object source)
      {
          var data = JObject.Parse(source.ToString());
 
          foreach (JObject item in data["questions"])
          {
              foreach (var jProperty in item.Properties())
              {
                  var metadata = new EntityPropertyMetadata()
                  {
                      Group = ...,
                      Header = ...,
                      PropertyType = ...,
                      PropertyType = ...,
                      ...
                  };
                  var property = new JsonEntityProperty(jProperty, item, metadata);
              }
          }
      }
  }
 
  public class JsonEntityProperty : EntityProperty
  {
      private JProperty property;
      public JsonEntityProperty(JProperty property, object item, EntityPropertyMetadata metadata) : base(property, item, metadata)
      {
          this.property = property;
      }
 
      public override object OriginalValue
      {
          get
          {
              return property.Value;
          }
      }
 
      public override void Commit()
      {
          //TODO: implement
          throw new NotImplementedException();
      }
  }



The setup of the DataForm will look like that:

dataForm.MetadataProvider = new JSONMetadataProvider();
dataForm.Source = jsonData;



We will also issue an article describing the specific case of binding the json.

Thank you for bringing the use case to our attention. I have updated your telerik points acordingly

Regards,
Tsvyatko
Telerik by Progress

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items

[/quote]

0
Lance | Manager Technical Support
Telerik team
answered on 30 Oct 2017, 03:11 PM
Hello Omid,

The code snippet Tsvyatko provided is a preview of what you'll be able to do an upcoming version of UI for Xamarin. The reason you're not seeing the EntityPropertyMetadata.PropertyType is because it is not available in 2017.3.1018.240 (which is the latest release).

When it is released, you'll be able to set the property type there and the RadDataForm will provide the appropriate editor for that type. If you have any implementation issues when you first try it, open a support ticket here and we'll dig deeper.


Note: I noticed that you do not have a license (trial or purchased), make sure you're added as the licensed user so that you can submit tickets. It takes less than a minute to assign you as the user and can be done here on the Manage Licensed Users page.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Omid
Top achievements
Rank 1
answered on 30 Oct 2017, 06:16 PM

Hi Lance

thanks for the reply. We hope that the next release will be released as soon as possible.

Thanks for any advice!

Asked by
roy
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
roy
Top achievements
Rank 1
Riaz
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Omid
Top achievements
Rank 1
Share this question
or