Beispiel #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="));

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