Home > Web Front-end > JS Tutorial > body text

jquery dynamically loading image data practice code_jquery

WBOY
Release: 2016-05-16 18:03:52
Original
1078 people have browsed it

I have been studying jquery these days, and I feel the power of this library, and I found a good book <<Sharp jquery>>
I just did it casually, the above is a photo List and two buttons. Click the small picture to display the large picture. When you click the button, you can view the pictures on the next page and the previous page.
Ideas:
1. First create a photo viewing page viewer.htm, with a simple layout, with a small picture and two buttons on the top, and a large picture on the bottom.
2. Create a general processing program viewServer.ashx to handle requests for photo viewing pages.
3. Then of course you need to use the database, including the path of the image, description and other information. Each small picture path should correspond to a large picture, which will be loaded when you click on the small picture. I did not make small pictures here, so I loaded them all with large pictures.
4. Use json for data transmission to create a function for loading images. When the page is loaded or the left and right buttons are clicked, the image is loaded through ajax and the start number and end number of the requested image are transferred to the background page.
After receiving the request information, the background page searches for the required image information in the database.
The effect is as shown:
jquery dynamically loading image data practice code_jquery
Implementation code:
viewer.htm

Copy codeThe code is as follows:




我的照片


















 




0张, 当前第0





viewserver.ashx:
复制代码 代码如下:

<%@ WebHandler Language="C#" Class="viewServer" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
public class viewServer : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string action = context.Request["action"].ToString();
if (action == "countPhoto") //统计共有多少图片
{
string sql = "select count(*) from T_SmallPhotos";
DataTable dt = sqlHelper.GetTable(sql);
int count = Convert.ToInt32(dt.Rows[0][0]);
context.Response.Write(count.ToString());
}
else if (action == "getData") //请求图片数据
{
string startId = context.Request["startId"].ToString();
string endId = context.Request["endId"].ToString();
//string sqlStr = string sqlStr = "SELECT * FROM (SELECT id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM T_SmallPhotos) t WHERE t .rownum >= 1 AND t .rownum <=5"
//这个查询语句有点小复杂,使用了开窗函数
string sqlStr = "SELECT * FROM (SELECT id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM T_SmallPhotos) t WHERE t .rownum >= @startId AND t .rownum <= @endId";
//string sqlStr = "SELECT [id], [imageName], [imageUrl], [imageAlt], [notes] FROM [T_SmallPhotos] where id>1 and id<10";
SqlParameter[] param = new SqlParameter[] {new SqlParameter("@startId",startId),
new SqlParameter("@endId",endId)};
DataTable dt = sqlHelper.GetTable(sqlStr, param);
List list = new List();
for (int i = 0; i < dt.Rows.Count; i )
{
list.Add(new Photo(Convert.ToInt32(dt.Rows[i][0]), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString(), Convert.ToInt32(dt.Rows[i][5])));
}
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();//将数据序列化为json数据
context.Response.Write(jss.Serialize(list));
}
}
public bool IsReusable
{
get
{
return false;
}
}
//封装一个照片类,然后使用json传递
public class Photo
{
public Photo(int i, string name, string url, string alt, string notes, int rownum)
{
id = i;
imageName = name;
imageUrl = url;
imageAlt = alt;
this.notes = notes;
this.rownum = rownum;
}
private int id; //图片编号
public int Id
{
get { return id; }
set { id = value; }
}
private string imageName;//图片名称
public string ImageName
{
get { return imageName; }
set { imageName = value; }
}
private string imageUrl; //图片路径
public string ImageUrl
{
get { return imageUrl; }
set { imageUrl = value; }
}
private string imageAlt; //图片描述
public string ImageAlt
{
get { return imageAlt; }
set { imageAlt = value; }
}
private string notes;
public string Notes
{
get { return notes; }
set { notes = value; }
}
private int rownum;
public int Rownum
{
get { return rownum; }
set { rownum = value; }
}
}
}

其中sqlHelper是我自定义的数据库访问类,比较简单就不贴出来了。
在实现过程中遇到一个ajax方面的问题,现在还是没搞太明白:
我的js代码中有两个请求函数,一个是获取图片总数getCountPhoto(),一个是加载图片的公共函数loadPhoto(startId,endId),我想在页面加载的时候同时调用这两个函数,分别显示出页码信息和具体图片列表,
复制代码 代码如下:

$(function(){
loadPhoto(1,9);
    getCountPhoto();
}

这样的话发现页面内容总是错误,经过调试发现原来两个ajax请求是交叉执行,并不是一个执行完成执行另一个的。
这就是前几天做的了。
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