I apologize if the title of my query is not very descriptive, I couldn't come up with a better one. Please feel free to suggest improvements.
I am currently working on developing a reusable "property grid" in Angular. My goal is to create a grid where an object can be bound, but with the flexibility to customize its presentation.
Below is a snippet of the directive template (excluding the form-element
):
<div ng-repeat="prop in propertyData({object: propertyObject})">
<div ng-switch on="prop.type">
<div ng-switch-when="text">
<form-element type="text"
label-translation-key="{{prop.key}}"
label="{{prop.key}}"
name="{{prop.key}}"
model="propertyObject[prop.key]"
focus-events-enabled="false">
</form-element>
</div>
</div>
</div>
and here is the code for the directive:
angular.module("app.shared").directive('propertyGrid', ['$log', function($log) {
return {
restrict: 'E',
scope: {
propertyObject: '=',
propertyData: '&'
}
templateUrl: 'views/propertyGrid.html'
};
}]);
An example usage of this directive would look like:
<property-grid edit-mode="true"
property-object="selectedSite"
property-data="getSitePropertyData(object)">
</property-grid>
And here is the function getSitePropertyData()
associated with it:
var lastSite;
var lastSitePropertyData;
$scope.getSitePropertyData = function (site) {
if (site == undefined) return null;
if (site == lastSite)
return lastSitePropertyData;
lastSite = site;
lastSitePropertyData = [
{key:"SiteName", value:site.SiteName, editable: true, type:"text"},
//{key:"Company.CompanyName", value:site.Company.CompanyName, editable: false, type:"text"},
{key:"Address1", value:site.Address1, editable: true, type:"text"},
{key:"Address2", value:site.Address2, editable: true, type:"text"},
{key:"PostalCode", value:site.PostalCode, editable: true, type:"text"},
{key:"City", value:site.City, editable: true, type:"text"},
{key:"Country", value:site.Country, editable: true, type:"text"},
{key:"ContactName", value:site.ContactName, editable: true, type:"text"},
{key: "ContactEmail", value: site.ContactEmail, editable: true, type:"email"},
{key: "ContactPhone", value: site.ContactPhone, editable: true, type:"text"},
{key: "Info", value: site.Info, editable: true, type:"text"}
];
return lastSitePropertyData;
};
The reason for using a "property data" function instead of directly binding to object properties is to have control over the order of properties, decide which ones should be displayed, and specify their types for presentation purposes.
Initially, I attempted passing values directly from the getSitePropertyData()
function, but encountered synchronization issues between the object and the property grid. Subsequently, I resorted to using the key
approach, where I could access properties like propertyObject[prop.key]
. However, dealing with nested properties proved challenging as direct bindings do not support them.
I am currently stuck on how to proceed. Both proper bindings and deep property access are crucial requirements. While I know such functionality is achievable (evident in projects like UI Grid), deciphering their implementation would demand considerable time.
Am I on the right track, or is there a better way to approach this?