Angularjs loop object properties to achieve dynamic columns Advantages: Save the object and only save one piece of data in the database Disadvantages: Adding object attributes requires modifying the table structure, code, and then republishing Implementation ideas 1) Create database tables (objects) and fields (object attributes) 2) Generate a configuration table based on tables (objects) and fields (object attributes) 3) Generate a three-tier architecture based on tables (objects) and fields (object attributes) 4) The demo code is as follows 1. Interface code: using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using WebApplication1.Models; namespace WebApplication1.Controllers { public class HomeController : Controller { public IActionResult Index(string objecttype) { ViewBag.objecttype = objecttype; return View(); } [HttpPost] public JsonResult GetItem(string objecttype) { if (objecttype == "student") { Student item = new Student { no = "S001", name = "Zhang San", gender = "Male", }; List<Column> columns = new List<Column>(); columns.Add(new Column { columnname = "no", displaynname="student number" }); columns.Add(new Column { columnname = "name", displaynname = "name" }); columns.Add(new Column { columnname = "gender", displaynname = "Gender" }); return Json(new { code = "1", msg = "", item = item, columns = columns }); } else { School item = new School { no = "S001", name = "Zhejiang University", address = "Zhejiang", }; List<Column> columns = new List<Column>(); columns.Add(new Column { columnname = "no", displaynname = "encoding" }); columns.Add(new Column { columnname = "name", displaynname = "name" }); columns.Add(new Column { columnname = "address", displaynname = "address" }); return Json(new { code = "1", msg = "", item = item, columns = columns }); } } [HttpPost] public JsonResult SaveItem(string objecttype, string itemstring) { if (objecttype == "student") { Student item = JsonConvert.DeserializeObject<Student>(itemstring); } else { School item = JsonConvert.DeserializeObject<School>(itemstring); } return Json(new { ResultCode = "1", ResultMessage = "Saved successfully!" }); } } public class Student { public string no { get; set; } public string name { get; set; } public string gender { get; set; } } public class School { public string no { get; set; } public string name { get; set; } public string address { get; set; } } public class Column { public string columnname { get; set; } public string displaynname { get; set; } } } 2. AngularJS front-end code @{ ViewData["Title"] = "Home Page"; } <script type="text/javascript"> var app = angular.module("my_app", []); app.controller('my_controller', function ($scope) { //Save $scope.saveItem = function () { var itemstring = JSON.stringify($scope.item) $.post('@Url.Action("SaveItem", "Home")', { objecttype: '@ViewBag.objecttype', itemstring: itemstring }, function (data) { }); } //Get $scope.getItem = function () { $.post('@Url.Action("GetItem", "Home")', { objecttype: '@ViewBag.objecttype' }, function (result) { $scope.item = result.item; $scope.columns = result.columns; $scope.$apply(); }); } $scope.getItem(); }); </script> <div> <ul> <li ng-repeat="column in columns"> <span>{{column.displaynname}}</span> <input ng-if="item[column.columnname]&&item[column.columnname].length" ng-model="item[column.columnname]" /> </li> </ul> <input type="button" value="Save" ng-click="saveItem();" /> </div> This is the end of this article about how to implement dynamic columns by looping object properties in angularjs. For more relevant angularjs dynamic column content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: 14 techniques for high-performance websites
>>: Solve the problem of ifconfig being unavailable in docker
swarm three virtual machines 132,133,134 1. Initi...
Table of contents Preface Style Function Descript...
Preface After the project is migrated to .net cor...
In MySQL, you can use the SQL statement rename ta...
The database enables slow query logs Modify the c...
This article example shares the specific code of ...
1. Background Sysbench is a stress testing tool t...
Usage of alter command in mysql to edit table str...
Phenomenon: Run an image, for example, ubuntu14.0...
Application scenarios: One of the new requirement...
Table of contents 1. Use object to create an obje...
Table of contents What is Vuex? Five properties o...
This article records the installation tutorial of...
<br />I have compiled some domestic design w...
Table of contents 1. setState() Description 1.1 U...