当前位置:Gxlcms > 数据库问题 > CMDB项目CURD组件之基于jQuery扩展封装组件

CMDB项目CURD组件之基于jQuery扩展封装组件

时间:2021-07-01 10:21:17 帮助过:18人阅读

技术图片
from django.shortcuts import render,HttpResponse
from django.views import View
import json

class AssetView(View):

    def get(self,request,*args,**kwargs):
        # 数据库中获取数据
        return render(request,asset.html)

class AssetJsonView(View):
    def get(self,request,*args,**kwargs):
        # 数据库中获取数据
        table_config = [
            {
                q: None,
                title: "选项",
                display: True,
                text: {content: "<input type=‘checkbox‘ />","kwargs": {}},
                attrs: {}
            },
            {
                q: id,
                title: ID,
                display: False,
                text:{},
                attrs: {}
            },
            {
                q: device_type_id,
                title: 资产类型,
                display: True,
                text: {content: "{n}", kwargs: {n: "@@device_type_choices"}},
                attrs: {}
            },
            {
                q: device_status_id,
                title: 状态,
                display: True,
                text: {content: "{n}", kwargs: {n: "@@device_status_choices"}},
                attrs: {name:device_status_id,origin:"@device_status_id",edit-enable: true, edit-type: select,"global-name": device_status_choices}
            },
            {
                q: idc__id,
                title: IDC,
                display: False,
                text: {},
                attrs: {}
            },
            {
                q: idc__name,
                title: IDC,
                display: True,
                text: {content: "{n}", kwargs: {n: "@idc__name"}},
                attrs: {name:idc_id,origin:"@idc__id",edit-enable: true, edit-type: select,"global-name": idc_choices}
            },
            {
                q: cabinet_order,
                title: 机柜位置,
                display: True,
                text: {content: "{n}",kwargs: {n: "@cabinet_order"}},
                attrs: {name:cabinet_order,origin:"@cabinet_order",edit-enable: true, edit-type: input}
            },
            {
                q: cabinet_num,
                title: 机柜号,
                display: True,
                text: {content: "{n}", kwargs: {n: "@cabinet_num"}},
                attrs: {},
            },
            {
                q: None,
                title: 操作,
                display: True,
                text: {content: "<a href=‘/assetdetail-{m}.html‘>{n}</a>", kwargs: {n: 查看详细,m: @id}},
                attrs: {},
            }
        ]

        q_list = []
        for i in table_config:
            if not i[q]:
                continue
            q_list.append(i[q])

        from repository import models
        # 分页组件用户获取数据
        data_list = models.Asset.objects.all().values(*q_list)
        data_list = list(data_list)

        result = {
            table_config:table_config,
            data_list:data_list,
            global_dict: {
                device_type_choices: models.Asset.device_type_choices,
                device_status_choices: models.Asset.device_status_choices,
                idc_choices: list(models.IDC.objects.values_list(id,name))
            },
            # 分页组件生成页码信息
            pager: """<li><a>1</a></li><li><a>2</a></li><li><a>3</a></li><li><a>4</a></li><li><a>5</a></li>"""

        }
        return HttpResponse(json.dumps(result))

    def put(self,request,*args,**kwargs):
        content = request.body
        v = json.loads(str(content,encoding=utf-8))
        print(v)
        ret = {
            status:True
        }
        return HttpResponse(json.dumps(ret))

class BuinessUnitView(View):

    def get(self,request,*args,**kwargs):
        # 数据库中获取数据
        return render(request,business_unit.html)

class BuinessUnitJsonView(View):
    def get(self,request,*args,**kwargs):
        # 数据库中获取数据
        table_config = [
            {
                q: None,
                title: "选项",
                display: True,
                text: {content: "<input type=‘checkbox‘ />","kwargs": {}},
                attrs: {}
            },
            {
                q: id,
                title: ID,
                display: False,
                text:{},
                attrs: {}
            },
            {
                q: name,
                title: 业务线名称,
                display: True,
                text: {content: "{n}", kwargs: {n: "@name"}},
                attrs: {edit-enable:true,edit-type:input,origin: @name}
            },
            {
                q: contact_id,
                title: 联系人组,
                display: False,
                text: {},
                attrs: {}
            },
            {
                q: contact__name,
                title: 联系人组,
                display: True,
                text: {content: "{n}", kwargs: {n: "@contact__name"}},
                attrs: {edit-enable:true,edit-type:select,origin: @contact_id,"global-name": contact_choices}
            },
            {
                q: manager__name,
                title: 管理员组,
                display: True,
                text: {content: "{n}", kwargs: {n: "@manager__name"}},
                attrs: {}
            },
            {
                q: None,
                title: 操作,
                display: True,
                text: {content: "<a href=‘/assetdetail-{m}.html‘>{n}</a>", kwargs: {n: 查看详细,m: @id}},
                attrs: {},
            }
        ]

        q_list = []
        for i in table_config:
            if not i[q]:
                continue
            q_list.append(i[q])

        from repository import models

        data_list = models.BusinessUnit.objects.all().values(*q_list)
        data_list = list(data_list)

        result = {
            table_config:table_config,
            data_list:data_list,
            global_dict: {
                # ‘device_type_choices‘: models.Asset.device_type_choices,
                # ‘device_status_choices‘: models.Asset.device_status_choices,
                # ‘idc_choices‘: list(models.IDC.objects.values_list(‘id‘,‘name‘))
                contact_choices:list(models.UserGroup.objects.values_list(id,name))
            },
            pager: """<li><a>1</a></li><li><a>2</a></li><li><a>3</a></li><li><a>4</a></li><li><a>5</a></li>"""

        }
        return HttpResponse(json.dumps(result))

    def put(self,request,*args,**kwargs):
        content = request.body
        v = json.loads(str(content,encoding=utf-8))
        print(v)
        ret = {
            status:True
        }
        return HttpResponse(json.dumps(ret))
