//api
//数据库连接字符串
string connstr = "Data Source=.;Initial Catalog=Month6;Integrated Security=True";
APIFileHelp help = new APIFileHelp();
//上传文件
[HttpPost]
public FileResult UpLoad()
{
return help.UpLoad();
}
//分页导出
[HttpGet]
public void Export1(int index, int size = 2)
{
SqlConnection conn = new SqlConnection(connstr);
var list = conn.Query("select * from Goods").ToList();
list = list.OrderBy(x => x.GId).Skip((index - 1) * size).Take(size).ToList();
Dictionary dic = new Dictionary();
dic.Add("GId", "商品编号");
dic.Add("GName", "商品名称");
dic.Add("GColor", "商品颜色");
dic.Add("GSize", "商品尺码");
dic.Add("GPrice", "商品价格");
help.ExportExcel("a.xls", list, dic);
}
//显示商品
[HttpGet]
public PageDate GetGoods(int index, int size)
{
SqlConnection conn = new SqlConnection(connstr);
var list = conn.Query("select * from Goods").ToList();
PageDate page = new PageDate();
page.List = list.OrderBy(x => x.GId).Skip((index - 1) * size).Take(size).ToList();
var count = list.Count();
page.PageCount = count / size + (count % size == 0 ? 0 : 1);
return page;
}
商品图片商品名称商品颜色商品尺码商品价格操作
var index1 = 0; //获得当前页
var pagecount = 0;
function load(index) {
daoshu();
index1 = index;
$.ajax({
url: "http://localhost:51518/api/Shop/GetGoods2",
data: { index: index, size: 2 },
type: "get",
dataType: "json",
success:
function (d) {
$("#tb").empty();
$(d.List).each(function () {
$("#tb").append(
'
' +'
'
' + this.GName + '' +'
' + this.GColor + '' +'
' + this.GSize + '' +'
' + this.GPrice + '' +'
' +'
')
})
pagecount = d.PageCount;
}
})
}
load(1);
function first() {
index1 = 1;
daoshu();
load(index1);
}
function prev() {
index1--;
if (index1 == 0) {
index1 = 1;
}
daoshu();
load(index1);
}
function next() {
index1++;
if (index1 > pagecount) {
index1 = pagecount;
}
daoshu();
load(index1);
}
function last() {
daoshu();
load(pagecount);
}
//删除
function del(id) {
var obj = {
GId: id
};
$.ajax({
url: "http://localhost:51518/api/Shop/DeleteGood",
data: obj,
type: "post",
dataType: "json",
success:
function (d) {
if (d > 0) {
alert('删除成功!');
load();
}
else {
alert('删除失败!');
}
}
})
}
//修改
function upt(id) {
location.href = "/Default/Update";
document.cookie = id;
}
//导出
function daoshu() {
$("#a").prop("href", "http://localhost:51518/api/Shop/Export1?index=" + index1);
}