欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-21 16:22:20
angularjs循环对象属性实现动态列优点:保存对象,在数据库只保存一条数据缺点:添加对象属性需要修改表结构、代码,然后重新重新发布实现思路1)数据库创建表(对象)、创建字段(对象属性)2)根据表(...

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动态列内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!