Пример #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;

            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)")}