Пример #1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (Loading == null)
            {
                Loading = true;
            }
            var vmQualifiedName = Vm.Model.GetType().AssemblyQualifiedName;

            vmQualifiedName = vmQualifiedName.Substring(0, vmQualifiedName.LastIndexOf(", Version=", StringComparison.CurrentCulture));

            var tempGridTitleId = Guid.NewGuid().ToNoSplitString();

            output.TagName = "table";
            output.Attributes.Add("id", Id);
            output.Attributes.Add("lay-filter", Id);
            output.TagMode = TagMode.StartTagAndEndTag;

            if (Limit == null)
            {
                Limit = GlobalServices.GetRequiredService <Configs>()?.UiOptions.DataTable.RPP;
            }
            if (Limits == null)
            {
                Limits = new int[] { 10, 20, 50, 80, 100, 150, 200 };
                if (!Limits.Contains(Limit.Value))
                {
                    var list = Limits.ToList();
                    list.Add(Limit.Value);
                    Limits = list.OrderBy(x => x).ToArray();
                }
            }
            if (UseLocalData) // 不需要分页
            {
                ListVM.NeedPage = false;
            }
            else
            {
                if (string.IsNullOrEmpty(Url))
                {
                    Url = "/_Framework/GetPagingData";
                }
                if (Filter == null)
                {
                    Filter = new Dictionary <string, object>();
                }
                Filter.Add("_DONOT_USE_VMNAME", vmQualifiedName);
                Filter.Add("_DONOT_USE_CS", ListVM.CurrentCS);
                Filter.Add("SearcherMode", ListVM.SearcherMode);
                if (ListVM.Ids != null && ListVM.Ids.Count > 0)
                {
                    Filter.Add("Ids", ListVM.Ids);
                }
                // 为首次加载添加Searcher查询参数
                if (ListVM.Searcher != null)
                {
                    var props = ListVM.Searcher.GetType().GetProperties();
                    props = props.Where(x => !_excludeTypes.Contains(x.PropertyType)).ToArray();
                    foreach (var prop in props)
                    {
                        if (!_excludeParams.Contains(prop.Name))
                        {
                            if (prop.PropertyType.IsGenericType == false || (prop.PropertyType.GenericTypeArguments[0] != typeof(ComboSelectListItem) && prop.PropertyType.GenericTypeArguments[0] != typeof(TreeSelectListItem)))
                            {
                                Filter.Add($"Searcher.{prop.Name}", prop.GetValue(ListVM.Searcher));
                            }
                        }
                    }
                }
            }

            // 是否需要分页
            var page = ListVM.NeedPage;

            #region 生成 Layui 所需的表头
            var rawCols   = ListVM?.GetHeaders();
            var maxDepth  = (ListVM?.ChildrenDepth) ?? 1;
            var layuiCols = new List <List <LayuiColumn> >();

            var tempCols = new List <LayuiColumn>();
            layuiCols.Add(tempCols);
            // 添加复选框
            if (!HiddenCheckbox)
            {
                var checkboxHeader = new LayuiColumn()
                {
                    Type        = LayuiColumnTypeEnum.Checkbox,
                    LAY_CHECKED = CheckedAll,
                    Rowspan     = maxDepth,
                    Fixed       = GridColumnFixedEnum.Left,
                    UnResize    = true,
                    //Width = 45
                };
                tempCols.Add(checkboxHeader);
            }
            // 添加序号列
            if (!HiddenGridIndex)
            {
                var gridIndex = new LayuiColumn()
                {
                    Type     = LayuiColumnTypeEnum.Numbers,
                    Rowspan  = maxDepth,
                    Fixed    = GridColumnFixedEnum.Left,
                    UnResize = true,
                    //Width = 45
                };
                tempCols.Add(gridIndex);
            }
            var nextCols = new List <IGridColumn <TopBasePoco> >();// 下一级列头

            generateColHeader(rawCols, nextCols, tempCols, maxDepth);

            if (nextCols.Count > 0)
            {
                CalcChildCol(layuiCols, nextCols, maxDepth, 1);
            }

            if (layuiCols.Count > 0 && layuiCols[0].Count > 0)
            {
                layuiCols[0][0].TotalRowText = ListVM?.TotalText;
            }

            #endregion

            #region 处理 DataTable 操作按钮

            var actionCol = ListVM?.GridActions;

            var rowBtnStrBuilder       = new StringBuilder(); // Grid 行内按钮
            var toolBarBtnStrBuilder   = new StringBuilder(); // Grid 工具条按钮
            var gridBtnEventStrBuilder = new StringBuilder(); // Grid 按钮事件

            if (actionCol != null && actionCol.Count > 0)
            {
                var vm = Vm.Model as BaseVM;
                foreach (var item in actionCol)
                {
                    AddSubButton(vmQualifiedName, rowBtnStrBuilder, toolBarBtnStrBuilder, gridBtnEventStrBuilder, vm, item);
                }
            }
            #endregion

            #region DataTable

            var vmName = string.Empty;
            if (VMType != null)
            {
                var vmQualifiedName1 = VMType.AssemblyQualifiedName;
                vmName = vmQualifiedName1.Substring(0, vmQualifiedName1.LastIndexOf(", Version=", StringComparison.CurrentCulture));
            }
            output.PostElement.AppendHtml($@"
<script>
var {Id}option = null;
/* 监听工具条 */
function wtToolBarFunc_{Id}(obj){{ //注:tool是工具条事件名,test是table原始容器的属性 lay-filter=""对应的值""
var data = obj.data, layEvent = obj.event, tr = obj.tr; //获得当前行 tr 的DOM对象
{(gridBtnEventStrBuilder.Length == 0 ? string.Empty : $@"switch(layEvent){{{gridBtnEventStrBuilder}default:break;}}")}
return;
}}
layui.use(['table'], function(){{
  var table = layui.table;
  {Id}option = {{
    elem: '#{Id}'
    ,id: '{Id}'
    {(!NeedShowTotal ? string.Empty : ",totalRow:true")}
    {(UseLocalData ? string.Empty : $",url: '{Url}'")}
    {(Filter == null || Filter.Count == 0 ? string.Empty : $",where: {JsonConvert.SerializeObject(Filter)}")}
    {(Method == null ? ",method:'post'" : $",method: '{Method.Value.ToString().ToLower()}'")}
    {(Loading ?? true ? string.Empty : ",loading:false")}
    {(page ? string.Empty : ",page:{layout:['count']}")}
    {(page ? $",limit:{Limit}" : $",limit:{(UseLocalData ? ListVM.GetEntityList().Count().ToString() : "0")}")}
    {(page
        ? (Limits == null || Limits.Length == 0
            ? string.Empty
            : $",limits:[{string.Join(',', Limits)}]"
        )
        : string.Empty)}
    {(!Width.HasValue ? string.Empty : $",width: {Width.Value}")}
    {(!Height.HasValue ? string.Empty : (Height.Value >= 0 ? $",height: {Height.Value}" : $",height: 'full{Height.Value}'"))}
    ,cols:{JsonConvert.SerializeObject(layuiCols, _jsonSerializerSettings)}
    {(!Skin.HasValue ? string.Empty : $",skin: '{Skin.Value.ToString().ToLower()}'")}
    {(Even.HasValue && !Even.Value ? $",even: false" : string.Empty)}
    {(!Size.HasValue ? string.Empty : $",size: '{Size.Value.ToString().ToLower()}'")}
    ,done: function(res,curr,count){{
      var tab = $('#{Id} + .layui-table-view');tab.find('table').css('border-collapse','separate');
      {(Height == null ? $"tab.css('overflow','hidden').addClass('donotuse_fill donotuse_pdiv');tab.children('.layui-table-box').addClass('donotuse_fill donotuse_pdiv').css('height','100px');tab.find('.layui-table-main').addClass('donotuse_fill');tab.find('.layui-table-header').css('min-height','40px');ff.triggerResize();" : string.Empty)}
      {(MultiLine == true ? $"tab.find('.layui-table-cell').css('height','auto').css('white-space','normal');" : string.Empty)}
      {(string.IsNullOrEmpty(DoneFunc) ? string.Empty : $"{DoneFunc}(res,curr,count)")}
Пример #2
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (Loading == null)
            {
                Loading = true;
            }
            var vmQualifiedName = Vm.Model.GetType().AssemblyQualifiedName;

            vmQualifiedName = vmQualifiedName.Substring(0, vmQualifiedName.LastIndexOf(", Version="));

            var tempGridTitleId = Guid.NewGuid().ToNoSplitString();

            output.TagName = "table";
            output.Attributes.Add("id", Id);
            output.Attributes.Add("lay-filter", Id);
            output.TagMode = TagMode.StartTagAndEndTag;
            //IEnumerable<TopBasePoco> data = null;
            if (Limit == null)
            {
                Limit = GlobalServices.GetRequiredService <Configs>()?.RPP;
            }
            if (Limits == null)
            {
                Limits = new int[] { 10, 20, 50, 80, 100, 150, 200 };
                if (!Limits.Contains(Limit.Value))
                {
                    var list = Limits.ToList();
                    list.Add(Limit.Value);
                    Limits = list.OrderBy(x => x).ToArray();
                }
            }
            // TODO 转换有问题
            Page = ListVM.NeedPage;
            if (UseLocalData)
            {
                // 不需要分页
                ListVM.NeedPage = false;
                //data = ListVM.GetEntityList().ToList();
            }
            else if (string.IsNullOrEmpty(Url))
            {
                Url = "/_Framework/GetPagingData";

                if (Filter == null)
                {
                    Filter = new Dictionary <string, object>();
                }
                Filter.Add("_DONOT_USE_VMNAME", vmQualifiedName);
                Filter.Add("_DONOT_USE_CS", ListVM.CurrentCS);
                Filter.Add("SearcherMode", ListVM.SearcherMode);
                if (ListVM.Ids != null && ListVM.Ids.Count > 0)
                {
                    Filter.Add("Ids", ListVM.Ids);
                }
                // 为首次加载添加Searcher查询参数
                if (ListVM.Searcher != null)
                {
                    var props = ListVM.Searcher.GetType().GetProperties();
                    props = props.Where(x => !_excludeTypes.Contains(x.PropertyType)).ToArray();
                    foreach (var prop in props)
                    {
                        if (!_excludeParams.Contains(prop.Name))
                        {
                            Filter.Add($"Searcher.{prop.Name}", prop.GetValue(ListVM.Searcher));
                        }
                    }
                }
            }

            var request = new Dictionary <string, object>
            {
                { "pageName", "Searcher.Page" },   //页码的参数名称,默认:page
                { "limitName", "Searcher.Limit" }, //每页数据量的参数名,默认:limit
            };
            var response = new Dictionary <string, object>
            {
                { "statusName", "Code" }, //数据状态的字段名称,默认:code
                { "statusCode", 200 },    //成功的状态码,默认:0
                { "msgName", "Msg" },     //状态信息的字段名称,默认:msg
                { "countName", "Count" }, //数据总数的字段名称,默认:count
                { "dataName", "Data" }    //数据列表的字段名称,默认:data
            };

            #region 生成 Layui 所需的表头
            var rawCols   = ListVM?.GetHeaders();
            var maxDepth  = (ListVM?.ChildrenDepth) ?? 1;
            var layuiCols = new List <List <LayuiColumn> >();
            var tempCols  = new List <LayuiColumn>();
            layuiCols.Add(tempCols);
            // 添加复选框
            if (!HiddenCheckbox)
            {
                var checkboxHeader = new LayuiColumn()
                {
                    Type        = LayuiColumnTypeEnum.Checkbox,
                    LAY_CHECKED = CheckedAll,
                    Rowspan     = maxDepth
                };
                tempCols.Add(checkboxHeader);
            }
            // 添加序号列
            if (!HiddenGridIndex)
            {
                var gridIndex = new LayuiColumn()
                {
                    Type    = LayuiColumnTypeEnum.Numbers,
                    Rowspan = maxDepth
                };
                tempCols.Add(gridIndex);
            }
            var nextCols = new List <IGridColumn <TopBasePoco> >();// 下一级列头
            foreach (var item in rawCols)
            {
                var tempCol = new LayuiColumn()
                {
                    Title    = item.Title,
                    Field    = item.Field,
                    Width    = item.Width,
                    Sort     = item.Sort,
                    Fixed    = item.Fixed,
                    Align    = item.Align,
                    UnResize = item.UnResize,
                    Hide     = item.Hide,
                    //Style = "height:auto !important;white-space:normal !important"
                    //EditType = item.EditType
                };
                switch (item.ColumnType)
                {
                case GridColumnTypeEnum.Space:
                    tempCol.Type = LayuiColumnTypeEnum.Space;
                    break;

                case GridColumnTypeEnum.Action:
                    tempCol.Toolbar = $"#{ToolBarId}";
                    break;

                default:
                    break;
                }
                if (item.Children != null && item.Children.Count() > 0)
                {
                    tempCol.Colspan = item.ChildrenLength;
                }
                if (maxDepth > 1 && (item.Children == null || item.Children.Count() == 0))
                {
                    tempCol.Rowspan = maxDepth;
                }
                tempCols.Add(tempCol);
                if (item.Children != null && item.Children.Count() > 0)
                {
                    nextCols.AddRange(item.Children);
                }
            }
            if (nextCols.Count > 0)
            {
                CalcChildCol(layuiCols, nextCols, maxDepth, 1);
            }

            #endregion

            #region 处理 DataTable 操作按钮

            var actionCol = ListVM?.GridActions;

            var rowBtnStrBuilder       = new StringBuilder(); // Grid 行内按钮
            var toolBarBtnStrBuilder   = new StringBuilder(); // Grid 工具条按钮
            var gridBtnEventStrBuilder = new StringBuilder(); // Grid 按钮事件

            if (actionCol != null && actionCol.Count > 0)
            {
                var vm = Vm.Model as BaseVM;
                foreach (var item in actionCol)
                {
                    AddSubButton(vmQualifiedName, rowBtnStrBuilder, toolBarBtnStrBuilder, gridBtnEventStrBuilder, vm, item);
                }
            }
            #endregion

            #region DataTable

            var vmName = string.Empty;
            if (VMType != null)
            {
                var vmQualifiedName1 = VMType.AssemblyQualifiedName;
                vmName = vmQualifiedName1.Substring(0, vmQualifiedName1.LastIndexOf(", Version="));
            }
            output.PostElement.AppendHtml($@"
<script>
  var table = layui.table;
  /* 暂时解决 layui table首次及table.reload()无loading的bug */
  var layer = layui.layer;
  var msg = layer.msg('数据请求中', {{
    icon: 16,
    time: -1,
    anim: -1,
    fixed: false
  }})
  /* 暂时解决 layui table首次及table.reload()无loading的bug */
 var {Id}option = {{
    elem: '#{Id}'
    ,id: '{Id}'
    ,autoSort: false
    {(string.IsNullOrEmpty(Url) ? string.Empty : $",url: '{Url}'")}
    {(Filter == null || Filter.Count == 0 ? string.Empty : $",where: {JsonConvert.SerializeObject(Filter)}")}
    {(Method == null ? ",method:'post'" : $",method: '{Method.Value.ToString().ToLower()}'")}
    {(UseLocalData ? $",data: {ListVM.GetDataJson()}" : string.Empty)}
    {(!Loading.HasValue ? string.Empty : $",loading: {Loading.Value.ToString().ToLower()}")}
    ,request: {JsonConvert.SerializeObject(request)}
    ,response: {JsonConvert.SerializeObject(response)}
    {(Page ?? true ? ",page:true" : ",page:{layout:['count']}")}
    ,limit: {(Page ?? true ? $"{Limit ?? 50}" : $"{(UseLocalData ? ListVM.GetEntityList().Count().ToString() : "0")}")}
    {(Page ?? true ?
        (Limits == null || Limits.Length == 0 ? ",limits:[10,20,50,80,100,150,200]" : $",limits:{JsonConvert.SerializeObject(Limits)}")
        : string.Empty)}
    {(!Width.HasValue ? string.Empty : $",width: {Width.Value}")}
    {(!Height.HasValue ? string.Empty : (Height.Value >= 0 ? $",height: {Height.Value}" : $",height: 'full{Height.Value}'"))}
    ,cols:{JsonConvert.SerializeObject(layuiCols, _jsonSerializerSettings)}
    {(!Skin.HasValue ? string.Empty : $",skin: '{Skin.Value.ToString().ToLower()}'")}
    {(!Even.HasValue ? ",even: true" : $",even: {Even.Value.ToString().ToLower()}")}
    {(!Size.HasValue ? string.Empty : $",size: '{Size.Value.ToString().ToLower()}'")}
,done: function(res,curr,count){{layer.close(msg);
    var tab = $('#{Id} + .layui-table-view');tab.find('table').css('border-collapse','separate');
    {(Height == null ? $"tab.css('overflow','hidden').addClass('donotuse_fill donotuse_pdiv');tab.children('.layui-table-box').addClass('donotuse_fill donotuse_pdiv').css('height','100px');tab.find('.layui-table-main').addClass('donotuse_fill');tab.find('.layui-table-header').css('min-height','40px');ff.triggerResize();" : string.Empty)}
    {(MultiLine == true ? $"tab.find('.layui-table-cell').css('height','auto').css('white-space','normal');" : string.Empty)}
    {(string.IsNullOrEmpty(DoneFunc) ? string.Empty : $"{DoneFunc}(res,curr,count)")}
Пример #3
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (Loading == null)
            {
                Loading = true;
            }
            var vmQualifiedName = Vm.Model.GetType().AssemblyQualifiedName;

            vmQualifiedName = vmQualifiedName.Substring(0, vmQualifiedName.LastIndexOf(", Version=", StringComparison.CurrentCulture));

            var tempGridTitleId = Guid.NewGuid().ToNoSplitString();

            output.TagName = "table";
            output.Attributes.Add("id", Id);
            output.Attributes.Add("lay-filter", Id);
            output.TagMode = TagMode.StartTagAndEndTag;

            var config = GlobalServices.GetRequiredService <Configs>();


            if (UseLocalData == false)
            {
                if (NeedShowPrint == null)
                {
                    NeedShowPrint = config?.UiOptions.DataTable.ShowPrint;
                }
                if (NeedShowFilter == null)
                {
                    NeedShowFilter = config?.UiOptions.DataTable.ShowFilter;
                }
            }
            var righttoolbar      = ",defaultToolbar: []";
            int lefttoolbarmergin = -120;

            if (NeedShowFilter == true && NeedShowPrint == true)
            {
                righttoolbar      = ",defaultToolbar: ['filter', 'print']";
                lefttoolbarmergin = -45;
            }
            else
            {
                if (NeedShowFilter == true)
                {
                    righttoolbar      = ",defaultToolbar: ['filter']";
                    lefttoolbarmergin = -80;
                }
                if (NeedShowPrint == true)
                {
                    righttoolbar      = ",defaultToolbar: ['print']";
                    lefttoolbarmergin = -80;
                }
            }

            if (Limit == null)
            {
                Limit = config?.UiOptions.DataTable.RPP;
            }
            if (Limits == null)
            {
                Limits = new int[] { 10, 20, 50, 80, 100, 150, 200 };
                if (!Limits.Contains(Limit.Value))
                {
                    var list = Limits.ToList();
                    list.Add(Limit.Value);
                    Limits = list.OrderBy(x => x).ToArray();
                }
            }
            if (UseLocalData) // 不需要分页
            {
                ListVM.NeedPage = false;
            }
            else
            {
                if (string.IsNullOrEmpty(Url))
                {
                    // TODO 已废弃,预计v3.0版本及v2.10版本开始将删除
                    Url = "/_Framework/GetPagingData";
                }
                if (Filter == null)
                {
                    Filter = new Dictionary <string, object>();
                }
                Filter.Add("_DONOT_USE_VMNAME", vmQualifiedName);
                Filter.Add("_DONOT_USE_CS", ListVM.CurrentCS);
                Filter.Add("SearcherMode", ListVM.SearcherMode);
                Filter.Add("ViewDivId", ListVM.ViewDivId);
                if (ListVM.Ids != null && ListVM.Ids.Count > 0)
                {
                    Filter.Add("Ids", ListVM.Ids);
                }
                // 为首次加载添加Searcher查询参数
                if (ListVM.Searcher != null)
                {
                    var props = ListVM.Searcher.GetType().GetProperties();
                    props = props.Where(x => !_excludeTypes.Contains(x.PropertyType)).ToArray();
                    foreach (var prop in props)
                    {
                        if (!_excludeParams.Contains(prop.Name))
                        {
                            if (prop.PropertyType.IsGenericType == false || (prop.PropertyType.GenericTypeArguments[0] != typeof(ComboSelectListItem) && prop.PropertyType.GenericTypeArguments[0] != typeof(TreeSelectListItem)))
                            {
                                var listvalue = prop.GetValue(ListVM.Searcher);
                                if (listvalue != null)
                                {
                                    Filter.Add($"Searcher.{prop.Name}", prop.GetValue(ListVM.Searcher));
                                }
                            }
                        }
                    }
                }
            }

            // 是否需要分页
            var page = ListVM.NeedPage;

            #region 生成 Layui 所需的表头
            var rawCols   = ListVM?.GetHeaders();
            var maxDepth  = (ListVM?.GetChildrenDepth()) ?? 1;
            var layuiCols = new List <List <LayuiColumn> >();

            var tempCols = new List <LayuiColumn>();
            layuiCols.Add(tempCols);
            // 添加复选框
            if (!HiddenCheckbox)
            {
                var checkboxHeader = new LayuiColumn()
                {
                    Type        = LayuiColumnTypeEnum.Checkbox,
                    LAY_CHECKED = CheckedAll,
                    Rowspan     = maxDepth,
                    Fixed       = GridColumnFixedEnum.Left,
                    UnResize    = true,
                    //Width = 45
                };
                tempCols.Add(checkboxHeader);
            }
            // 添加序号列
            if (!HiddenGridIndex)
            {
                var gridIndex = new LayuiColumn()
                {
                    Type     = LayuiColumnTypeEnum.Numbers,
                    Rowspan  = maxDepth,
                    Fixed    = GridColumnFixedEnum.Left,
                    UnResize = true,
                    //Width = 45
                };
                tempCols.Add(gridIndex);
            }
            var nextCols = new List <IGridColumn <TopBasePoco> >();// 下一级列头

            generateColHeader(rawCols, nextCols, tempCols, maxDepth);

            if (nextCols.Count > 0)
            {
                CalcChildCol(layuiCols, nextCols, maxDepth, 1);
            }

            if (layuiCols.Count > 0 && layuiCols[0].Count > 0)
            {
                layuiCols[0][0].TotalRowText = ListVM?.TotalText;
            }

            #endregion

            #region 处理 DataTable 操作按钮

            var actionCol = ListVM?.GetGridActions();

            var rowBtnStrBuilder       = new StringBuilder(); // Grid 行内按钮
            var toolBarBtnStrBuilder   = new StringBuilder(); // Grid 工具条按钮
            var gridBtnEventStrBuilder = new StringBuilder(); // Grid 按钮事件

            if (actionCol != null && actionCol.Count > 0)
            {
                var vm = Vm.Model as BaseVM;
                foreach (var item in actionCol)
                {
                    AddSubButton(vmQualifiedName, rowBtnStrBuilder, toolBarBtnStrBuilder, gridBtnEventStrBuilder, vm, item);
                }
            }
            if (hasButtonGroup == true)
            {
                toolBarBtnStrBuilder.Append($@"<script type=""text/javascript"" des=""buttongroup"">layui.use([""form""], function () {{
                            var form = layui.form, $ = layui.jquery;
                            $("".downpanel"").on(""click"", "".layui-select-title"", function(e) {{
                                $("".layui-form-select"").not($(this).parents("".layui-form-select"")).removeClass(""layui-form-selected"");
                                $(this).parents("".layui-form-select"").toggleClass(""layui-form-selected"");
                                            e.stopPropagation();
                                        }});
                            $(document).click(function(event) {{
                            var _con2 = $("".downpanel"");
                            if (!_con2.is (event.target) && (_con2.has(event.target).length === 0)) {{
                            _con2.removeClass(""layui -form-selected"");
                            }}
                            }});
                            }});</script>");
            }

            #endregion

            #region DataTable
            var toolbardef = "";
            if (toolBarBtnStrBuilder.Length > 0 || NeedShowFilter == true || NeedShowPrint == true)
            {
                toolbardef = $" ,toolbar: '#{ToolBarId}2'";
            }
            var vmName = string.Empty;
            if (VMType != null)
            {
                var vmQualifiedName1 = VMType.AssemblyQualifiedName;
                vmName = vmQualifiedName1.Substring(0, vmQualifiedName1.LastIndexOf(", Version=", StringComparison.CurrentCulture));
            }
            output.PostElement.AppendHtml($@"
<script>
var {Id}option = null;
/* 监听工具条 */
function wtToolBarFunc_{Id}(obj){{ //注:tool是工具条事件名,test是table原始容器的属性 lay-filter=""对应的值""
var data = obj.data, layEvent = obj.event, tr = obj.tr; //获得当前行 tr 的DOM对象
{(gridBtnEventStrBuilder.Length == 0 ? string.Empty : $@"var ids; var objs;switch(layEvent){{{gridBtnEventStrBuilder}default:break;}}")}
return;
}}
layui.use(['table'], function(){{
  var table = layui.table;
  {Id}option = {{
    elem: '#{Id}'
    ,id: '{Id}'
    ,text:{{
        none:'{Program._localizer["NoData"]}'
    }}
    {toolbardef}
    {righttoolbar}
    {(!NeedShowTotal ? string.Empty : ",totalRow:true")}
    {(UseLocalData ? string.Empty : $",url: '{Url}'")}
    ,headers: {{layuisearch: 'true'}}
    {(Filter == null || Filter.Count == 0 ? string.Empty : $",where: {JsonConvert.SerializeObject(Filter)}")}
    {(Method == null ? ",method:'post'" : $",method: '{Method.Value.ToString().ToLower()}'")}
    {(Loading ?? true ? string.Empty : ",loading:false")}
    {(page ? $@",page:{{
        rpptext:'{Program._localizer["RecordsPerPage"]}',
        totaltext:'{Program._localizer["Total"]}',
        recordtext:'{Program._localizer["Record"]}',
        gototext:'{Program._localizer["Goto"]}',
        pagetext:'{Program._localizer["Page"]}',
        oktext:'{Program._localizer["GotoButtonText"]}',
    }}":",page:false")}
    {(page ? $",limit:{Limit}" : $",limit:{(UseLocalData ? ListVM.GetEntityList().Count().ToString() : "0")}")}
    {(page
        ? (Limits == null || Limits.Length == 0
            ? string.Empty
            : $",limits:[{string.Join(',', Limits)}]"
        )
        : string.Empty)}
    {(!Width.HasValue ? string.Empty : $",width: {Width.Value}")}
    {(!Height.HasValue ? string.Empty : (Height.Value >= 0 ? $",height: {Height.Value}" : $",height: 'full{Height.Value}'"))}
    ,cols:{JsonConvert.SerializeObject(layuiCols, _jsonSerializerSettings)}
    {(!Skin.HasValue ? string.Empty : $",skin: '{Skin.Value.ToString().ToLower()}'")}
    {(Even.HasValue && !Even.Value ? $",even: false" : string.Empty)}
    {(!Size.HasValue ? string.Empty : $",size: '{Size.Value.ToString().ToLower()}'")}
    ,done: function(res,curr,count){{
        this.where={Id}defaultfilter.where;
      if(res.Code == 401){{ layui.layer.confirm(res.Msg,{{title:'{Program._localizer["Error"]}'}}, function(index){{window.location.reload();layer.close(index);}});}}
      if(res.Code != undefined && res.Code != 200){{ layui.layer.alert(res.Msg,{{title:'{Program._localizer["Error"]}'}});}}
     var tab = $('#{Id} + .layui-table-view');tab.find('table').css('border-collapse','separate');
      {(Height == null ? $"tab.css('overflow','hidden').addClass('donotuse_fill donotuse_pdiv');tab.children('.layui-table-box').addClass('donotuse_fill donotuse_pdiv').css('height','100px');tab.find('.layui-table-main').addClass('donotuse_fill');tab.find('.layui-table-header').css('min-height','40px');ff.triggerResize();" : string.Empty)}
      {(MultiLine == true ? $"tab.find('.layui-table-cell').css('height','auto').css('white-space','normal');" : string.Empty)}
       tab.find('div [lay-event=\'LAYTABLE_COLS\']').attr('title','{Program._localizer["ColumnFilter"]}');
       tab.find('div [lay-event=\'LAYTABLE_PRINT\']').attr('title','{Program._localizer["Print"]}');
      {(string.IsNullOrEmpty(DoneFunc) ? string.Empty : $"{DoneFunc}(res,curr,count)")}
Пример #4
0
        /// <summary>
        /// 批量修改,默认对Ids中包含的数据进行修改,子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchEdit()
        {
            //获取批量修改VM的所有属性
            var         pros    = LinkedVM.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
            bool        rv      = true;
            List <Guid> idsData = new List <Guid>(Ids);
            //找到对应的BaseCRUDVM,并初始化
            var vmtype = this.GetType().Assembly.GetExportedTypes().Where(x => x.IsSubclassOf(typeof(BaseCRUDVM <TModel>))).FirstOrDefault();
            IBaseCRUDVM <TModel> vm = null;

            if (vmtype != null)
            {
                vm = vmtype.GetConstructor(System.Type.EmptyTypes).Invoke(null) as IBaseCRUDVM <TModel>;
                vm.CopyContext(this);
            }
            //循环所有数据
            for (int i = 0; i < idsData.Count; i++)
            {
                try
                {
                    //如果找不到对应数据,则输出错误
                    TModel entity = null;
                    entity = DC.Set <TModel>().Find(idsData[i]);
                    if (vm != null)
                    {
                        vm.SetEntity(entity);
                    }
                    if (entity == null)
                    {
                        ErrorMessage.Add(idsData[i], "数据不存在");
                        rv = false;
                        break;
                    }
                    //如果能找到,则循环LinkedVM中的属性,给entity中同名属性赋值
                    foreach (var pro in pros)
                    {
                        var proToSet = entity.GetType().GetProperty(pro.Name);
                        var val      = FC.ContainsKey("LinkedVM." + pro.Name) ? FC["LinkedVM." + pro.Name] : null;
                        if (proToSet != null && val != null)
                        {
                            var hasvalue = false;
                            if (val is StringValues sv && StringValues.IsNullOrEmpty(sv) == false)
                            {
                                hasvalue = true;
                            }
                            if (hasvalue)
                            {
                                proToSet.SetValue(entity, pro.GetValue(LinkedVM));
                            }
                        }
                    }
                    //如果有对应的BaseCRUDVM则使用其进行数据验证
                    if (vm != null)
                    {
                        vm.Validate();
                        var errors = vm.MSD;
                        if (errors != null && errors.Count > 0)
                        {
                            var error = "";
                            foreach (var key in errors.Keys)
                            {
                                if (errors[key].Count > 0)
                                {
                                    error += errors[key].Select(x => x.ErrorMessage).ToSpratedString();
                                }
                            }
                            if (error != "")
                            {
                                ErrorMessage.Add(idsData[i], error);
                                rv = false;
                                break;
                            }
                        }
                    }
                    if (typeof(TModel).IsSubclassOf(typeof(BasePoco)))
                    {
                        BasePoco ent = entity as BasePoco;
                        if (ent.CreateTime == null)
                        {
                            ent.CreateTime = DateTime.Now;
                        }
                        if (string.IsNullOrEmpty(ent.CreateBy))
                        {
                            ent.CreateBy = LoginUserInfo?.ITCode;
                        }
                    }
                    DC.UpdateEntity(entity);
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, idsData[i]);
                    rv = false;
                }
            }
            //进行数据库的修改操作
            if (rv == true)
            {
                try
                {
                    DC.SaveChanges();
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, null);
                    rv = false;
                }
            }

            //如果有错误,输出错误信息
            if (rv == false)
            {
                if (ErrorMessage.Count > 0)
                {
                    foreach (var id in idsData)
                    {
                        if (!ErrorMessage.ContainsKey(id))
                        {
                            ErrorMessage.Add(id, "已回滚");
                        }
                    }
                }
                ListVM.DoSearch();
                foreach (var item in ListVM.GetEntityList())
                {
                    item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault();
                }
            }
            return(rv);
        }
Пример #5
0
        /// <summary>
        /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchDelete()
        {
            bool rv = true;
            //循环所有数据Id
            List <Guid> idsData = new List <Guid>(Ids);

            for (int i = 0; i < idsData.Count; i++)
            {
                string checkErro = null;
                //检查是否可以删除,如不能删除则直接跳过
                if (CheckIfCanDelete(idsData[i], out checkErro) == false)
                {
                    ErrorMessage.Add(idsData[i], checkErro);
                    rv = false;
                    break;
                }
                //进行删除
                try
                {
                    var ctor = typeof(TModel).GetConstructor(Type.EmptyTypes);
                    if (typeof(TModel).IsSubclassOf(typeof(PersistPoco)))
                    {
                        var pp = DC.Set <TModel>().Find(idsData[i]);
                        (pp as PersistPoco).IsValid    = false;
                        (pp as PersistPoco).UpdateTime = DateTime.Now;
                        (pp as PersistPoco).UpdateBy   = LoginUserInfo.ITCode;
                        DC.UpdateProperty(pp, "IsValid");
                        DC.UpdateProperty(pp, "UpdateTime");
                        DC.UpdateProperty(pp, "UpdateBy");
                    }
                    else
                    {
                        TModel m = ctor.Invoke(null) as TModel;
                        m.ID = idsData[i];
                        DC.Set <TModel>().Attach(m);
                        DC.DeleteEntity(m);
                    }
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, idsData[i]);
                    rv = false;
                }
            }
            //进行数据库的删除操作
            if (rv == true)
            {
                try
                {
                    DC.SaveChanges();
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, null);
                    rv = false;
                }
            }
            //如果失败,添加错误信息
            if (rv == false)
            {
                if (ErrorMessage.Count > 0)
                {
                    foreach (var id in idsData)
                    {
                        if (!ErrorMessage.ContainsKey(id))
                        {
                            ErrorMessage.Add(id, "已回滚");
                        }
                    }
                }
                ListVM?.DoSearch();
                if (ListVM != null)
                {
                    foreach (var item in ListVM?.GetEntityList())
                    {
                        item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault();
                    }
                }
                MSD.AddModelError("", "数据已被使用,无法删除");
            }
            return(rv);
        }
Пример #6
0
        /// <summary>
        /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchDelete()
        {
            bool rv = true;
            //循环所有数据Id
            List <string> idsData   = Ids.ToList();
            var           modelType = typeof(TModel);
            var           pros      = modelType.GetAllProperties();
            //如果包含附件,则先删除附件
            List <Guid> fileids   = new List <Guid>();
            var         fa        = pros.Where(x => x.PropertyType == typeof(FileAttachment) || typeof(TopBasePoco).IsAssignableFrom(x.PropertyType)).ToList();
            var         isPersist = modelType.IsSubclassOf(typeof(PersistPoco));
            var         query     = DC.Set <TModel>().AsQueryable();
            var         fas       = pros.Where(x => typeof(IEnumerable <ISubFile>).IsAssignableFrom(x.PropertyType)).ToList();

            foreach (var f in fas)
            {
                query = query.Include(f.Name);
            }
            query = query.AsNoTracking().CheckIDs(idsData);
            var entityList = query.ToList();

            for (int i = 0; i < entityList.Count; i++)
            {
                string checkErro = null;
                //检查是否可以删除,如不能删除则直接跳过
                if (CheckIfCanDelete(idsData[i], out checkErro) == false)
                {
                    ErrorMessage.Add(idsData[i], checkErro);
                    rv = false;
                    break;
                }
                //进行删除
                try
                {
                    var Entity = entityList[i];
                    if (isPersist)
                    {
                        (Entity as PersistPoco).IsValid    = false;
                        (Entity as PersistPoco).UpdateTime = DateTime.Now;
                        (Entity as PersistPoco).UpdateBy   = LoginUserInfo.ITCode;
                        DC.UpdateProperty(Entity, "IsValid");
                        DC.UpdateProperty(Entity, "UpdateTime");
                        DC.UpdateProperty(Entity, "UpdateBy");
                    }
                    else
                    {
                        foreach (var f in fa)
                        {
                            if (f.PropertyType == typeof(FileAttachment))
                            {
                                string fidfield = DC.GetFKName2(modelType, f.Name);
                                var    fidpro   = pros.Where(x => x.Name == fidfield).FirstOrDefault();
                                var    idresult = fidpro.GetValue(Entity);
                                if (idresult != null)
                                {
                                    Guid fid = Guid.Empty;
                                    if (Guid.TryParse(idresult.ToString(), out fid) == true)
                                    {
                                        fileids.Add(fid);
                                    }
                                }
                            }
                            f.SetValue(Entity, null);
                        }

                        foreach (var f in fas)
                        {
                            var subs = f.GetValue(Entity) as IEnumerable <ISubFile>;
                            if (subs != null)
                            {
                                foreach (var sub in subs)
                                {
                                    fileids.Add(sub.FileId);
                                }
                                f.SetValue(Entity, null);
                            }
                            else
                            {
                            }
                        }
                        if (typeof(TModel) != typeof(FileAttachment))
                        {
                            foreach (var pro in pros)
                            {
                                if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco)))
                                {
                                    pro.SetValue(Entity, null);
                                }
                            }
                        }
                        DC.DeleteEntity(Entity);
                    }
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, idsData[i]);
                    rv = false;
                }
            }
            //进行数据库的删除操作
            if (rv == true)
            {
                try
                {
                    DC.SaveChanges();
                    var fp = KnifeVirgo.HttpContext.RequestServices.GetRequiredService <VirgoFileProvider>();
                    foreach (var item in fileids)
                    {
                        fp.DeleteFile(item.ToString(), DC.ReCreate());
                    }
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, null);
                    rv = false;
                }
            }
            //如果失败,添加错误信息
            if (rv == false)
            {
                if (ErrorMessage.Count > 0)
                {
                    foreach (var id in idsData)
                    {
                        if (!ErrorMessage.ContainsKey(id))
                        {
                            ErrorMessage.Add(id, Core.CoreProgram.Callerlocalizer?["Sys.Rollback"]);
                        }
                    }
                }
                ListVM?.DoSearch();
                if (ListVM != null)
                {
                    foreach (var item in ListVM?.GetEntityList())
                    {
                        item.BatchError = ErrorMessage.Where(x => x.Key == item.GetID().ToString()).Select(x => x.Value).FirstOrDefault();
                    }
                }
                MSD.AddModelError("", Core.CoreProgram.Callerlocalizer?["Sys.DataCannotDelete"]);
            }
            return(rv);
        }