1. 程式人生 > >easyui列表查詢報錯:Cannot read property 'length' of undefined

easyui列表查詢報錯:Cannot read property 'length' of undefined

1.問題描述

    easyui中datagrid執行loadData方法出現如下異常:Cannot read property 'length' of undefined

2.一開始懷疑是js或者頁面的問題,然後從早上幹到下午,網上各種方法用盡了就是不行!

    最後發現規律了:

    使用mybatis從資料庫查詢返回的List不報錯,但是自己new的ArrayList總是報錯!

    後來發現原來mybatis返回的不是ArrayList!而是PageList!

3.解決問題

    PageList中有個引數Paginator包含了page(當前頁), limit(每頁顯示條數), totalCount(總記錄數),但是ArrayList就沒有這個引數了。

    PageList是ArrayList的子類!

    知道了原因,就好辦了!把你new的ArrayList改成PageList!程式碼如下:

    原始碼:  

  List<MsgInfo> list=new ArrayList<MsgInfo>();
    修改後程式碼:
  Paginator paePaginator=new Paginator(page, pageSize, totalCount);
	    List<MsgInfo> list=new PageList<MsgInfo>(paePaginator);
    然後return list;

    大功告成!

4.答題解惑

   可能你用瀏覽器檢視返回的json資料時看不到page(當前頁), limit(每頁顯示條數), totalCount(總記錄數)這些資訊,但是easyUI能解析到!

   原因可能是因為瀏覽器任務這些資訊類似於請求的頭資訊一樣直接過濾了所以你看不到,但是會返回給easyUI!

   手打半天,以惠後人!