0

我正在使用 Kendo UI MVC Grid,我想封装样板代码,这样我就不必在每个网格上复制相同的代码。在网格上配置命令如下所示:

columns.Command(command =>
            {
                command.Custom("Edit").Text("<span class='k-icon k-edit'></span>").Click("editRecord");
                command.Custom("Delete").Text("<span class='k-icon k-i-delete'></span>").Click("deleteItem");
            }).Width(130);

编辑和删除是样板文件,但是根据网格,可能会有额外的自定义命令。命令的 lambda 类型是Action<GridActionCommandFactory<T>>. 如何将样板抽象为方法或其他内容,同时仍允许输入自定义命令?对其进行伪编码,我认为它看起来像这样:

columns.Command(command =>
            {
                //Custom commands here
                SomeConfigClass.DefaultGridCommands(command);
                //Custom commands here
            }).Width(130);

或者可能:

columns.Command(command =>
            {
                //Custom commands here
                command.DefaultCommands();
                //Custom commands here
            }).Width(130);

这将包括编辑和删除命令。但是我不知道如何以这种方式修改 lambda 表达式,我该如何实现呢?

4

1 回答 1

0

好吧,我做了更多的挖掘,结果并没有那么难。不确定这是否是最优雅的解决方案,但我是这样做的:

public static Action<GridActionCommandFactory<T>> GetDefaultGridCommands<T>(Action<GridActionCommandFactory<T>> customCommandsBeforeDefault = null, Action<GridActionCommandFactory<T>> customCommandsAfterDefault = null) where T : class
    {
        Action<GridActionCommandFactory<T>> defaultCommands = x =>
        {
            x.Custom("Edit").Text("<span class='k-icon k-edit'></span>").Click("editRecord");
            x.Custom("Delete").Text("<span class='k-icon k-i-delete'></span>").Click("deleteItem");
        };

        List<Action<GridActionCommandFactory<T>>> actions = new List<Action<GridActionCommandFactory<T>>>();

        if(customCommandsBeforeDefault != null)
            actions.Add(customCommandsBeforeDefault);
        actions.Add(defaultCommands);
        if(customCommandsAfterDefault != null)
            actions.Add(customCommandsAfterDefault);

        Action<GridActionCommandFactory<T>> combinedAction = (Action<GridActionCommandFactory<T>>) Delegate.Combine(actions.ToArray());

        return combinedAction;
    }

然后在网格中调用它:

columns.Command(KendoUiGridConfig.GetDefaultGridCommands<MyViewModel>()).Width(130);

Delegate.Combine方法是我正在寻找的。

于 2016-11-04T20:41:18.180 回答