views 技术图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" />
    <style>

    </style>
</head>
<body>
{#    <select id="i1">#}
{#        <option>上阿海</option>#}
{#        <option>北京</option>#}
{#        <option>广州</option>#}
{#    </select>#}

    <div style="width: 800px;margin: 0 auto;">
        <h1>资产列表</h1>
        <div class="btn-group" role="group" aria-label="...">
          <button id="idCheckAll" type="button" class="btn btn-default">全选</button>
          <button id="idReverseAll" type="button" class="btn btn-default">反选</button>
          <button id="idCancelAll" type="button" class="btn btn-default">取消</button>
          <button id="idEditMode" type="button" class="btn btn-default">进入编辑模式</button>
          <button type="button" class="btn btn-default">批量删除</button>
          <button id="idSave" type="button" class="btn btn-default">保存</button>
          <a id="idAdd" href="/web/asset-add.html" class="btn btn-default">添加</a>
        </div>
        <table class="table table-bordered">
            <thead id="table_th"></thead>
            <tbody id="table_tb"></tbody>
        </table>
        <ul id="idPagination" class="pagination">

        </ul>
    </div>

    <script src="/static/js/jquery-3.1.1.js"></script>
    <script src="/static/js/nbList.js"></script>

    <script>
        $(function () {
            $.NB("/web/asset-json.html");
        });


    </script>
    

</body>
</html>
asset.html 技术图片
(function () {
        var requestUrl = null;
        function bindChangePager() {
            $(‘#idPagination‘).on(‘click‘,‘a‘,function () {
                var num = $(this).text();
                init(num);

            })
        }

        function bindSave() {
            $(‘#idSave‘).click(function () {
                var postList = [];
                //找到已经编辑过的tr,tr has-edit=‘true‘
                $(‘#table_tb‘).find(‘tr[has-edit="true"]‘).each(function () {
                    // $(this) => tr
                    var temp = {};
                    var id = $(this).attr(‘row-id‘);
                    temp[‘id‘] = id;
                    $(this).children(‘[edit-enable="true"]‘).each(function () {
                        // $(this) = > td
                        var name = $(this).attr(‘name‘);
                        var origin = $(this).attr(‘origin‘);
                        var newVal = $(this).attr(‘new-val‘);
                        if (origin != newVal){
                            temp[name] = newVal;
                        }
                    });
                    postList.push(temp);
                })

                $.ajax({
                    url:requestUrl,
                    type: ‘PUT‘,
                    data: {‘post_list‘: JSON.stringify(postList)},
                    dataType: ‘JSON‘,
                    success:function (arg) {
                        if(arg.status){
                            init(1);
                        }else{
                            alert(arg.error);
                        }
                    }
                })
            })
        }

        function bindReverseAll() {
            $(‘#idReverseAll‘).click(function () {
                $(‘#table_tb‘).find(‘:checkbox‘).each(function () {
                    // $(this) => checkbox
                    if($(‘#idEditMode‘).hasClass(‘btn-warning‘)) {
                        if($(this).prop(‘checked‘)){
                            $(this).prop(‘checked‘,false);
                            trOutEditMode($(this).parent().parent());
                        }else{
                            $(this).prop(‘checked‘,true);
                            trIntoEditMode($(this).parent().parent());
                        }
                    }else{
                        if($(this).prop(‘checked‘)){
                            $(this).prop(‘checked‘,false);
                        }else{
                            $(this).prop(‘checked‘,true);
                        }
                    }
                })
            })
        }

        function bindCancelAll() {
            $(‘#idCancelAll‘).click(function () {
                $(‘#table_tb‘).find(‘:checked‘).each(function () {
                    // $(this) => checkbox
                    if($(‘#idEditMode‘).hasClass(‘btn-warning‘)){
                        $(this).prop(‘checked‘,false);
                        // 退出编辑模式
                        trOutEditMode($(this).parent().parent());
                    }else{
                        $(this).prop(‘checked‘,false);
                    }
                });
            })
        }

        function bindCheckAll() {
            $(‘#idCheckAll‘).click(function () {
                $(‘#table_tb‘).find(‘:checkbox‘).each(function () {
                    // $(this)  = checkbox
                    if($(‘#idEditMode‘).hasClass(‘btn-warning‘)){
                        if($(this).prop(‘checked‘)){
                            // 当前行已经进入编辑模式了
                        }else{
                            // 进入编辑模式
                            var $currentTr = $(this).parent().parent();
                            trIntoEditMode($currentTr);
                            $(this).prop(‘checked‘,true);
                        }
                    }else{
                        $(this).prop(‘checked‘,true);
                    }
                })
            })
        }

        function bindEditMode() {
            $(‘#idEditMode‘).click(function () {
                var editing = $(this).hasClass(‘btn-warning‘);
                if(editing){
                    // 退出编辑模式
                    $(this).removeClass(‘btn-warning‘);
                    $(this).text(‘进入编辑模式‘);

                    $(‘#table_tb‘).find(‘:checked‘).each(function () {
                        var $currentTr = $(this).parent().parent();
                        trOutEditMode($currentTr);
                    })

                }else<                    

人气教程排行