Home > Web Front-end > JS Tutorial > How to use JQuery.dataTables to implement the table plug-in to add the function of jumping to a specified page

How to use JQuery.dataTables to implement the table plug-in to add the function of jumping to a specified page

巴扎黑
Release: 2017-06-22 17:43:41
Original
1945 people have browsed it

This article mainly introduces the JQuery.dataTables table plug-in to add a solution to jump to the specified page. It is very good and has reference value. Friends in need can refer to it

1. Solution

1. Add a custom toolbar and embed the text box


 "dom": &#39;l<"toolbar">frtip&#39;, //自定义工具栏 
//设置工具栏内容 
//l - length changing input control 
//f - filtering input 
//t - The table! 
//i - Table information summary 
//p - pagination control 
//r - processing display element 
[javascript] view plain copy print?
$("p.toolbar").html(&#39; <b style="color:red">跳转第</b><input id="searchNumber"/><b style="color:red;">页</b>&#39;);
Copy after login

2. Listen to the change event of the text box, and execute the plug-in’s transfer page method


//调转到指定页面索引 ,注意大小写 
var oTable = $(&#39;#example1&#39;).dataTable(); 
oTable.fnPageChange(page);
Copy after login

3. The plug-in is drawn successfully Finally, bind the value of the text box


//绘制的时候触发,绑定文本框的值 
table.on(&#39;draw.dt&#39;, function (e, settings, data) { 
  var info = table.page.info(); 
  //此处的page为0开始计算 
  console.info(&#39;Showing page: &#39; + info.page + &#39; of &#39; + info.pages); 
 
  $(&#39;#searchNumber&#39;).val(info.page + 1); 
});
Copy after login

2. Complete sample code


<table id="example1" class="table table-hover table-striped"> 
  <thead> 
    <tr> 
      <th>编号</th> 
      <th>姓名</th> 
      <th>性别</th> 
      <th>生日</th> 
      <th>班级</th> 
    </tr> 
  </thead> 
</table> 
$(function () { 
  //注意方法名为DataTable 
  var table = $(&#39;#example1&#39;).DataTable({ 
    "dom": &#39;l<"toolbar">frtip&#39;, //自定义工具栏 
    "pagingType": "full_numbers", 
    lengthMenu: [3, 5, 10], 
    processing: true,//是否使用进度条 
    serverSide: true,//是否启用数据库加载 
    ajax: { 
      url: &#39;/tableone/getlist&#39;, 
      type: &#39;post&#39;, 
      data: function (d) { 
        d.name = &#39;张三&#39;; 
        /* 
        * 自定义aja参数 
        * 特别说明,此处可以重写控件的默认参数,比如分页参数 
        */ 
        // d.start = 0; 
        //console.info(d); 
        //var page = $(&#39;#searchNumber&#39;).val(); 
        //page = parseInt(page) || 1; 
        //d.start = (page - 1) * d.length; 
      } 
    }, 
    //指定列绑定的字段 
    columns: [ 
      { data: &#39;sno&#39; }, 
      { data: &#39;sname&#39; }, 
      { data: &#39;ssex&#39; }, 
      { data: &#39;sbirthday&#39; }, 
      { data: &#39;class&#39; } 
    ], 
    order: [ 
      [3, &#39;desc&#39;] 
    ] 
  }); 
  $("p.toolbar").html(&#39; <b style="color:red">跳转第</b><input id="searchNumber"/><b style="color:red;">页</b>&#39;); 
  //绑定分页事件----在切换分页的时候触发 
  //table.on(&#39;page.dt&#39;, function () { 
  //  var info = table.page.info(); 
  //  console.info(&#39;Showing page: &#39; + info.page + &#39; of &#39; + info.pages); 
  //}); 
  //绘制的时候触发,绑定文本框的值 
  table.on(&#39;draw.dt&#39;, function (e, settings, data) { 
    var info = table.page.info(); 
    //此处的page为0开始计算 
    console.info(&#39;Showing page: &#39; + info.page + &#39; of &#39; + info.pages); 
    $(&#39;#searchNumber&#39;).val(info.page + 1); 
  }); 
  //监听文本框更改 
  $(&#39;#searchNumber&#39;).change(function () { 
    var page = $(this).val(); 
    page = parseInt(page) || 1; 
    page = page - 1; 
    //调转到指定页面索引 ,注意大小写 
    var oTable = $(&#39;#example1&#39;).dataTable(); 
    oTable.fnPageChange(page); 
  }); 
});
Copy after login

is displayed as follows:

The above is the detailed content of How to use JQuery.dataTables to implement the table plug-in to add the function of jumping to a specified page. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template