ホームページ > Java > &#&チュートリアル > SpringMVC+Bootstrap+DataTables に基づいたテーブルサーバー側のページングとファジークエリの実装

SpringMVC+Bootstrap+DataTables に基づいたテーブルサーバー側のページングとファジークエリの実装

高洛峰
リリース: 2017-01-07 10:23:57
オリジナル
2467 人が閲覧しました

はじめに

SpringMVC+Bootstrap+DataTables に基づいて、データ テーブルのサーバー側ページング、ファジー クエリ (非 DataTables 検索)、および非同期ページ更新が実装されています。

注: sp:message タグは SpringMVC 国際化

効果

DataTable テーブル

キーワードクエリ

カスタムキーワードクエリ、非 DataTable 検索

SpringMVC+Bootstrap+DataTables に基づいたテーブルサーバー側のページングとファジークエリの実装

コード

HTML コード

クエリ条件コード

rrreええ

データテーブルコード

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<!-- 查询、添加、批量删除、导出、刷新 -->

<div class="row-fluid">

<div class="pull-right">

<div class="btn-group">

<button type="button" class="btn btn-primary btn-sm" id="btn-add">

<i class="fa fa-plus"></i> <sp:message code="sys.add"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-delAll">

<i class="fa fa-remove"></i> <sp:message code="sys.delAll"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-export">

<i class="fa fa-sign-in"></i> <sp:message code="sys.export"/>

</button>

<button type="button" class="btn btn-primary btn-sm" id="btn-re">

<i class="fa fa-refresh"></i> <sp:message code="sys.refresh"/>

</button>

</div>

</div>

<div class="row">

<form id="queryForm" action="<%=path%>/goodsType/list" method="post">

<div class="col-xs-2">

<input type="text" id="keyword" name="keyword" class="form-control input-sm"

placeholder="<sp:message code="sys.keyword"/>">

</div>

<button type="button" class="btn btn-primary btn-sm" id="btn-query">

<i class="fa fa-search"></i> <sp:message code="sys.query"/>

</button>

</form>

</div>

</div>

ログイン後にコピー

JSコード

DataTablesの初期化、サーバーデータリクエスト、クエリ条件のカプセル化

1

2

3

4

5

6

7

8

9

10

11

12

13

<table id="dataTable" class="table table-striped table-bordered table-hover table-condensed" align="center">

<thead>

<tr class="info">

<td><input type="checkbox" id="checkAll"></td>

<th><sp:message code="sys.no"/></th>

<th><sp:message code="goods.type.name.cn"/></th>

<th><sp:message code="goods.type.name.en"/></th>

<th><sp:message code="sys.create.time"/></th>

<th><sp:message code="sys.update.time"/></th>

<th><sp:message code="sys.oper"/></th>

</tr>

</thead>

</table>

ログイン後にコピー

Javaコード

コントローラ処理メソッド。テーブルデータを必要とするページのクエリを実行し、Jsonをフォーマットして返す役割を果たします。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

<!-- page script -->

<script>

