CI框架分页展示

这篇博客介绍了如何在CI框架中实现分页功能。首先,在applicationhelpers创建了一个名为func_helper.php的函数文件,接着在applicationconfigautoload.php配置文件中加载这个助手函数。最后,展示了在视图中应用分页后的效果。
摘要由CSDN通过智能技术生成

CI框架分页

先在\application\helpers 创建一个函数文件func_helper.php

/**
 * [pageinfo CI 分页函数]
 $url [分页链接地址]
 $total [总数]
 $limit [每页显示的数量]
 $segment[页码在地址的第几段]
 $theme ['分页主题类型']
 [strimg] [跳转或后退的字符串]

 */
function pageinfo($url,$total,$limit,$segment,$theme=1){
	$CI = &get_instance(); // 获取全局的CI对象,才能加载类
	
	$CI->load->library('pagination'); //加载分页类
	$config['base_url'] = $url; //分页链接
	$config['total_rows'] =$total;//总数
	$config['per_page'] = $limit;//每页显示多少条记录
	$config['use_page_numbers'] = TRUE; //让地址参数显示为页码
	$config['uri_segment'] = $segment; //页码所在的段数
	$config['first_link'] = '首页'; //首页链接
	$config['first_tag_open'] = '<li>';  //开始标签
	$config['first_tag_close'] = '</li>'; //结束标签
	$config['last_link'] = '最后一页';
	$config['last_tag_open'] = '<li>';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 分页展示Hdfs文件列表 前端代码: ```html <!-- 分页插件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <table class="table table-hover"> <thead> <tr> <th>#</th> <th>文件名</th> <th>大小</th> <th>修改时间</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${hdfsFiles}" var="hdfsFile" varStatus="status"> <tr> <td>${status.index+1}</td> <td><a href="#">${hdfsFile.name}</a></td> <td>${hdfsFile.size}</td> <td>${hdfsFile.modificationTime}</td> <td> <button class="btn btn-danger" onclick="deleteFile('${hdfsFile.path}')">删除</button> <button class="btn btn-success" onclick="downloadFile('${hdfsFile.path}')">下载</button> </td> </tr> </c:forEach> </tbody> </table> <div class="text-center"> <ul class="pagination"> <c:choose> <c:when test="${pageInfo.pageNum == 1}"> <li class="disabled"><a href="#">首</a></li> <li class="disabled"><a href="#">上一</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=1">首</a></li> <li><a href="?pageNum=${pageInfo.prePage}">上一</a></li> </c:otherwise> </c:choose> <c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i"> <c:choose> <c:when test="${i == pageInfo.pageNum}"> <li class="active"><a href="#">${i}</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=${i}">${i}</a></li> </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="${pageInfo.pageNum == pageInfo.pages}"> <li class="disabled"><a href="#">下一</a></li> <li class="disabled"><a href="#">尾</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=${pageInfo.nextPage}">下一</a></li> <li><a href="?pageNum=${pageInfo.pages}">尾</a></li> </c:otherwise> </c:choose> </ul> </div> ``` 后端代码: ```java @RequestMapping("/listFiles") public ModelAndView listFiles(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception { ModelAndView mv = new ModelAndView("fileList"); FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); RemoteIterator<LocatedFileStatus> fileStatusListIterator = fs.listFiles(new Path("/"), true); List<HdfsFile> hdfsFiles = new ArrayList<>(); while (fileStatusListIterator.hasNext()) { LocatedFileStatus fileStatus = fileStatusListIterator.next(); HdfsFile hdfsFile = new HdfsFile(); hdfsFile.setName(fileStatus.getPath().getName()); hdfsFile.setPath(fileStatus.getPath().toString()); hdfsFile.setSize(fileStatus.getLen()); hdfsFile.setModificationTime(fileStatus.getModificationTime()); hdfsFiles.add(hdfsFile); } PageHelper.startPage(pageNum, pageSize); PageInfo<HdfsFile> pageInfo = new PageInfo<>(hdfsFiles); mv.addObject("hdfsFiles", pageInfo.getList()); mv.addObject("pageInfo", pageInfo); return mv; } ``` 2. 上传文件 前端代码: ```html <form action="${pageContext.request.contextPath}/uploadFile" method="post" enctype="multipart/form-data"> <div class="form-group"> <label>选择文件</label> <input type="file" name="file"> </div> <button type="submit" class="btn btn-primary">上传</button> </form> ``` 后端代码: ```java @RequestMapping("/uploadFile") public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path("/" + file.getOriginalFilename()); FSDataOutputStream outputStream = fs.create(dst); IOUtils.copyBytes(file.getInputStream(), outputStream, 4096, true); return "redirect:/listFiles"; } ``` 3. 删除文件 前端代码: ```html <button class="btn btn-danger" onclick="deleteFile('${hdfsFile.path}')">删除</button> ``` 后端代码: ```java @RequestMapping("/deleteFile") public String deleteFile(String path) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path(path); fs.delete(dst, true); return "redirect:/listFiles"; } ``` 4. 下载文件 前端代码: ```html <button class="btn btn-success" onclick="downloadFile('${hdfsFile.path}')">下载</button> ``` 后端代码: ```java @RequestMapping("/downloadFile") public ResponseEntity<byte[]> downloadFile(String path) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path(path); FSDataInputStream inputStream = fs.open(dst); byte[] bytes = IOUtils.toByteArray(inputStream); HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", dst.getName()); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值