ligerUI---ligerGrid分頁排序的使用(從後台擷取資料顯示),

來源:互聯網
上載者:User

ligerUI---ligerGrid分頁排序的使用(從後台擷取資料顯示),

寫在前面:

  最近項目的前框架構用的是ligerUI,裡面用到了ligerGrid表格,下面就來說說從後台擷取資料並在前台頁面進行完美展示。啊哈哈哈。。(天啦,坐我旁邊的麗姐貌似炒股 一個月可以搞幾十萬,厲害厲害厲害了。。。。)

 

  1.前台頁面代碼:

 

            roleGrid = $("#roleGrid").ligerGrid(                {                    columns: [                        {display: '角色編號', name: 'roleId', align: 'left', width: 100, minWidth: 60,},                        {display: '角色名稱', name: 'roleName', minWidth: 120},//hide:'true'                        {display: '角色描述', name: 'roleDes', minWidth: 120,},                        {display: '角色類型編號', name: 'roleTypeId', minWidth: 120,},                        {display: '角色類型', name: 'roleTypeName', minWidth: 120,},                        {display: '建立者', name: 'creator', minWidth: 120,},                        {display: '建立時間', name: 'cTime', minWidth: 120},                        {display: '更新者', name: 'updator', minWidth: 120},                        {display: '更新時間', name: 'uTime', minWidth: 120},                        {display: '系統編號', name: 'systemId', minWidth: 120,},                        {display: '系統名', name: 'systemName', minWidth: 120,},                    ],                    //這裡請求之後的資料不用解析 自動產生表格  注意欄位要相互對應                    url: '${baseURL}/listAllRole.action',                    //向後台提交的資料 根據systemId來進行排序                    sortName: 'systemId',                    //向後台提交的資料 排序方向為升序                    sortorder:'asc',                    //後台提交的資料 每頁顯示的記錄數                    pageSize: 10,                    //onSelectRow選擇行事件                    onSelectRow: function (rowdata, rowindex, rowobj) {                        //選中行事件。。。                    },                    checkbox: false,                    height: '100%',                    rownumbers: true                });

 

  2.後台action代碼:

@Controller("RoleAction")public class RoleAction extends ActionSupport {    //注入業務層介面    @Resource    private RoleService roleService;    public String roleId;    public String roleName;    public String roleDes;    public String creator;    public String updator;    public String systemId;    public String functionIds;    public String cTime;    public String roleTypeId;    //分頁排序  接收對應的欄位    //這些都是前台向後台提交的資料 需要自己擷取    public String page; //當前頁    public String pagesize; //每頁大小    public String sortname;  //排序欄位    public String sortorder; //升序還是降序    //對返回的json資料提供get方法    private JSONObject jsonObject;    public JSONObject getJsonObject() {        return jsonObject;    }    public String listAllRole(){        try{            //擷取所有的角色資料  分頁排序處理 將分頁排序的相關欄位傳到dao層進行處理 這裡不做多解釋            List<Role> roleList = roleService.getAllByPage(Role.class,Integer.parseInt(page),Integer.parseInt(pagesize),sortname,sortorder);            //擷取總記錄數            Integer totalCount = roleService.getCount(Role.class);            List<RoleVO> roleVOList = new ArrayList<>();            //這裡都是本項目中對資料的一些處理 可以不用細看  這裡不重要            for (int i = 0; i < roleList.size(); i++) {                RoleVO roleVO = new RoleVO();                roleVO.setRoleId(roleList.get(i).getRoleId());                roleVO.setRoleName(roleList.get(i).getRoleName());                roleVO.setRoleDes(roleList.get(i).getRoleDes());                roleVO.setCreator(roleList.get(i).getCreator());                roleVO.setUpdator(roleList.get(i).getUpdator());                roleVO.setSystemId(roleList.get(i).getSystem().getSystemId());                roleVO.setSystemName(roleList.get(i).getSystem().getSystemName());                roleVO.setcTime(roleList.get(i).getcTime());                roleVO.setuTime(roleList.get(i).getuTime());                roleVO.setRoleTypeId(roleList.get(i).getRoleType().getRoleTypeId());                roleVO.setRoleTypeName(roleList.get(i).getRoleType().getRoleTypeName());                roleVOList.add(roleVO);            }            //因為對已ligerGrid 需要的json格式的資料要有Rows ,Total兩個欄位 這裡可以用map集合來做處理            Map<String, Object> map = new HashMap<>();            //Rows裝的是記錄資料集合            map.put("Rows", roleVOList);            //Rows裝的是總的記錄數            map.put("Total", totalCount);            //對資料做json格式轉換的處理            JsonConfig jsonConfig = new JsonConfig();            jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());            jsonObject = JSONObject.fromObject(map, jsonConfig);            java.lang.System.out.println("jsonObject:" + jsonObject);            //{"Total":5,"Rows":[{"cTime":"2017-10-24 00:00:00","creator":123,"roleDes":"測試測試","roleId":33,"roleName":"測試1","roleTypeId":2,"roleTypeName":"技術類型","systemId":1,"systemName":"USM","uTime":"2017-10-24 00:00:00","updator":123}]}        }catch (Exception e){            e.printStackTrace();        }        return SUCCESS;    }}

  其實分頁排序與我們平時寫的分頁排序沒有太大的區別 唯一的就是現在用了前端架構,要將這些相關的欄位在後台進行接收 然後傳到dao層做處理

  千萬不要以為只要我後台action中的資料返回的格式是json格式,就可以在前台頁面中直接擷取到然後作顯示,那是不可能的。

  前台能接收背景json格式的資料需要做以下處理:

  1.首先你的後台返回的json格式的資料要與你前台頁面ligerGrid進行顯示的欄位內容相對應(ligerGrid的Rows與Total是在前台頁面進行資料展示需要的兩個欄位,故後台也對應傳這兩個欄位的json格式資料):

  

  2.配置struts2返回的結果集視圖 設定type=json(我這裡用的是struts2的架構 故以struts2架構舉例)

<action name="listAll*" class="{1}Action" method="listAll{1}">            <result type="json">                            <param name="root">jsonObject</param>            </result></action>    

  3.設定對應action所在的package繼承自json-default

 <package name="fums" namespace="/" extends="struts-default,json-default">

  4.在後台action中將要返回的資料提供get方法

  5.在struts.xml中設定返回資料的格式(見第2步中的代碼,即設定的<param>部分,這裡返回的json格式的資料的名字叫

jsonObject,所以對其進行資料來源過濾)


成功啦成功啦~~~~~~~~~~~~~~~~~~~~~~
然後也可以點擊上一頁下一頁進行分頁啦。。。。。就不再展示了。。。。。


  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.