$(function () {

//添加、修改异步提交地址

var url = "";

var tables = $("#dataTable").dataTable({

serverSide: true,//分页,取数据等等的都放到服务端去

processing: true,//载入数据的时候是否显示“载入中”

pageLength: 10, //首次加载的数据条数

ordering: false, //排序操作在服务端进行,所以可以关了。

pagingType: "full_numbers",

autoWidth: false,

stateSave: true,//保持翻页状态,和comTable.fnDraw(false);结合使用

searching: false,//禁用datatables搜索

ajax: {

type: "post",

url: "<%=path%>/goodsType/getData",

dataSrc: "data",

data: function (d) {

var param = {};

param.draw = d.draw;

param.start = d.start;

param.length = d.length;

var formData = $("#queryForm").serializeArray();//把form里面的数据序列化成数组

formData.forEach(function (e) {

param[e.name] = e.value;

});

return param;//自定义需要传递的参数。

},

},

columns: [//对应上面thead里面的序列

{"data": null,"width":"10px"},

{"data": null},

{"data": &#39;typeNameCn&#39; },

{"data": &#39;typeNameEn&#39; },

{"data": &#39;createTime&#39;,

"render":function(data,type,full,callback) {

return data.substr(0,19)

}

},

{"data": &#39;updateTime&#39;, defaultContent: "",

"render":function(data,type,full,callback) {

if(data != null && data != ""){

return data.substr(0,19)

}else{

return data;

}

}

},

{"data": null,"width":"60px"}

],

//操作按钮

columnDefs: [

{

targets: 0,

defaultContent: "<input type=&#39;checkbox&#39; name=&#39;checkList&#39;>"

},

{

targets: -1,

defaultContent: "<div class=&#39;btn-group&#39;>"+

"<button id=&#39;editRow&#39; class=&#39;btn btn-primary btn-sm&#39; type=&#39;button&#39;><i class=&#39;fa fa-edit&#39;></i></button>"+

"<button id=&#39;delRow&#39; class=&#39;btn btn-primary btn-sm&#39; type=&#39;button&#39;><i class=&#39;fa fa-trash-o&#39;></i></button>"+

"</div>"

}

],

language: {

lengthMenu: "",

processing: "<sp:message code=&#39;sys.load&#39;/>",

paginate: {

previous: "<",

next: ">",

first: "<<",

last: ">>"

},

zeroRecords: "<sp:message code=&#39;sys.nodata&#39;/>",

info: "<sp:message code=&#39;sys.pages&#39;/>",

infoEmpty: "",

infoFiltered: "",

sSearch: "<sp:message code=&#39;sys.keyword&#39;/>:",

},

//在每次table被draw完后回调函数

fnDrawCallback: function(){

var api = this.api();

//获取到本页开始的条数

   var startIndex= api.context[0]._iDisplayStart;

   api.column(1).nodes().each(function(cell, i) {

     cell.innerHTML = startIndex + i + 1;

   });

}

});

//查询按钮

$("#btn-query").on("click", function () {

tables.fnDraw();//查询后不需要保持分页状态,回首页

});

//添加

$("#btn-add").on("click", function () {

url = "<%=path%>/goodsType/add";

$("input[name=typeId]").val(0);

$("input[name=typeNameCn]").val("");

$("input[name=typeNameEn]").val("");

$("#editModal").modal("show");

});

//批量删除

$("#btn-delAll").on("click", function () {

});

//导出

$("#btn-export").on("click", function () {

});

//刷新

$("#btn-re").on("click", function () {

tables.fnDraw(false);//刷新保持分页状态

});

//checkbox全选

$("#checkAll").on("click", function () {

if ($(this).prop("checked") === true) {

$("input[name=&#39;checkList&#39;]").prop("checked", $(this).prop("checked"));

//$("#dataTable tbody tr").addClass(&#39;selected&#39;);

$(this).hasClass(&#39;selected&#39;)

} else {

$("input[name=&#39;checkList&#39;]").prop("checked", false);

$("#dataTable tbody tr").removeClass(&#39;selected&#39;);

}

});

//修改

$("#dataTable tbody").on("click", "#editRow", function () {

var data = tables.api().row($(this).parents("tr")).data();

$("input[name=typeId]").val(data.typeIdStr);

$("input[name=typeNameCn]").val(data.typeNameCn);

$("input[name=typeNameEn]").val(data.typeNameEn);

url = "<%=path%>/goodsType/update";

$("#editModal").modal("show");

});

$("#btn-submit").on("click", function(){

$.ajax({

cache: false,

type: "POST",

url: url,

data:$("#editForm").serialize(),

async: false,

error: function(request) {

showFail("Server Connection Error...");

},

success: function(data) {

if(data.status == 1){

$("#editModal").modal("hide");

showSuccess("<sp:message code=&#39;sys.oper.success&#39;/>");

tables.fnDraw();

}else{

showFail("<sp:message code=&#39;sys.oper.fail&#39;/>");

}

}

});

});

//删除

$("#dataTable tbody").on("click", "#delRow", function () {

var data = tables.api().row($(this).parents("tr")).data();

if(confirm("是否确认删除这条信息?")){

$.ajax({

url:&#39;<%=path%>/goodsType/del/&#39;+data.typeIdStr,

type:&#39;delete&#39;,

dataType: "json",

cache: "false",

success:function(data){

if(data.status == 1){

showSuccess("<sp:message code=&#39;sys.oper.success&#39;/>");

tables.api().row().remove().draw(false);

}else{

showFail("<sp:message code=&#39;sys.oper.fail&#39;/>");

}

},

error:function(err){

showFail("Server Connection Error...");

}

});

}

});

});

</script>

ログイン後にコピー

DataTables

1

2

3

4

5

6

7

8

@RequestMapping(value="/goodsType/getData", produces = "text/json;charset=UTF-8")

@ResponseBody

public String getData(HttpServletRequest request, QueryCondition query) {

DatatablesView<GoodsType> dataTable = goodsTypeService.getGoodsTypeByCondition(query);

dataTable.setDraw(query.getDraw());

String data = JSON.toJSONString(dataTable);

return data;

}

ログイン後にコピー

Service業務処理クラスで要求される形式に従って定義されたJsonデータ形式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

{

"data": [{

"createTime": "2016-10-27 09:42:28.0",

"typeId": 96824775296417986,

"typeIdStr": "96824775296417986",

"typeNameCn": "食品",

"typeNameEn": "Foods",

"updateTime": "2016-10-28 13:00:24.0"

},

{

"createTime": "2016-10-27 09:42:27.0",

"typeId": 96824775296417979,

"typeIdStr": "96824775296417979",

"typeNameCn": "汽车",

"typeNameEn": "Cars123",

"updateTime": "2016-10-27 09:51:24.0"

}],

"draw": 1,

"recordsFiltered": 17,

"recordsTotal": 17

}

ログイン後にコピー

DatatablesViewを返します。主にクエリ条件に基づいてレコード数をカウントし、現在のページデータリストをクエリします

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

public class DatatablesView<T> {

private List<T> data; //data 与datatales 加载的“dataSrc"对应

private int recordsTotal;

private int recordsFiltered;

private int draw;

public DatatablesView() {

}

public int getDraw() {

return draw;

}

public void setDraw(int draw) {

this.draw = draw;

}

public List<T> getData() {

return data;

}

public void setData(List<T> data) {

this.data = data;

}

public int getRecordsTotal() {

return recordsTotal;

}

public void setRecordsTotal(int recordsTotal) {

this.recordsTotal = recordsTotal;

this.recordsFiltered = recordsTotal;

}

public int getRecordsFiltered() {

return recordsFiltered;

}

public void setRecordsFiltered(int recordsFiltered) {

this.recordsFiltered = recordsFiltered;

}

}

ログイン後にコピー

Daoレイヤーは基本的なデータベース クエリ操作です。ここでは省略します...

終了

クエリ条件はキーワード ファジー クエリのみを使用します。ビジネス ニーズに応じて、他のクエリ条件を動的に追加でき、それに応じてバックグラウンドを処理する必要があります。

上記は、編集者によって紹介された SpringMVC+Bootstrap+DataTables に基づくテーブル サーバー ページングとファジー クエリの実装です。ご質問があれば、私にメッセージを残してください。編集者が対応します。時間内に返信してください!

SpringMVC+Bootstrap+DataTables に基づくテーブル サーバー側ページングとファジー クエリに関するその他の関連記事については、PHP 中国語 Web サイトに注目してください。


関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート