Skip to content

Commit

Permalink
#7 Support for multiple columns in lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdurow committed Jun 16, 2015
1 parent ed1f6b5 commit ff4368f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ TEXTAREA.sparkle-Input {
padding-right: 3px;
}

.sparkle-menu-item-img img {
vertical-align: top;
max-height: 16px;
max-width: 16px;
}

.sparkle-menu-item-moreinfo {
padding-left:19px;
color:#666666;
}

.sparkle-menu-item-label {
}

Expand Down
11 changes: 11 additions & 0 deletions SparkleXrmSource/CrmPackage/WebResources/sparkle_/css/sparkle.css
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ TEXTAREA.sparkle-Input {
padding-right: 3px;
}

.sparkle-menu-item-img img {
vertical-align: top;
max-height: 16px;
max-width: 16px;
}

.sparkle-menu-item-moreinfo {
padding-left:19px;
color:#666666;
}

.sparkle-menu-item-label {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ TEXTAREA.sparkle-Input {
padding-right: 3px;
}

.sparkle-menu-item-img img {
vertical-align: top;
max-height: 16px;
max-width: 16px;
}

.sparkle-menu-item-moreinfo {
padding-left:19px;
color:#666666;
}

.sparkle-menu-item-label {
}

Expand Down
11 changes: 11 additions & 0 deletions SparkleXrmSource/DebugWeb/WebResources/sparkle_/css/sparkle.css
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ TEXTAREA.sparkle-Input {
padding-right: 3px;
}

.sparkle-menu-item-img img {
vertical-align: top;
max-height: 16px;
max-width: 16px;
}

.sparkle-menu-item-moreinfo {
padding-left:19px;
color:#666666;
}

.sparkle-menu-item-label {
}

Expand Down
63 changes: 61 additions & 2 deletions SparkleXrmSource/SparkleXrmUI/CustomBinding/XrmLookupBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public override void Init(System.Html.Element element, Func<object> valueAccesso
string nameAttribute = ((string)allBindingsAccessor()["nameAttribute"]);
string idAttribute = ((string)allBindingsAccessor()["idAttribute"]);
string typeCodeAttribute = ((string)allBindingsAccessor()["typeCodeAttribute"]);
string[] columnAttributes = null;
// If there multiple names, add them to the columnAttributes
string[] columns = nameAttribute.Split(",");

if (columns.Length > 1)
{
columnAttributes = columns;
nameAttribute = columnAttributes[0];
}

// wire up source to CRM search
Action<AutoCompleteRequest, Action<AutoCompleteItem[]>> queryDelegate = delegate(AutoCompleteRequest request, Action<AutoCompleteItem[]> response)
Expand All @@ -74,6 +83,8 @@ public override void Init(System.Html.Element element, Func<object> valueAccesso
results[i].Label = (string)fetchResult.Entities[i].GetAttributeValue(nameAttribute);
results[i].Value = fetchResult.Entities[i].GetAttributeValue(idAttribute);
results[i].Data = fetchResult.Entities[i].LogicalName;
GetExtraColumns(columnAttributes, fetchResult, results, i);

string typeCodeName = fetchResult.Entities[i].LogicalName;
// Get the type code from the name to find the icon
if (!string.IsNullOrEmpty(typeCodeAttribute))
Expand Down Expand Up @@ -110,8 +121,16 @@ public override void Init(System.Html.Element element, Func<object> valueAccesso
// Set render template
((RenderItemDelegate)Script.Literal("{0}.data('ui-autocomplete')", inputField))._renderItem = delegate(object ul, AutoCompleteItem item)
{

return (object)jQuery.Select("<li>").Append("<a class='sparkle-menu-item'><span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span><span class='sparkle-menu-item-label'>" + item.Label + "</span></a>").AppendTo((jQueryObject)ul);
string html = "<a class='sparkle-menu-item'><span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span><span class='sparkle-menu-item-label'>" + item.Label + "</span>";
if (item.ColumnValues != null && item.ColumnValues.Length > 0)
{
foreach (string value in item.ColumnValues)
{
html += "<br><span class='sparkle-menu-item-moreinfo'>" + value + "</span>";
}
}
html += "</a>";
return (object)jQuery.Select("<li>").Append(html).AppendTo((jQueryObject)ul);

};

Expand Down Expand Up @@ -176,6 +195,46 @@ public override void Init(System.Html.Element element, Func<object> valueAccesso

//Script.Literal("return { controlsDescendantBindings: true };");
}

/// <summary>
/// Extracts additional column display values from the search result
/// The name attribute can now be a column separated list of attribute logical names
/// </summary>
/// <param name="columnAttributes"></param>
/// <param name="fetchResult"></param>
/// <param name="results"></param>
/// <param name="i"></param>
internal static void GetExtraColumns(string[] columnAttributes, EntityCollection fetchResult, AutoCompleteItem[] results, int i)
{
if (columnAttributes != null)
{
List<string> columnValues = new List<string>();
bool first = true;
// Get the additional column attributes to display
foreach (string attribute in columnAttributes)
{
if (first)
{
first = false;
continue;
}
string value = "";
Entity record = fetchResult.Entities[i];

if (record.FormattedValues.ContainsKey(attribute + "name"))
{
value = record.FormattedValues[attribute + "name"];
}
else
{
value = record.GetAttributeValue(attribute).ToString();
}
columnValues.Add(value);

}
results[i].ColumnValues = (string[])columnValues;
}
}

private static void TrySetObservable(Func<object> valueAccessor, jQueryObject inputField, EntityReference value)
{
Expand Down
25 changes: 22 additions & 3 deletions SparkleXrmSource/SparkleXrmUI/GridEditor/XrmLookupEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using jQueryApi;
using jQueryApi.UI.Widgets;
using Slick;
using SparkleXrm.CustomBinding;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
Expand All @@ -19,6 +20,7 @@ public class XrmLookupEditorOptions
public string nameAttribute;
public string idAttribute;
public string typeCodeAttribute;
public string[] columns;
public bool showImage = true;
}

Expand Down Expand Up @@ -111,6 +113,15 @@ public XrmLookupEditor(EditorArguments args) : base(args)
};
XrmLookupEditorOptions editorOptions = (XrmLookupEditorOptions)args.Column.Options;

// If there multiple names, add them to the columnAttributes
string[] columns = editorOptions.nameAttribute.Split(",");

if (columns.Length > 1)
{
editorOptions.columns = columns;
editorOptions.nameAttribute = columns[0];
}

// wire up source to CRM search
Action<AutoCompleteRequest, Action<AutoCompleteItem[]>> queryDelegate = delegate(AutoCompleteRequest request, Action<AutoCompleteItem[]> response)
{
Expand All @@ -129,7 +140,7 @@ public XrmLookupEditor(EditorArguments args) : base(args)
id.LogicalName = fetchResult.Entities[i].LogicalName;
id.Id = (Guid)fetchResult.Entities[i].GetAttributeValue(editorOptions.idAttribute);
results[i].Value = id;

XrmLookupBinding.GetExtraColumns(editorOptions.columns, fetchResult, results, i);
string typeCodeName = fetchResult.Entities[i].LogicalName;

// Get the type code from the name to find the icon
Expand Down Expand Up @@ -167,8 +178,15 @@ public XrmLookupEditor(EditorArguments args) : base(args)
{
itemHtml += "<span class='sparkle-menu-item-img'><img src='" + item.Image + "'/></span>";
}
itemHtml += "<span class='sparkle-menu-item-label'>" + item.Label + "</span></a>";

itemHtml += "<span class='sparkle-menu-item-label'>" + item.Label + "</span>";
if (item.ColumnValues != null && item.ColumnValues.Length > 0)
{
foreach (string value in item.ColumnValues)
{
itemHtml += "<br><span class='sparkle-menu-item-moreinfo'>" + value + "</span>";
}
}
itemHtml += "</a>";
return (object)jQuery.Select("<li>").Append(itemHtml).AppendTo((jQueryObject)ul);
};

Expand Down Expand Up @@ -333,6 +351,7 @@ public class AutoCompleteItem
public object Value;
public string Image;
public object Data;
public string[] ColumnValues;
}
[Imported]
[IgnoreNamespace]
Expand Down

0 comments on commit ff4368f

Please sign in to comment.