iuap-design/blog

使用moy快速开发后台管理系统(三)

HuYuee opened this issue · 0 comments

moy是什么?

moy 是基于模型框架 keroUI 框架 neoui 实现的应用框架,是前端集成解决方案,为企业级应用开发而生。
github地址:https://github.com/iuap-design/tinper-moy

step 3. 实现grid表格数据展示和分页

  1. 添加grid表格ui:进入pages/cardtable/cardtable.html中加入grid表格相关代码到相应位置:

    <div class="list-panel" id="manager">
        <h2>系统管理</h2>
      	
       <!-- grid表格相关代码 begin -->
        <div class="main-container cartable-container">
            <div class="hr-table">
                <div u-meta='{"id":"gridcardtable","type":"grid","data":"dt1","columnWidth":"150px","multiSelect":true,"contentSelect":false,"contentFocus":false,"canSwap":false}'>
                    <div options='{"field":"code","title":"系统编码","dataType":"String","editType":"string"}'></div>
                    <div options='{"field":"name","title":"系统名称","dataType":"String","editType":"string"}'></div>
                    <div options='{"field":"gateway","title":"网关地址","dataType":"String","editType":"string"}'></div>
                    <div options='{"field":"radiodatacontroller","title":"需要授权","type":"u-checkbox","data":"dt1","editOptions":{"datasource":"radiodatacontroller"},"renderType":"comboRender"}'></div>
                    <div options='{"field":"radiodataassociate","title":"用户自关联","type":"u-checkbox","data":"dt1","editOptions":{"datasource":"radiodataassociate"},"renderType":"comboRender"}'></div>
                    <div options='{"field":"combodatamodel","title":"认证模式","dataType":"String","editType":"string","renderType":"comboRender","editOptions":{"datasource":"combodatamodel"}}'></div>
                    <div options='{"field":"operate","dataType":"String","title":"操作","renderType":"optFun","editable":false,"autoExpand":true}'></div>
                </div>
            </div>
          	<!-- 分页部分 -->
            <div id='pagination' class='pagination u-pagination pagination-sm'></div>
        </div>
      <!-- grid表格相关代码 end -->
    </div>
  2. 在js中添加grid表格中每一列对应field的参数:进入pages/cardtable/cardtablemeta.js中,将下面代码直接拷贝到该文件中

    var metaCardTable={
    		meta: {
                //系统名称,和html中每一列的options参数中的field是一一对应
    		"name":{
    			type:'string',
    			required:true,
    			notipFlag: true,
    			hasSuccess: true,
    			nullMsg:'系统名称不能为空!'
    		},
    		"code":{
    			type:'string',
    			required:true,
    			notipFlag: true,
    			hasSuccess: true,
    			nullMsg:'系统编码不能为空!'
    		},
    		"radiodatacontroller":"",
    		"radiodataassociate":"",
    		"combodatamodel":"",
    		"gateway": "",
    		"operate": ""
    	}
    };
  3. 然后将上面的js文件引入到pages/cardtable/cardtablemeta.js中,并且在**init()**中加入以下代码来实现html和数据的绑定:

    **示例代码请点击展开**
    var listUrl = ctx + '/cardtable/list';
    var viewModel = {
                app: {},
                draw: 1,//当前页面index
                totlePage: 0,
                pageSize: 5,
                totleCount: 0,
      			//dt1对应的就是上面html中的u-meta的data中的dt1
                dt1: new u.DataTable(metaCardTable),
                radiodatacontroller: [{
                 value: 'Y',
                 name: '是'
                }, {
                 value: 'N',
                 name: '否'
                }],
                radiodataassociate: [{
                 value: 'Y',
                 name: '是'
               }, {
                 value: 'N',
                 name: '否'
               }],
               combodatamodel: [{
                 name: '信任',
                 value: 'Y'
               }, {
                 name: '不信任',
                 value: 'N'
               }],
                event: {
                    //清除datatable数据
                    clearDt: function(dt) {
                        dt.removeAllRows();
                        dt.clear();
                    },
                    // 卡片表数据读取
                    initCardTableList: function() {
                        var jsonData = {
                            pageIndex: viewModel.draw - 1,
                            pageSize: viewModel.pageSize,
                            sortField: "createtime",
                            sortDirection: "asc"
                        };
                        $.ajax({
                            type: 'get',
                            url: listUrl,
                            dataType: 'json',
                            data: jsonData,
                            contentType: 'application/json;charset=utf-8',
                            success: function(res) {
                                if (res) {
                                    if (res.success == 'success') {
                                        if (res.detailMsg.data) {
                                            if(jsonData.pageSize==5&&jsonData.pageIndex==1){
                                                res.detailMsg.data.content.splice(0,5);
                                            }else if(jsonData.pageSize==5){
                                                res.detailMsg.data.content.splice(5,3);
                                            }
                                            viewModel.totleCount = res.detailMsg.data.totalElements;
                                            viewModel.totlePage = res.detailMsg.data.totalPages;
                                          	//更新分页信息
                                            viewModel.event.comps.update({
                                                totalPages: viewModel.totlePage,
                                                pageSize: viewModel.pageSize,
                                                currentPage: viewModel.draw,
                                                totalCount: viewModel.totleCount
                                            });
                                            viewModel.dt1.removeAllRows();
                                            viewModel.dt1.clear();
                                            viewModel.dt1.setSimpleData(res.detailMsg.data.content, {
                                                unSelect: true
                                            });
                                        }
                                    } else {
                                        var msg = "";
                                        if (res.success == 'fail_global') {
                                            msg = res.message;
                                        } else {
                                            for (var key in res.detailMsg) {
                                                msg += res.detailMsg[key] + "<br/>";
                                            }
                                        }
                                        u.messageDialog({
                                            msg: msg,
                                            title: '请求错误',
                                            btnText: '确定'
                                        });
                                    }
                                } else {
                                    u.messageDialog({
                                        msg: '后台返回数据格式有误,请联系管理员',
                                        title: '数据错误',
                                        btnText: '确定'
                                    });
                                }
                            },
                            error: function(er) {
                                u.messageDialog({
                                    msg: '请求超时',
                                    title: '请求错误',
                                    btnText: '确定'
                                });
                            }
                        });
                    },
                    //分页相关
                    pageChange: function() {
                        viewModel.event.comps.on('pageChange', function(pageIndex) {
                            viewModel.draw = pageIndex + 1;
                            viewModel.event.initCardTableList();
                        });
                    },
                    sizeChange: function() {
                        viewModel.event.comps.on('sizeChange', function(arg) {
                            viewModel.pageSize = parseInt(arg);
                            viewModel.draw = 1;
                            viewModel.event.initCardTableList();
                        });
                    },
                    //页面初始化
                    pageInit: function() {
                        viewModel.app = u.createApp({
                            el: element,
                            model: viewModel
                        });
    					//找到分页的节点
                        var paginationDiv = $(element).find('#pagination')[0];
                      	//初始化分页页面,在获取到页面的具体的数据时候还会去更新分页信息
                        this.comps = new u.pagination({
                            el: paginationDiv,
                            jumppage: true
                        });
                        this.initCardTableList();
                        viewModel.event.pageChange();
                        viewModel.event.sizeChange();
                    }
                }
            }
            $(element).html(html);
            viewModel.event.pageInit();
  4. 然后刷新页面,看到表格出现了!

    在此代码中,主要最重要的就是u-meta中的json字符串其中的参数,我这里控制的是不能点击,表头不支持拖动更改列顺序,其他功能可以参见文档


到这里,我们已经完成了前面的准备工作,后面我会开始写如何去实现实现grid数据新增

请期待下一篇。

(完)