js 从一个json拼接成另一个json,并做json数据分页table展示
先给数据:
//原始json数据
json = [{"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"}, {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} {"id":"1","aid":"013","performanceperson":"668","customername":"\u6cb3\u5357\u4eba\uff0c\u4e0d\u77e5\u540d","customerphone":"13013013000","company":"\u6731\u5f69\u4e91","remarks":"\u9700\u8981","createtime":"2015-11-14 11:06:17"} ];
//预定义空 准备用于存放新拼接的json
var Datatable = ' ';
//新json数据的表头
var columns = [{"cid":"customername","ctext":"客户名字"},{"cid":"customerphone","ctext":"客户电话"},{"cid":"company","ctext":"客服/公司"},{"cid":"performanceperson","ctext":"销售"},{"cid":"createtime","ctext":"创建时间"},{"cid":"remarks","ctext":"备注"},{"cid":"edit","ctext":"操作"}];
主体ajax 获取数据---thinkphp 3.1.3
//获得全部客户信息
function getall()
{
var ajaxurl = '/app/index.php/Customerinfos/handler';
$.post(ajaxurl,{},function(data){
data=getnewjson(data);
$('#result').html(setcustomerinfoallsplitPage(json));
tableData = getnewjson(json);
//tableData = JSON.stringify(json); //json转换为字符串
splitPage(1,10);
},'json');
$("#wd").attr("disabled", 'true');
$("#phone").attr("disabled", 'true');
$("#company").attr("disabled", 'true');
}
setcustomerinfoallsplitPage(json)
function setcustomerinfoallsplitPage(json)
{
if(json == null || json == undefined || json == '')
{
return "返回值为空!";
}
var html = "<table class='imagetable' id='tb'>";
html+="<tr><th>客户名字</th><th>客户电话</th><th>客服/公司</th><th>销售</th><th>创建时间</th><th>备注</th><th>操作</th></tr>";
for(i=0;i<json.length;i++){
html+="<tr align='center'>";
html+="<td>"+json[i].customername+"</td><td>"+json[i].customerphone+"</td><td>"+json[i].company+"</td><td>"+json[i].performanceperson+"</td><td>"+json[i].createtime+"</td><td>"+json[i].remarks+"</td><td><nobr><input type='button' id='edit' value='编辑' onclick='javascript:edit_customerphone("+json[i].customerphone+");' /><input type='button' id='delete' value='删除' onclick='javascript:delete_customerphone("+json[i].customerphone+");' /></nobr></td>";
html+="</tr>";
}
html+="</table><hr style='position:absolute;width:1425px;heigth:30px;top:530px;'><div style='position:absolute;width:1425px;heigth:30px;top:550px;' id='page_bar' align='center'></div><hr style='position:absolute;width:1425px;heigth:30px;top:570px;'>";
return html;
}
拼接新的json
//截取json并拼接新的json
function getnewjson(json)
{
if(json == null || json == undefined || json == '')
{
return null;
}
var obj = [];
for(var i=0;i<json.length;i++) {
var customer = {};
customer.customername = json[i].customername;
customer.customerphone= json[i].customerphone;
customer.company = json[i].company;
customer.performanceperson = json[i].performanceperson;
customer.createtime = json[i].createtime;
customer.remarks = json[i].remarks;
customer.edit = "<nobr><input type='button' id='edit' value='编辑' onclick='javascript:edit_customerphone("+json[i].customerphone+");' /><input type='button' id='delete' value='删除' onclick='javascript:delete_customerphone("+json[i].customerphone+");'/></nobr>";
obj.push(customer);
}
return obj;
}
json 数据分页
/**
page:页码
pageSize:每页的记录条数
此方法除了传入page和pageSize之外,还应知道的有三个参数:
一、表的全部数据,json串格式,可通过action查询数据库得到。
二、表头所对应的列的key及名称,也是json串格式
三、表所对应的id
注:此处只是适合表头只有一行,且事先写好的情况。您可以根据需要改一下,逻辑思路就是这样,欢迎批评指正。
*/
function splitPage(page,pageSize)
{
/* if(Object.prototype.toString.call(tableData) === "[object String]"){ //判断是否为字符串
var json=JSON.parse(tableData); //字符串转化为json
}*/
var ptable = document.getElementById("tb"); //获取表格对象
var num = ptable.rows.length;//table.rows返回表格中包含的所有行,此处假设表由表头1行和表体N行组成
//alert(num);
//清除tbody
for(var i=num-1;i>0;i--)
{
ptable.deleteRow(i);
}
var totalNums = tableData.length;//总行数
var totalPage = Math.ceil(totalNums/pageSize);//总页数
var begin = (page-1)*pageSize;//页起始位置(包括)
var end = page*pageSize;//页结束位置(不包括)
end = end>totalNums?totalNums:end;
//向tbody中写入数据
var n = 1;//tbody的起始行
for(var i=begin;i<end;i++)
{
var row = ptable.insertRow(n++);
var rowData = tableData[i];
for(var j=0;j<columns.length;j++)
{
var col = columns[j].cid;
var cell = row.insertCell(j);
var cellData = rowData[col];
cell.innerHTML = cellData;
}
}
//生成分页工具条
var pageBar = "第"+page+"页/共"+totalPage+"页"+" ";
if(page>1)
{
pageBar += "<a href='javascript:splitPage("+1+","+pageSize+");'>首页</a> ";
}
else
{
pageBar += "首页 ";
} if(page>1)
{
pageBar += "<a href='javascript:splitPage("+(page-1)+","+pageSize+");'>上一页</a> ";
}
else
{
pageBar += "上一页 ";
}
if(page<totalPage)
{
pageBar += "<a href='javascript:splitPage("+(page+1)+","+pageSize+");'>下一页</a> ";
}
else
{
pageBar += "下一页 ";
}
if(page<totalPage)
{
pageBar += "<a href='javascript:splitPage("+(totalPage)+","+pageSize+");'>尾页</a> ";
}
else
{
pageBar += "尾页 ";
}
$('#page_bar').html(pageBar).show();
}
完工~~~~
js 从一个json拼接成另一个json,并做json数据分页table展示的更多相关文章
- vue 把后台返回的json拼接成excel并下载
先封装一下生成excel的方法 downfile.js export default { data() { return {} }, components: {}, created() { }, me ...
- 127. 126. Word Ladder *HARD* -- 单词每次变一个字母转换成另一个单词
127. Given two words (beginWord and endWord), and a dictionary's word list, find the length of short ...
- 关于SQL Server将一列的多行内容拼接成一行的问题讨论
http://blog.csdn.net/rolamao/article/details/7745972 昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我 ...
- SQL Server将一列的多行内容拼接成一行的问题讨论
转自http://blog.csdn.net/rolamao/article/details/7745972 昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出 ...
- SQL Server将一列的多行内容拼接成一行
昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我想将一个表的一个列的多行内容拼接成一行 比如表中有两列数据 : ep_classes ep_name A ...
- sqlserver中将查询结果拼接成字符串
#for xml path(param)--将查询结果以xml格式输出 select id,name from table1 for xml path --id和name为table1的真实字段 - ...
- SQL查询结果拼接成字符串
sqlserver中将查询结果拼接成字符串 #for xml path(param)--将查询结果以xml格式输出 1 select id,name from table1 for xml pat ...
- SpringMVC将通过ajax发送的 json数据封装成JavaBean
SpringMVC将通过ajax发送的 json数据封装成JavaBean 通过ajax发送的 json数据封装成JavaBean对发送时有如下要求: 1.发送的数据类型必须时UTF-8 2.发送的必 ...
- [原创]SQL 把表中某一个列按照逗号拼接成一行
在我们开发的过程中,难免遇到一种场景,把某个表中的的某个列的值拼接成用逗号隔开的一行数据 如图:我们把UserId列拼接成一行数据 -------> 为此我写了一个存储过程来解决此类问题. -- ...
随机推荐
- 什么是epoll
什么是epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new A ...
- Codeforces 713C Sonya and Problem Wihtout a Legend(单调DP)
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上 ...
- mongoose的用法(注:连接数据库)
第一步:连接数据库: mongoose.connect('mongodb://'+user+':'+pass+'@mongo.duapp.com:'+port+'/xzWIRHYlWLAApdsfAz ...
- 翻纸牌游戏(dfs回溯)
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- 启用Spring quartz定时器,导致tomcat服务器自动停止
在项目中添加了一个定时功能,基于Spring quartz: 设置好执行时间后(如:每天14:00) 当程序执行完后,就会出现以下信息: 2013-7-22 11:36:02 org.apache.c ...
- ASP.NET页面之间数据传递的几种方法
1)Request.QueryString 在ASP时代,这个是较常用的方法,到了ASP.NET,好像用的人不多了,但是不管怎么说,这是一个没有过时,且很值得推荐的方法,因为不管是ASP还是ASP ...
- PKU POJ 1006 Biorhythms (中国剩余定理)
中国剩余定理 x = ai (mod mi) ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk 其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st M ...
- ELK(ElasticSearch, Logstash, Log4j)系统日志搭建
1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...
- 系统变量写在.bash_profile和.bashrc的区别
今天配置一个代理,正儿八经的把我搞蒙了,不就是export http_porxy=xxx.xxx.xxx.xxx:xxxx 然后重启服务service network restart ,依然连接不了外 ...
- 【Cocos2D-x 3.5实战】坦克大战(2)游戏开始界面
关于游戏的素材都是在网上到处搜集到的,然后自己再用二流的ps技术修修改改的,所以有可能混在一起有点不搭调(没有办法啊,没有美工Orz.. 项目已经建立好了,然后我们需要把我们下载的素材放到Resour ...