MVC+EasyUI+三层新闻网站建立 分页查询数据功能(七)

2025-05-29 0 87

MVC新闻网站建立,完成分页查询数据功能。

1、在Model里面建立NewInfo(里面存放的是新闻信息的实体信息)

MVC+EasyUI+三层新闻网站建立 分页查询数据功能(七)

然后在DAL层中建立NewInfoDal (里面存放对新闻信息的操作)

写入分页查询的代码

?

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
/// <summary>

/// 分页查询

/// </summary>

/// <param name="start">分页开始条数</param>

/// <param name="end">分页结束条数</param>

/// <returns>返回查询到的list集合</returns>

public List<NewInfo> GetPageEntityList(int start,int end)

{

string sql = "select * from(select row_number()over(order by id)as num,*from T_News)as t where t.num>=@start and t.num<=@end";

SqlParameter[] pms = {

new SqlParameter("@start",SqlDbType.Int),

new SqlParameter("@end",SqlDbType.Int),

};

pms[0].Value = start;

pms[1].Value = end;

DataTable dt = SqlHelper.ExcuteDataTable(sql,CommandType.Text,pms);

List<NewInfo> newList = null;

if (dt.Rows.Count>0)

{

newList = new List<NewInfo>();

NewInfo newinfo = null;

foreach (DataRow item in dt.Rows)

{

newinfo = new NewInfo();

LoadEntity(item,newinfo);

newList.Add(newinfo);

}

}

return newList;

}

/// <summary>

/// 查询出页面条数

/// </summary>

/// <returns></returns>

public int GetRecordCount()

{

string sql = "select count(*) from T_News";

int count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql,CommandType.Text));

return count;

}

在BLL层中建立NewInfoServices(里面存放对新闻信息的逻辑处理)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
DAL.NewInfoDal NewInfoDal = new DAL.NewInfoDal();

/// <summary>

/// 分页查询数据

/// </summary>

/// <param name="pageIndex">当前页码值</param>

/// <param name="pageSize">一个多少条数据</param>

/// <returns></returns>

public List<NewInfo> GetPageEntityList(int pageIndex, int pageSize)

{

int start = (pageIndex - 1) * pageSize + 1;

int end = pageSize * pageIndex;

return NewInfoDal.GetPageEntityList(start,end);

}

/// <summary>

/// 查询出页面的记录数

/// </summary>

/// <returns></returns>

public int GetRecordCount()

{

return NewInfoDal.GetRecordCount();

}

我们把新闻管理的url指定为/NewInfo/Index

MVC+EasyUI+三层新闻网站建立 分页查询数据功能(七)

那么就要新建NewInfo控制器 Index视图就是新闻管理页面的主页了。

新闻管理主页的布局很简单就是一个表格,所以就先在body里面写了一表格

?

1

2

3

4

5
<body>

<div>

<table id="tt"></table>

</div>

</body/>

这里用到的是easyui的框架,所以先引用文件。

然后就是通过写js代码来显示出表格的行和列

?

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
<script type="text/javascript">

$(function () {

//初始化表格

initTable();

});

//初始化表格

function initTable() {

$("#tt").datagrid({

//指向一个地址,当表格加载完成后自动请求该地址

//自动向后台发送 rows 当前页多少条数据 page:当前页

//要求返回的数据json对象 {total:200,rows:[{},{}]}

url: '/NewInfo/ShowNewsList',

title: "新闻管理",

fitColumns: true,

height: $(window).height()-10,

idField: 'Id', //后台返回数据中的主键列。一定注意大小写。

loadMsg: "正在加载新闻信息........",

pagination: true, //启用分页

singleSelect: true, //只允许选中一行

pageSize: 10, //一页默认多少条

pageNumber: 1, //默认页

rownumbers: true,//行号

pageList: [10, 20, 30], //允许一页多少条数据

queryParams: {}, //异步请求可以额外传递的数据

columns: [[

{ field: 'ck', checkbox: true, align: 'left', width: 50 }, // 设置cheakbox

{ field: 'Title', title: '标题', width: 120 },

{ field: 'SubDateTime', title: '发布时间', width: 80, formatter: ChangeDateFormat, },

{ field: 'Author', title: '作者', width: 80 },

{

field: 'operate', title: '操作', align: 'center', width: $(this).width() * 0.1,

formatter: function (value, row, index) {

var str = "";

str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="detail" id="detail" class="easyui-linkbutton" onclick="showDetail('+row.Id+')"></a>';

str += '',

str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="update" id="update" class="easyui-linkbutton" onclick="updateNewInfo(' + row.Id + ')" ></a>';

str += '',

str += '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="delete" id="delete" class="easyui-linkbutton" onclick="deleteNewInfo(' + row.Id + ')" ></a>';

return str;

}

}

]],

onLoadSuccess: function (data) {

$("a[name='detail']").linkbutton({ text: '详情', plain: true, iconCls: 'icon-more' });

$("a[name='update']").linkbutton({ text: '编辑', plain: true, iconCls: 'icon-edit' });

$("a[name='delete']").linkbutton({ text: '删除', plain: true, iconCls: 'icon-cancel' });

////点击详情按钮

//clickDetail();

},

toolbar: [{

id: 'btnAdd',

text: '添加',

iconCls: 'icon-add',

handler: function () {

addBtnClick(); //添加新闻

}

}],

});

}

要完成数据的显示则还需要查询数据库。

根据 url: '/NewInfo/ShowNewsList', 所以需要在NewInfo控制器下建立ShowNewsList方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
/// <summary>

/// 分页展示数据

/// </summary>

/// <returns></returns>

public JsonResult ShowNewsList()

{

//要求返回的数据json对象 {total:200,rows:[{},{}]}

int pageSize = int.Parse(Request["rows"]??"10");

int pageIndex = int.Parse(Request["page"]??"1");

List<NewInfo> newInfoList= NewInfoBll.GetPageEntityList(pageIndex, pageSize);

//查询所有数据

var allNews = NewInfoBll.GetRecordCount();

//把totle和rows:[{},{}]一起返回

//先建立一个匿名类

var dataJson = new { total = allNews, rows = newInfoList };

var json = Json(dataJson, JsonRequestBehavior.AllowGet);

return json;

}

MVC+EasyUI+三层新闻网站建立 分页查询数据功能(七)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 MVC+EasyUI+三层新闻网站建立 分页查询数据功能(七) https://www.kuaiidc.com/99560.html

相关文章

猜你喜欢
发表评论
暂无评论