• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

angularjs循环对象属性实现动态列的思路详解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

angularjs循环对象属性实现动态列

优点:保存对象,在数据库只保存一条数据

缺点:添加对象属性需要修改表结构、代码,然后重新重新发布

实现思路

1)数据库创建表(对象)、创建字段(对象属性)

2)根据表(对象)、字段(对象属性)生成配置表

3)根据表(对象)、字段(对象属性)生成三层架构

4)demo代码如下

1.接口代码:

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 = "张三",
                    gender = "男",
                };
                List<Column> columns = new List<Column>();
                columns.Add(new Column { columnname = "no", displaynname="学号" });
                columns.Add(new Column { columnname = "name", displaynname = "姓名" });
                columns.Add(new Column { columnname = "gender", displaynname = "性别" });
                return Json(new { code = "1", msg = "", item = item, columns = columns });
            }
            else
            {
                School item = new School
                {
                    no = "S001",
                    name = "浙江大学",
                    address = "浙江",
                };
                List<Column> columns = new List<Column>();
                columns.Add(new Column { columnname = "no", displaynname = "编码" });
                columns.Add(new Column { columnname = "name", displaynname = "名称" });
                columns.Add(new Column { columnname = "address", displaynname = "地址" });
                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 = "保存成功!" });
        }
    }
    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前端代码

@{
    ViewData["Title"] = "Home Page";
}
 
<script type="text/javascript">
    var app = angular.module("my_app", []);
    app.controller('my_controller', function ($scope) {
        //保存
        $scope.saveItem = function () {
            var itemstring = JSON.stringify($scope.item)
            $.post('@Url.Action("SaveItem", "Home")', { objecttype: '@ViewBag.objecttype', itemstring: itemstring }, function (data) {
 
            });
        }
        //获取
        $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="保存" ng-click="saveItem();" />
</div>

到此这篇关于angularjs循环对象属性实现动态列的文章就介绍到这了,更多相关angularjs动态列内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
vue实现两列水平时间轴的示例代码发布时间:2022-02-05
下一篇:
js中hasOwnProperty的属性及实例用法详解发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap