我用的c#,已经对着官方的文档改,也百度查过了很多,除去排序的bug,还是做不出来。 在后台我已经将从数据库查出的数据拼接成json格式了,返回的数据看起来也对的上json格式。但是table显示无数据。
<script>
layui.use('table', function () {
var table = layui.table;
table.render({
method: 'post'
, elem: '#test'
, url: "table.aspx/AjaxMethod"
, contentType: 'application/json'
, cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
, width: 1400
, height: 600
, toolbar: 'default'//定义一个id 给后面table重载的时候使用
, page: true
, cols: [[
{ field: '用户名', width: 100, minWidth: 100, title: '用户名', align: 'center' }
, { field: '基本工资', width: 100, title: '基本工资', align: 'center' }
, { field: '岗位津贴', width: 100, title: '岗位津贴', sort: true, align: 'center' }
, { field: '技能津贴', width: 100, title: '技能津贴', align: 'center' }
, { field: '保密津贴', title: '保密津贴', width: 100, minWidth: 100, align: 'center'} //minWidth:局部定义当前单元格的最小宽度,layui 2.2.1 新增
, { field: '加班工资', width: 100, title: '加班工资', sort: true, align: 'center' }
, { field: '提成/绩效', title: '提成/绩效', sort: true, align: 'center', width: 100 }
, { field: '全勤奖', title: '全勤奖', align: 'center', width: 100 }
, { field: '专项奖励', width: 100, title: '专项奖励', sort: true, align: 'center' }
, { field: '住宿补贴', width: 100, title: '住宿补贴', sort: true, align: 'center' }
, { field: '交通补贴', width: 100, title: '交通补贴', sort: true, align: 'center' }
, { field: '其他补贴', width: 100, title: '其他补贴', sort: true, align: 'center' }
, { field: '应付工资', width: 100, title: '应付工资', sort: true, align: 'center' }
, { field: '社保费用', width: 100, title: '社保费用', sort: true, align: 'center' }
]], id: 'test',
parseData: function (res) {
return {
"code": 0, //解析接口状态
"msg": res.msg, //解析提示文本
"count": res.count,
"data": res.data//解析数据列表
}
}
})
});
</script>
这是后台写的方法
public static string AjaxMethod()
{
SqlConnection sqlconn = new SqlConnection("Server=(local);DataBase=bishe;Integrated Security=sspi");
sqlconn.Open();
string sql1 = "SELECT * FROM 工资表 where 用户名='" + "444" + "'";//
//用户名='" + HttpContext.Current.Session["user"] + "'"
SqlCommand sqlcom1 = new SqlCommand(sql1, sqlconn);
SqlDataReader datar;
datar = sqlcom1.ExecuteReader();
StringBuilder jsonString = new StringBuilder();
jsonString.Append("{");
jsonString.Append("'code':0,'msg':'','count':1000,'data':");
jsonString.Append("[");
while (datar.Read())
{
jsonString.Append("{");
for (int i = 0; i < datar.FieldCount; i++)
{
Type type = datar.GetFieldType(i);
string strKey = datar.GetName(i);
string strValue = datar[i].ToString();
jsonString.Append("\"" + strKey + "\":");
strValue = String.Format(strValue, type);
//datetime和int类型不能出现为空的情况,所以将其转换成字符串来进行处理。
//需要加""的
if (type == typeof(string) || type == typeof(DateTime) || type == typeof(int))
{
if (i <= datar.FieldCount - 1)
{
jsonString.Append("\"" + strValue + "\",");
}
else
{
jsonString.Append(strValue);
}
}
//不需要加""的
else
{
if (i <= datar.FieldCount - 1)
{
jsonString.Append("" + strValue + ",");
}
else
{
jsonString.Append(strValue);
}
}
}
jsonString.Append("},");
}
datar.Close();
//当读取到的数据为空,此时jsonString中只有一个字符"["
if (jsonString.Length == 1)
{
jsonString.Append("]}");
//jsonString.Append("]");
}
else//数据不为空
{
//所有数据读取完成,移除最后三个多余的",},"字符
jsonString.Remove(jsonString.Length - 3, 3);
jsonString.Append("}");
jsonString.Append("]");
jsonString.Append("}");
}
return jsonString.ToString();
}
返回的json字符串
table显示无数据
不能直接用,你要把返回值处理掉