欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

bootstrap table服務(wù)端實(shí)現(xiàn)分頁(yè)效果

 更新時(shí)間:2017年08月10日 11:19:25   作者:starkfang  
這篇文章主要為大家詳細(xì)介紹了bootstrap table服務(wù)端實(shí)現(xiàn)分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

實(shí)現(xiàn)bootstrap table服務(wù)端實(shí)現(xiàn)分頁(yè)demo,具體內(nèi)容如下

首頁(yè)index.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Demo</title>
  <link rel="stylesheet" href="/assets/css/bootstrap.min.css" rel="external nofollow" />
  <link rel="stylesheet" href="/assets/css/bootstrap-table.min.css" rel="external nofollow" >
  <script src="/assets/js/jquery-2.1.1.min.js"></script>
  <script src="/assets/js/bootstrap.min.js"></script>
  <script src="/assets/js/bootstrap-table.min.js"></script>
  <script src="/assets/js/bootstrap-table-zh-CN.min.js"></script>
  <script src="/assets/js/index.js"></script>
</head>

<body>
  <!--查詢窗體-->
  <div class="widget-content">
    <form method="post" class="form-horizontal" id="eventqueryform">
      <input type="text" class="span2" name="seqNo" placeholder="編號(hào)"> 
      <input type="text" class="span3" name="name" placeholder="姓名"> 
      <input type="button" class="btn btn-default span1" id="eventquery" value="查詢">
    </form>
  </div>

  <div class="widget-content">
    <!--工具條-->
    <div id="toolbar">
      <button class="btn btn-success btn-xs" data-toggle="modal" data-target="#add">添加事件</button>
    </div>
    <table id="eventTable"></table>
  </div>
</body>

</html>


index.js

$(function() {
  // 初始化表格
  initTable();

  // 搜索按鈕觸發(fā)事件
  $("#eventquery").click(function() {
    $('#eventTable').bootstrapTable(('refresh')); // 很重要的一步,刷新url!
    // console.log("/program/area/findbyItem?offset="+0+"&"+$("#areaform").serialize())
    $('#eventqueryform input[name=\'name\']').val('')
    $('#eventqueryform input[name=\'seqNo\']').val('')
  });

});

// 表格初始化
function initTable() {
  $('#eventTable').bootstrapTable({
    method : 'post',  // 向服務(wù)器請(qǐng)求方式
    contentType : "application/x-www-form-urlencoded", // 如果是post必須定義
    url : '/bootstrap_table_demo/findbyitem',  // 請(qǐng)求url
    cache : false, // 是否使用緩存,默認(rèn)為true,所以一般情況下需要設(shè)置一下這個(gè)屬性(*)
    striped : true, // 隔行變色
    dataType : "json", // 數(shù)據(jù)類型
    pagination : true, // 是否啟用分頁(yè)
    showPaginationSwitch : false, // 是否顯示 數(shù)據(jù)條數(shù)選擇框
    pageSize : 10, // 每頁(yè)的記錄行數(shù)(*)
    pageNumber : 1,   // table初始化時(shí)顯示的頁(yè)數(shù)
    pageList: [5, 10, 15, 20], //可供選擇的每頁(yè)的行數(shù)(*)
    search : false, // 不顯示 搜索框
    sidePagination : 'server', // 服務(wù)端分頁(yè)
    classes : 'table table-bordered', // Class樣式
//   showRefresh : true, // 顯示刷新按鈕
    silent : true, // 必須設(shè)置刷新事件
    toolbar : '#toolbar',    // 工具欄ID
    toolbarAlign : 'right',   // 工具欄對(duì)齊方式
    queryParams : queryParams, // 請(qǐng)求參數(shù),這個(gè)關(guān)系到后續(xù)用到的異步刷新
    columns : [ {
      field : 'seqNo',
      title : '編號(hào)',
      align : 'center'
    }, {
      field : 'name',
      title : '姓名',
      align : 'center'
    }, {
      field : 'sex',
      title : '性別',
      align : 'center'
    }, {
      field : 'id',
      title : '操作',
      align : 'center',
      width : '280px',
      formatter : function(value, row, index) {
//        console.log(JSON.stringify(row));
      }
    } ],
  });
}

// 分頁(yè)查詢參數(shù),是以鍵值對(duì)的形式設(shè)置的
function queryParams(params) {
  return {
    name : $('#eventqueryform input[name=\'name\']').val(),  // 請(qǐng)求時(shí)向服務(wù)端傳遞的參數(shù)
    seqNo : $('#eventqueryform input[name=\'seqNo\']').val(),

    limit : params.limit, // 每頁(yè)顯示數(shù)量
    offset : params.offset, // SQL語(yǔ)句偏移量
  }
}

