黑马程序员技术交流社区
标题:
一道前端笔试题
[打印本页]
作者:
wzg682735
时间:
2016-6-17 15:39
标题:
一道前端笔试题
如下图,请实现表格信息的排序功能,当点击表头的属性区域,将表格信息进行排序切换功能,即第一次点击为降序排序,再一次点击进行升序排序。
2.png
(6.2 KB, 下载次数: 10)
下载附件
表格
2016-6-17 15:36 上传
参考答案:
<table id="table" border="1">
<tr>
<th id="name">姓名</th>
<th id="power">力量</th>
<th id="agility">敏捷</th>
<th id="iq">智力</th>
</tr>
</table>
复制代码
var data = [{
name: '德鲁伊',
power: 17,
agility: 24,
iq: 13
}, {
name: '月之骑士',
power: 15,
agility: 22,
iq: 16
}, {
name: '众神之王',
power: 19,
agility: 15,
iq: 20
}, {
name: '流浪剑客',
power: 23,
agility: 15,
iq: 14
}];
! function() { //初始化渲染
var table = $('table');
for (var i = 0; i < data.length; i++) {
item = '<tr><td class="name">' + data[i].name + '</td><td class="power">' + data[i].power + '</td><td class="agility">' + data[i].agility + '</td><td class="iq">' + data[i].iq + '</td></tr>';
table.append(item);
}
}()
! function() {
var count_name = 0,count_power = 0,count_agility = 0,count_iq = 0;
$('#name').click(function() {
count_power = 0,count_agility = 0,count_iq = 0; //将其他排序列状态置零,也就是说当点了姓名进行排序之后,若再点其它排序,那么必然是从升序开始
if (count_name % 2 === 1) { //奇数升序
data = sort(data, 'name', 'unicode,ascend');
} else { //偶数降序
data = sort(data, 'name', 'unicode,descend');
}
count_name++;
//视图层渲染
var area = $("tr").slice(1);
for (var i = 0; i < data.length; i++) {
area.eq(i).find('.name').text(data[i].name);
area.eq(i).find('.power').text(data[i].power);
area.eq(i).find('.agility').text(data[i].agility);
area.eq(i).find('.iq').text(data[i].iq);
}
})
$('#power').click(function() {
count_name = 0,count_agility = 0,count_iq = 0;
if (count_power % 2 === 1) {
data = sort(data, 'power', 'number,ascend');
} else {
data = sort(data, 'power', 'number,descend');
}
count_power++;
var area = $("tr").slice(1);
for (var i = 0; i < data.length; i++) {
area.eq(i).find('.name').text(data[i].name);
area.eq(i).find('.power').text(data[i].power);
area.eq(i).find('.agility').text(data[i].agility);
area.eq(i).find('.iq').text(data[i].iq);
}
})
$('#agility').click(function() {
count_name = 0,count_power = 0,count_iq = 0;
if (count_agility % 2 === 1) {
data = sort(data, 'agility', 'number,ascend');
} else {
data = sort(data, 'agility', 'number,descend');
}
count_agility++;
var area = $("tr").slice(1);
for (var i = 0; i < data.length; i++) {
area.eq(i).find('.name').text(data[i].name);
area.eq(i).find('.power').text(data[i].power);
area.eq(i).find('.agility').text(data[i].agility);
area.eq(i).find('.iq').text(data[i].iq);
}
})
$('#iq').click(function() {
count_name = 0,count_power = 0,count_agility = 0;
if (count_iq % 2 === 1) {
data = sort(data, 'iq', 'number,ascend');
} else {
data = sort(data, 'iq', 'number,descend');
}
count_iq++;
var area = $("tr").slice(1);
for (var i = 0; i < data.length; i++) {
area.eq(i).find('.name').text(data[i].name);
area.eq(i).find('.power').text(data[i].power);
area.eq(i).find('.agility').text(data[i].agility);
area.eq(i).find('.iq').text(data[i].iq);
}
})
}()
//对数据进行排序
function sort(data, field, compareFunction) {
var handler = {
'unicode,ascend': function(pre, next) {
if (pre[field] > next[field])
return -1;
else
return 1;
},
'unicode,descend': function(pre, next) {
if (pre[field] > next[field])
return 1;
else
return -1;
},
'number,ascend': function(pre, next) {
return next[field] - pre[field];
},
'number,descend': function(pre, next) {
return pre[field] - next[field];
}
}
if (typeof compareFunction === 'string') {
data.sort(handler[compareFunction]);
} else if (typeof compareFunction === 'function')
data.sort(compareFunction);
else
data.sort();
return data;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2