LayUI paging, LayUI dynamic paging, LayUI laypage paging

LayUI paging, LayUI dynamic paging, LayUI laypage paging, LayUI  laypage refresh the current page

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright Sweet Potato Yao August 1, 2017

http://fanshuyao.iteye.com/

 

Effect picture:



 

 

1. Reference js dependencies

Mainly jquery-1.11.3.min.js and layui.all.js, json2.js is used for json object conversion

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/plugin/layui/lay/dest/layui.all.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/json2.js"></script>

 

Second, js paging method encapsulation (pagination using template laytpl)

1. Template rendering

/**
 * Rendering method of pagination template
 * @param templateId The id of the template that needs to be rendered for paging
 * @param resultContentId The container id of the content displayed on the page after the template is rendered
 * @param data The json object returned by the server
 */
function renderTemplate(templateId, resultContentId, data){
	layui.use(['form','laytpl'], function(){
		var laytpl = layui.laytpl;
		laytpl($("#"+templateId).html()).render(data, function(html){
			$("#"+resultContentId).html(html);
		});
	});
	layui.form().render();// render
};

 

2, layui.laypage paging package

/**
 * layui.laypage paging package
 * @param laypageDivId The id of the Div layer of the paging control
 * @param pageParams paging parameters
 * @param templateId The id of the template that needs to be rendered for paging
 * @param resultContentId The container id of the content displayed on the page after the template is rendered
 * @param url The url link address to request pagination from the server
 */
function renderPageData(laypageDivId, pageParams, templateId, resultContentId, url){
	if(isNull(pageParams)){
		pageParams = {
			pageIndex : 1,
			pageSize : 10
		}
	}
	$.ajax({
		url : url,//basePath + '/sysMenu/pageSysMenu',
		method : 'post',
		data : pageParams,//JSON.stringify(datasub)
		async : true,
		complete : function (XHR, TS){},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if("error"==textStatus){
				error("The server is not responding, please try again later");
			}else{
				error("Failed to operate, textStatus="+textStatus);
			}
		},
		success : function(data) {
			var jsonObj;
			if('object' == typeof data){
				jsonObj = data;
			}else{
				jsonObj = JSON.parse(data);
			}
			renderTemplate(templateId, resultContentId, jsonObj);
			
			//Reinitialize the paging plugin
			layui.use(['form','laypage'], function(){
				laypage = layui.laypage;
				laypage({
				    account: laypageDivId,
				    curr : jsonObj.pager.pageIndex,
				    pages : jsonObj.pager.totalPage,
				    skip : true,
				    jump: function(obj, first){//obj is an object type. All configuration information for paging is included. The first is a Boolean class that detects whether the page is initially loaded. Very useful to avoid infinite refreshes.
				    	pageParams.pageIndex = obj.curr;
				    	pageParams.pageSize = jsonObj.pager.pageSize;
				    	if(!first){
				    		renderPageData(laypageDivId, pageParams, templateId, resultContentId, url);
				    	}
			      	}
				});
			});
		}
	});
};

 

3. The method of refreshing the current paging, which can be omitted

/**
 * The paging plugin refreshes the data of the current page, there must be a jump OK button, because it refreshes according to the button click event
 */
function reloadCurrentPage(){
	$(".layui-laypage-btn").click();
};

 

3. Page code

1. Paging tables and paging controls

<!-- Pagination table-->
<div class="layui-form">
  <table class="layui-table">
    <thead>
      <tr>
        <th class="w20"><input type="checkbox" name="checkBoxAll" lay-skin="primary" lay-filter="allChoose"></th>
	 <th class="w200">License Name</th>
	 <th class="w200">License Code</th>
	 <th class="w200">Menu name</th>
	 <th>License Link</th>
	</tr>
     </thead>
      <tbody id="page_template_body_id">
    </tbody>
  </table>
</div>
<!-- Paging control div -->		
<div id="imovie-page-div"></div>

 

 

2. Paging template

<script id="page_template_id" type="text/html">
{{#  layui.each(d.list, function(index, item){ }}
<tr>
	<td><input type="checkbox" name="permissionId" lay-skin="primary" value="{{item.permissionId}}"></td>
	<td>{{item.permissionName || ''}}</td>
	<td>{{item.permissionCode || ''}}</td>
	<td>{{item.menuName || ''}}</td>
	<td>{{item.permissionUrl || ''}}</td>
</tr>
{{#  }); }}
</script>

 

 

3. Paging execution code:

Pagination parameters:

function getPageParams(){
    var pageParams = {
	pageIndex : 1,
	pageSize : 2
    };
    pageParams.permissionName = $("input[name='permissionName']").val();
    pageParams.permissionCode = $("input[name='permissionCode']").val();
    pageParams.menuName = $("input[name='menuName']").val();
    return pageParams;
};

 

Paging execution method:

function initPage(){
	renderPageData("imovie-page-div", getPageParams(), "page_template_id",
			"page_template_body_id", basePath + '/sysPermission/pageSysPermission');
};

 

Page load initializes pagination:

$(function(){
	initPage();
});

 

If you include the query of the above renderings, as follows:

Html page code

<div>
			<form class="layui-form layui-form-pane">
				<div class="layui-form-item">
				    <div class="layui-inline">
						<label class="layui-form-label">许可名称</label>
					    <div class="layui-input-inline">
					    	<input type="text" name="permissionName" 
					    		autocomplete="off" class="layui-input" placeholder="请输入许可名称" >
					    </div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">许可编码</label>
					    <div class="layui-input-inline">
					    	<input type="text" name="permissionCode" 
					    		autocomplete="off" placeholder="请输入许可编码" class="layui-input">
					    </div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">菜单名称</label>
					    <div class="layui-input-inline layui-input-inline-0">
					    	<input type="text" name="menuName" 
					    		autocomplete="off" placeholder="请选择菜单名称" class="layui-input">
				    		
					    </div>
					</div>
					<div class="layui-inline">
						<button id="btnSubmit" class="layui-btn" lay-submit="" lay-filter="formFilter">查询</button>
					</div>
			  	</div>
			</form>
		</div>

 

查询语句:

$(function(){
	initPage();
	
	layui.use(['form'], function(){
		var form = layui.form();
		//监听提交
		form.on('submit(formFilter)', function(data){
			initPage();
			return false;
		});
		  
		  
	});
});

 

四、懂 jquery 插件封装的大神可以将其封装成独立的分页插件,这样更加容易使用。我表示不太懂,^_^

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年8月1日

http://fanshuyao.iteye.com/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056646&siteId=291194637