服務(wù)端 servlet

/**
 * Servlet實(shí)現(xiàn)類
 */
public class UserFindByItemSetvlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private IUserService service = new UserServiceImpl();

  public UserFindByItemSetvlet() {
    super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/json; charset=UTF-8");

    // 得到表單數(shù)據(jù)
    int offset = Integer.parseInt(request.getParameter("offset").trim());
    int limit = Integer.parseInt(request.getParameter("limit").trim());
    String seqNo = request.getParameter("seqNo").trim();
    String name = request.getParameter("name").trim();

    // 調(diào)用業(yè)務(wù)組件,得到結(jié)果
    PageBean<UserBean> pageBean;
    try {
      pageBean = service.findByItem(offset, limit, seqNo, name);
      ObjectMapper oMapper = new ObjectMapper();
      //對(duì)象轉(zhuǎn)換為json數(shù)據(jù) ,且返回
      oMapper.writeValue(response.getWriter(), pageBean);
    } catch (Exception e) {     
      e.printStackTrace();
    }

  }

}

分頁(yè)封裝類

/**
 * 分頁(yè)實(shí)體類
 */
public class PageBean<T> {
  /** 行實(shí)體類 */
  private List<T> rows = new ArrayList<T>();
  /** 總條數(shù) */
  private int total;

  public PageBean() {
    super();
  }

  public List<T> getRows() {
    return rows;
  }

  public void setRows(List<T> rows) {
    this.rows = rows;
  }

  public int getTotal() {
    return total;
  }

  public void setTotal(int total) {
    this.total = total;
  }

}

獲取用戶實(shí)現(xiàn)類

public class UserServiceImpl implements IUserService{

  /**
   * sql查詢語(yǔ)句
   */
  public PageBean<UserBean> findByItem(int offset, int limit, String seqNo, String name) {
    PageBean<UserBean> cutBean = new PageBean<UserBean>();

    // 基本SQL語(yǔ)句
    String sql = "SELECT * FROM c_userinfo where 1=1 ";

    // 動(dòng)態(tài)條件的SQL語(yǔ)句
    String itemSql = "";

    if (seqNo != null && seqNo.length() != 0) {
      itemSql += "and SeqNo like '%" + seqNo + "%' ";
    }

    if (name != null && name.length() != 0) {
      itemSql += "and Name like '%" + name + "%' ";
    }

    // 獲取sql連接
    Connection con = DButil.connect();
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {

      ps = con.prepareStatement(sql + itemSql + "limit ?,?");
      ps.setInt(1, offset);
      ps.setInt(2, limit);
      rs = ps.executeQuery();
      while (rs.next()) {
        UserBean bean = new UserBean();
        bean.setSeqNo(rs.getInt("seqNo"));
        bean.setName(rs.getString("name"));
        bean.setSex(rs.getInt("sex"));
        bean.setBirth(rs.getString("birth"));
        cutBean.getRows().add(bean);
      }
      // 得到總記錄數(shù),注意,也需要添加動(dòng)態(tài)條件
      ps = con.prepareStatement("SELECT count(*) as c FROM c_userinfo where 1=1 " + itemSql);
      rs = ps.executeQuery();
      if (rs.next()) {
        cutBean.setTotal(rs.getInt("c"));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      DButil.close(con);
      if (ps != null) {
        try {
          ps.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
    }
    return cutBean;
  }
}

數(shù)據(jù)庫(kù)工具類

/**
 * 數(shù)據(jù)庫(kù)工具類
 * 
 * @author way
 * 
 */
public class DButil {
  /**
   * 連接數(shù)據(jù)庫(kù)
   * 
   */
  public static Connection connect() {
    Properties pro = new Properties();
    String driver = null;
    String url = null;
    String username = null;
    String password = null;
    try {
      InputStream is = DButil.class.getClassLoader()
          .getResourceAsStream("DB.properties");
      pro.load(is);
      driver = pro.getProperty("driver");
      url = pro.getProperty("url");
      username = pro.getProperty("username");
      password = pro.getProperty("password");
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username,
          password);
      return conn;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 關(guān)閉數(shù)據(jù)庫(kù)
   * 
   * @param conn
   *     
   */
  public static void close(Connection con) {
    if (con != null) {
      try {
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

DB.properties文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/gov_social?useUnicode\=true&characterEncoding\=utf-8
username=root
password=root

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論