How to use foreign key with memberName and load dataSource from Url

1 Answer 243 Views
Grid
Nguyen Ngoc
Top achievements
Rank 1
Iron
Nguyen Ngoc asked on 17 Jun 2021, 09:01 AM | edited on 17 Jun 2021, 03:29 PM

I'm creating a generic grid, but have a problem that the foreign key column cannot use memberName and load datasource from url.
Please help me, thanks.

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 22 Jun 2021, 07:26 AM

Hi Nguyen,

At this point, the "ForeignKey" method supports only predicates. However, I have created a feature request with a vote on your behalf asking to implement a String overload:

https://feedback.telerik.com/aspnet-core-ui/1524863-foreignkey-method-with-string-overload

Feel free to let me know in case of any other queries.

 

Regards, Mihaela Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Nguyen Ngoc
Top achievements
Rank 1
Iron
commented on 22 Jun 2021, 07:35 AM

Thank Mihaela!
Nguyen Ngoc
Top achievements
Rank 1
Iron
commented on 22 Jun 2021, 07:41 AM | edited

Hi Mihaela,

Thank Mihaela, I used the temporary way:

var expression = PropertyExtensions.CreateExpression<T>(col.Name);
colBuild = d.ForeignKey(expression, ds => ds.Read(r => r.Url(col.ForeignUrl)), "Id", "Name");
But it throws an error if I don't specify a specific data type. (Ex: Expression<Func<T, object>>)
public static Expression<Func<T, int?>> CreateExpression<T>(string propertyName)
{
    var arg = Expression.Parameter(typeof(T), "x");
    var property = Expression.Property(arg, propertyName);
    //return the property as object
    //var conv = Expression.Convert(property, typeof(object));
    return Expression.Lambda<Func<T, int?>>(property, new ParameterExpression[] { arg });
}


Mihaela
Telerik team
commented on 24 Jun 2021, 07:32 AM

Here is an example of how to get a field's type ("Type" is the Model type and "memberName" is the name of the field):

internal static MemberInfo FindPropertyOrField(this Type type, string memberName)
{
    MemberInfo memberInfo = type.FindPropertyOrField(memberName, false);
    if (memberInfo == null)
    {
       memberInfo = type.FindPropertyOrField(memberName, true);
    }

    return memberInfo;
}

internal static MemberInfo FindPropertyOrField(this Type type, string memberName, bool staticAccess)
{
    BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly |
        (staticAccess ? BindingFlags.Static : BindingFlags.Instance);
    foreach (Type t in type.SelfAndBaseTypes())
    {
        var members = t.GetProperties(flags)
            .OfType<MemberInfo>()
            .Concat(t.GetFields(flags).OfType<MemberInfo>())
            .Where(m => m.Name.IsCaseInsensitiveEqual(memberName)).ToArray();
        if (members.Length != 0) return members[0];
    }
    return null;
}

internal static IEnumerable<Type> SelfAndBaseTypes(this Type type)
{
    if (type.IsInterface())
    {
        List<Type> types = new List<Type>();
        AddInterface(types, type);
        return types;
    }
    return SelfAndBaseClasses(type);
}

internal static IEnumerable<Type> SelfAndBaseClasses(this Type type)
{
    while (type != null)
    {
        yield return type;
        type = type.GetTypeInfo().BaseType;
    }
}

static void AddInterface(List<Type> types, Type type)
{
    if (!types.Contains(type))
    {
        types.Add(type);
        foreach (Type t in type.GetInterfaces()) AddInterface(types, t);
    }
}
Tags
Grid
Asked by
Nguyen Ngoc
Top achievements
Rank 1
Iron
Answers by
Mihaela
Telerik team
Share this question
or