1. 程式人生 > >easyUI 之 Datagrid的使用和前臺分頁

easyUI 之 Datagrid的使用和前臺分頁

由於公司業務變更和任務安排,博主最近開始寫前端了,前端頁面!!!!雖然之前一直覺得前端很繁瑣,但是確實繁瑣啊,沒辦法,卯足勁乾乾幹。

公司前端用的easyui框架,博主之前沒咋弄過前端,不要說easyui了,所以蹭這次機會,來玩玩easyui。

接觸了一段時日後,博主真心覺得easyui很強大,各種功能外掛很齊全,使用起來也很方便。某些朋友說有點醜哈哈,確實不如現在主流的vue、angular以及bootstrap好看。還有一個是easyui文件少,使用起來費勁。好了,廢話不多說了,接下來開始總結一下最近easyui遇到的各種坑吧。

博主使用easyui的Datagrid顯示錶格資料,接下來一起來先看看datagrid吧。

<table id="dg" class="easyui-datagrid" title="FB League Serach" style="width:auto;height:auto"
			data-options="
				iconCls: 'icon-edit',  //載入圖示
				singleSelect: true,    //單行選中
				toolbar: '#tb',
				
				onClickRow: onClickRow,  //點選行編輯
				pagination:true,   //開啟分頁功能
				pagePosition:'bottom',
				rownumbers:true,
				pageSize:20,       
				pageList: [27, 28, 30],
				fitColumns:true,
				fit:true,  //固定分頁導航欄位置
				">
				 
				<%-- url: '<%=request.getContextPath()%>/football/listPheasantLeagueVerbose.do',
				method: 'get',  --%>
			<thead>
			<tr>
				<th data-options="field:'id',width:50">Item ID</th>
				<th data-options="field:'leagueNameCn',width:250,editor:'text'">LeagueNameCN</th>
				<th data-options="field:'leagueNameEn',width:250,editor:'text'">LeagueNameEN</th>
				<th data-options="field:'leagueNameBig5',width:250,editor:'text'">LeagueNameBig5</th>
				<th data-options="field:'leagueNameAbbr',width:250,editor:'text'">LeagueNameAbbr</th>
				<th data-options="field:'leagueNameOther',width:250,editor:'text'">LeagueNameOther</th>
				<th data-options="field:'sport',width:200,editor:'text'">Sport</th>
				<th data-options="field:'leagueLevel',width:200,editor:'numberbox'">LeagueLevel</th>
				
			</tr>
			</thead>
		</table>
//新增按鈕
<div id="tb" style="height:auto;">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()" style="width:200px;height:30px;">Append</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()" style="width:200px;height:30px;">Remove</a>
		<a href="javascript:appendRow()" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()" style="width:200px;height:30px;">Accept</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()" style="width:200px;height:30px;">Reject</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()" style="width:200px;height:30px;">GetChanges</a>
	</div>

在table初始化時,可以設定url和method,datagrid會呼叫內部封裝的ajax方法去load資料,但是資料是全量載入和顯示的,這時候不會真正的分頁,點選分頁只是遊標會變化,資料不會有顯示上的變化。

tabe的append remove accept和reject方法如下js實現:在對應的按鈕下實現ajax請求與後臺和資料庫互動。通過

<a href="javascript:appendRow()" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()" style="width:200px;height:30px;">Accept</a>
<script type="text/javascript">
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#dg').datagrid('validateRow', editIndex)){
				$('#dg').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickRow(index){
			if (editIndex != index){
				if (endEditing()){
					$('#dg').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					editIndex = index;
				} else {
					$('#dg').datagrid('selectRow', editIndex);
				}
			}
		}
		
		
		function append(){
			if (endEditing()){
				$('#dg').datagrid('appendRow',{status:'P'});
				editIndex = $('#dg').datagrid('getRows').length-1;
				$('#dg').datagrid('selectRow', editIndex)
						.datagrid('beginEdit', editIndex);
			}
		}
		
		//增加一行到資料庫
		function appendRow(){
			var rows = $('#dg').datagrid('getSelections');
			//var json = [];
			var loc;
			
				loc = {
					"id":rows[0].id,	
					"leagueNameCn":rows[0].leagueNameCn,
					"leagueNameEn":rows[0].leagueNameEn,
					"leagueNameBig5":rows[0].leagueNameBig5,
					"leagueNameAbbr":rows[0].leagueNameAbbr,
					"leagueNameOther":rows[0].leagueNameOther,
					"sport":rows[0].sport,
					"leagueLevel":rows[0].leagueLevel
				};
				//json.push(loc);
			
			json = JSON.stringify(loc);
			//alert(json);
			
			$.ajax({
				url:'<%=request.getContextPath()%>/football/savePheasantLeague.do',
				type:'post',
				async:true,
				dataType:'json',
				data:{"league":json},
				error:function(){alert("操作不成功");},
				success:function(response){
					if (response.value == 0) {
	                    alert("success");
	                    location.reload();
	                } else {
	                    alert("failed")
	                }
				}
			});
		}
		
		
		function removeit(){
			if (editIndex == undefined){return}
			$('#dg').datagrid('cancelEdit', editIndex)
					.datagrid('deleteRow', editIndex);
			editIndex = undefined;
		}
		function accept(){
			if (endEditing()){
				$('#dg').datagrid('acceptChanges');
			}
		}
		function reject(){
			$('#dg').datagrid('rejectChanges');
			editIndex = undefined;
		}
		function getChanges(){
			var rows = $('#dg').datagrid('getChanges');
			alert(rows.length+' rows are changed!');
		}
