1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| private void btnBuild_Click(object sender, EventArgs e) { var data = ReadDbTableHead();
this.textHtmlCode.Text = GetHtmlCode("PO", data); this.textTypeScriptCode.Text = GetTsCode("PO", data); }
private static string GetTsCode(string tableName, List<DbTableHeadEntity> data) { StringBuilder sb = new StringBuilder(); sb.AppendLine($"dataSet{tableName} = [];"); sb.AppendLine($"editCache{tableName} = [];"); sb.AppendLine($"nzTotal{tableName} = 0;"); sb.AppendLine($"{tableName}PageIndex = 1;"); sb.AppendLine($"{tableName}PageSize = 10;"); sb.AppendLine($"is{tableName}Loading = false;");
//分页事件 sb.AppendLine($"PageIndexChange{tableName}(): void"); sb.AppendLine("{"); sb.AppendLine("console.log('PageIndexChange');"); sb.AppendLine("}");
//开始编辑 sb.AppendLine($"startEdit{tableName}(Id: number): void"); sb.AppendLine("{"); sb.AppendLine($"this.editCache{tableName}[Id].edit = true;"); sb.AppendLine("}");
//保存数据 sb.AppendLine($"saveEdit{tableName}(Id: number): void"); sb.AppendLine("{"); sb.AppendLine("console.log(Id + 'saveEdit');"); sb.AppendLine($"this.editCache{tableName}[Id].edit = false;"); sb.AppendLine("}");
//取消编辑 sb.AppendLine($"cancelEdit{tableName}(Id: number): void"); sb.AppendLine("{"); sb.AppendLine($"this.editCache{tableName}[Id].edit = false;"); sb.AppendLine("}");
//删除数据 sb.AppendLine($"delete{tableName}(Id:number): void"); sb.AppendLine("{"); sb.AppendLine("console.log(Id + 'delete');"); sb.AppendLine($"this.{tableName}Service.delete(Id).subscribe(res => "); sb.AppendLine("{");
sb.AppendLine("this.nzMessageService.success('Delete Success!');"); sb.AppendLine($"this.get{tableName}List(null, null, null, null);"); sb.AppendLine($"this.editCache{tableName}[Id].edit = false;"); sb.AppendLine("})"); sb.AppendLine("}");
//查询数据事件 sb.AppendLine($"get{tableName}List(startTime,stopTime,pageSize=10,pageIndex=1) "); sb.AppendLine("{"); sb.AppendLine($"this.is{tableName}Loading = true;"); sb.AppendLine($"this.{tableName}Service.get{tableName}List(startTime,stopTime,pageSize,pageIndex).subscribe("); sb.AppendLine("(res: {}) => {"); sb.AppendLine($"this.dataSet{tableName} = res['POList'];"); sb.AppendLine($"this.nzTotal{tableName} = res['ResultListCount'];"); sb.AppendLine($"this.dataSet{tableName}.forEach(item => "); sb.AppendLine("{"); sb.AppendLine($"this.editCache{tableName}[item.ID] ="); sb.AppendLine("{"); sb.AppendLine("edit: false,"); sb.AppendLine("data: item"); sb.AppendLine("}"); sb.AppendLine("});"); sb.AppendLine($"this.is{tableName}Loading = false;"); sb.AppendLine("})"); sb.AppendLine("}");
//加载时调用方法 sb.AppendLine("ngOnInit() "); sb.AppendLine("{"); sb.AppendLine($"this.get{tableName}List(null, null, null, null);"); sb.AppendLine("}");
return sb.ToString(); }
public static string GetHtmlCode(string tableName, List<DbTableHeadEntity> tableHeads) { StringBuilder sb = new StringBuilder(); sb.AppendLine($"<nz-card nzTitle='{tableName}' [nzExtra]='addTemplate'>"); sb.AppendLine($"<nz-table #edit{tableName} nzBordered [nzData]='dataSet{tableName}' nzFrontPagination='false' [nzTotal]='nzTotal{tableName}' nzShowSizeChanger='true' [nzPageIndex]='{tableName}PageIndex' [nzPageSize]='{tableName}PageSize' (nzPageIndexChange)='PageIndexChange{tableName}()' (nzPageSizeChange)='PageIndexChange{tableName}()' [nzLoading]='is{tableName}Loading' [nzShowTotal]='totalTemplate'>");
//表头 sb.AppendLine("<thead>"); sb.AppendLine("<tr>"); foreach (var item in tableHeads) { sb.AppendLine("<th>"); sb.AppendLine($"{item.ColumnName}"); sb.AppendLine("</th>"); } sb.AppendLine("<th>Action</th>"); sb.AppendLine("</tr>"); sb.AppendLine("</thead>");
sb.AppendLine("<tbody>");
//数据列 sb.AppendLine($"<tr *ngFor='let data of edit{tableName}.data'>"); foreach (var item in tableHeads) { sb.AppendLine("<td>"); sb.AppendLine($"<ng-container *ngIf='!editCache{tableName}[data.ID].edit'>"); sb.AppendLine("{{data." + item.ColumnName + "}}"); sb.AppendLine("</ng-container>"); sb.AppendLine($"<ng-container *ngIf='editCache{tableName}[data.ID].edit'>");
sb.AppendLine(GetTypeInput(tableName, item.ColumnType, item.ColumnName));
sb.AppendLine("</ng-container>"); sb.AppendLine("</td>"); }
//操作 sb.AppendLine("<td>"); sb.AppendLine("<div class='editable-row-operations'>"); sb.AppendLine($"<ng-container *ngIf='!editCache{tableName}[data.ID].edit'>"); sb.AppendLine($"<a (click)='startEdit{tableName}(data.ID)'>Edit</a>"); sb.AppendLine("</ng-container>"); sb.AppendLine($"<ng-container *ngIf='editCache{tableName}[data.ID].edit'>"); sb.AppendLine($"<a (click)='saveEdit{tableName}(data.ID)'>Save</a>"); sb.AppendLine($"<nz-popconfirm [nzTitle]=\"'Sure to cancel?'\" (nzOnConfirm)='cancelEdit{tableName}(data.ID)'>"); sb.AppendLine("<a nz-popconfirm>Cancel</a>"); sb.AppendLine("</nz-popconfirm>"); sb.AppendLine("</ng-container>"); sb.AppendLine("<nz-divider nzType='vertical'></nz-divider>"); sb.AppendLine($"<a nz-popconfirm nzTitle='您确定要删除这条数据?' (nzOnConfirm)='delete{tableName}(data.ID)'>Delete</a>"); sb.AppendLine("</div>"); sb.AppendLine("</td>");
sb.AppendLine("</tr>");
sb.AppendLine("</tbody>"); sb.AppendLine("</nz-table>"); sb.AppendLine("</nz-card>");
//总条数 sb.AppendLine("<ng-template #totalTemplate>"); sb.AppendLine("共 {{nzTotal" + tableName + "}} 条"); sb.AppendLine("</ng-template>");
//添加数据 sb.AppendLine("<ng-template #addTemplate>"); sb.AppendLine("<a (click)='isVisible=(!isVisible)'>Add</a>"); sb.AppendLine("</ng-template>");
//添加数据表单 sb.AppendLine($"<nz-modal [(nzVisible)]='isVisible' nzTitle='Add {tableName} Info' (nzOnCancel)='isVisible=(!isVisible)' nzCancelText='Cencel' nzOkText='Add' (nzOnOk)='handleOk()'>"); sb.AppendLine("<p>表单数据</p>"); sb.AppendLine("</nz-modal>");
return sb.ToString(); }
private static string GetTypeInput(string tableName,string columnType, string columnName) { string result = $"<input type='text' nz-input [(ngModel)]='editCache{tableName}[data.ID].data.{columnName}'>"; if (columnType.IndexOf("Int") != -1) { result = $"<input type='number' nz-input [(ngModel)]='editCache{tableName}[data.ID].data.{columnName}'>"; } if (columnType.IndexOf("DateTime") != -1) { result = $"<nz-date-picker [(ngModel)]='editCache{tableName}[data.ID].data.{columnName}'></nz-date-picker>"; } return result; }
static List<DbTableHeadEntity> ReadDbTableHead() { List<DbTableHeadEntity> result = new List<DbTableHeadEntity>();
var model = new POEntity(); Type t = model.GetType(); PropertyInfo[] PropertyList = t.GetProperties(); int Id = 0; foreach (PropertyInfo item in PropertyList) { DbTableHeadEntity dbTableHeadEntity = new DbTableHeadEntity(); object value = item.GetValue(model, null); dbTableHeadEntity.Id = ++Id; dbTableHeadEntity.ColumnName = item.Name; if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { var columnType = item.PropertyType.GetGenericArguments()[0]; dbTableHeadEntity.ColumnType = columnType.Name; } else { dbTableHeadEntity.ColumnType = item.PropertyType.Name; } result.Add(dbTableHeadEntity); } return result; }
private void btnCopyHtml_Click(object sender, EventArgs e) { this.textHtmlCode.Focus(); KeyBoard.CtrlX(); }
private void btnCopyTypeScript_Click(object sender, EventArgs e) { this.textTypeScriptCode.Focus(); KeyBoard.CtrlX(); }
|