</script>

接下來,就開啟用datagrid的前端分頁方法。

//載入資料時用js做前臺分頁
		function pagerFilter(data){
            if (typeof data.length == 'number' && typeof data.splice == 'function'){    // 判斷資料是否是陣列
                data = {
                    total: data.length,
                    rows: data
                }
            }
            var dg = $(this);
            var opts = dg.datagrid('options');
            var pager = dg.datagrid('getPager');
            pager.pagination({
                onSelectPage:function(pageNum, pageSize){
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh',{
                        pageNumber:pageNum,
                        pageSize:pageSize
                    });
                    dg.datagrid('loadData',data);
                }
            });
            if (!data.originalRows){
                data.originalRows = (data.rows);
            }
            var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
            var end = start + parseInt(opts.pageSize);
            data.rows = (data.originalRows.slice(start, end));
            return data;
        }
 
         $(function(){//載入資料
            $('#dg').datagrid({loadFilter:pagerFilter}).datagrid({
            	url:'<%=request.getContextPath()%>/football/listPheasantLeagueVerbose.do',
            	method: 'get'
            	})
        }); 

後臺controller:返回json陣列

@RequestMapping(value = "/listPheasantLeagueVerbose.do", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<PheasantLeagueDTO> listPheasantLeagueVerbose() {
		
        return pheasantService.listPheasantLeagueVerbose();
    }

之前用ajax請求url載入資料,如下面的方法:

 $(function(){//載入資料
            $('#dg').datagrid({loadFilter:pagerFilter}).datagrid('loadData', getData());
        });

//ajax載入資料
		function getData(){
			$.ajax({
				url:'<%=request.getContextPath()%>/football/listPheasantLeagueVerbose.do',
				type:'get',
				async:true,
				dataType:'json',
				data:null,
				error:function(){alert("loading data failed.");},
				success:function(data){
					if (data != null) {
	                    alert("success");
	                    //location.reload();
	                    return data;
	                } else {
	                    alert("failed")
	                }
				}
			 	
			});

通過前端除錯發現在呼叫getData之後,還沒帶success方法之前就進入到pageFilter分頁方法了,導致分頁方法傳參data為null,判斷data是否為陣列時,data.length則報錯,不知為什麼,還請各位大佬指教。

相關推薦

easyUI Datagrid的使用前臺

由於公司業務變更和任務安排,博主最近開始寫前端了,前端頁面!!!!雖然之前一直覺得前端很繁瑣,但是確實繁瑣啊,沒辦法,卯足勁乾乾幹。 公司前端用的easyui框架,博主之前沒咋弄過前端,不要說easyui了,所以蹭這次機會,來玩玩easyui。 接觸了一段時日後,博主真心

Easyuidatagrid中有,如何設定重新整理按鈕

 var p = $('#manager').datagrid('getPager'); if (p){ $(p).pagination({ //設定分頁功能欄 //分頁功能可以通過Pagination的事件呼叫後臺分頁功能來實

EasyUIDataGrid

ets before table rst add function use userdata pat 第一步創建分頁DataGrid <table id="dg"> <thead> <tr> <th

easyui datagrid前臺

function pagerFilter(data){ if (typeof data.length == 'number' && typeof data.splice == 'function'){ // 判斷資料是否是陣列

easyui datagrid 前臺的實現

原文連結:https://www.cnblogs.com/wangyt223/p/4189414.html使用easyui分頁,有後臺伺服器端實現和前臺瀏覽器端實現。伺服器端實現按規定的格式返回資料就可以了,前臺實現需要寫js來實現。程式碼如下:關鍵程式碼:123456789

bootstrap-table前臺後臺對json格式的要求

total bsp 為什麽 ros 技術 ots cli 前端 microsoft Bootstrap是一款前端非常流行的框架,其中的表格更為大家經常使用。大家都知道表格的分頁分為前臺和後臺分頁,也就是表格配置中sidePagination屬性,當sidePaginatio

DRF頻率限制、、解析器渲染器

一、頻率限制 1、頻率限制是做什麼的 開放平臺的API介面呼叫需要限制其頻率,以節約伺服器資源和避免惡意的頻繁呼叫。   2、頻率元件原理 DRF中的頻率控制基本原理是基於訪問次數和時間的,當然我們可以通過自己定義的方法來實現。當我們請求進來,走到我們頻率元件的時候,DRF內部會有一個字典

Nginx學習路(六)NginX中的記憶體管理---Nginx中的記憶體對齊記憶體

Nginx由於極高的效能受到大家的追捧,而Nginx的高效能與它優秀的記憶體管理方式是分不開的,今天就來聊一聊Nginx中的記憶體對齊和記憶體分頁。先說下Nginx中的記憶體對齊,Nginx中的記憶體對齊機制是它高效能的關鍵因素之一,先說點基礎的東西,什麼是記憶體對齊呢? 記

jquery easyui datagrid使用,、排序、查詢

天涯孤岸軟體商城,專業的個人網站,.net電子商務商城原始碼,,以及其它原始碼, 歡迎訪問:tyga.tygaweb.com (新) $('#dg').datagrid({                 url: "xxx.ashx",                

JDBC學習路(八)可滾動結果集資料

如果在資料庫中有大量資料,然後要求從第150個數據開始讀取,讀10個數據,這種要求在論壇回覆和交流中常見 ,當然了,主流資料庫都提供了指定查詢集的方法,下面我們先來演示一下如果不使用Mysql的查詢集方法怎麼做,當 然,這個在實際開發中應該用處不大,但是作為一種想法還是需要

easyui datagrid 預設方式

EasyUI 的 datagrid 支援伺服器端分頁,但是官方的資料比較少,以下總結了兩種 datagrid 的伺服器端分頁機制,可根據情況具體使用。 一:使用 datagrid 預設機制 後臺: public JsonResult GetQuestionUnit()  {      // eas

datatable指南--前臺後臺

一、介紹 Datatables是一款jQuery表格外掛。它是一個高度靈活的工具,可以將任何HTML表格新增高階的互動功能。 分頁,即時搜尋和排序幾乎支援任何資料來源:DOM, javascript, Ajax 和 伺服器處理支援不同主題 DataTables, jQuery UI, Boot

BootStrap Table前臺後臺對JSON格式的要求

serve pad name spa paginati dep side border containe BootStrap Table前臺和後臺分頁對JSON格式的要求 前臺分頁: sidePagination: "client",對應的json格式必須為: [

Python路65-Django、自定義

python目錄一、XSS二、分頁1三、分頁2四、分頁3一、XSS有下面一段代碼,想將這段代碼中的字符串渲染到HTML頁面中,默認Django是不能這樣去操作的views.pydef user_list(request): page_str = """ <a href="/use

mysqloracle

order 同時 影響 排序 lec 必須 _id member from 一mysql 分頁 1.掃描出6000+10條數據 取出10條 數據量大書影響查詢速度select * from member order by member_id asc LIMIT 6000

Linq 使用skiptake

agen 復制 pos pan void int com nbsp 代碼      static int Main(string[] args) { //每頁條數 const int pageSize

Cookie、SessionDjango

{} 最大 開開 patch 裝飾器 由於 登錄驗證 mat cbv cookie Cookie的由來 大家都知道HTTP協議是無狀態的。 無狀態的意思是每次請求都是獨立的,它的執行情況和結果與前面的請求和之後的請求都無直接關系,它不會受前面的請求響應情況直接影響,也

EasyUIDataGrid屬性

method 格式化 eth 動態 name eas 屬性 clas tle 1、editor屬性 使用方法一:靜態數據 <th field="level" width="80" editor="{ type:‘combobox‘,

MiniUI前臺,假實現源碼

MiniUI 假分頁背景對於數據較少,無需後臺分頁的需求,可使用以下解決方案方案MiniUI提供了監聽事件,特別方便即可實現。源碼mini.parse(); var grid = mini.get("datagridTable"); // 獲取所有數據和總記錄數 { tot

物理邏輯

mysql數據庫 只需要 能夠 占用 lis list集合 實時性 空間 集合 一、概述 1、物理分頁 物理分頁依賴的是某一物理實體,這個物理實體就是數據庫,比如MySQL數據庫提供了limit關鍵字,程序員只需要編寫帶有limit關鍵字的SQL語句,數據庫返回的就是分頁結