championRank.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. $(function() {
  2. var rankTpl = __inline('../tpl/championRank.tmpl');
  3. var curList = [];
  4. var baseList = [];
  5. function getRank() {
  6. var url = "http://api.lolbox.duowan.com/api/v2/rank/championStat/?page_items=172";
  7. $.ajax({
  8. url: url,
  9. dataType: 'jsonp',
  10. success: function(resp) {
  11. var ret = resp.results;
  12. if(!ret || !ret.length) {
  13. renderEmpty();
  14. return;
  15. }
  16. curList = baseList = _.sortBy(ret, 'win_ratio').reverse();
  17. renderList(curList);
  18. $("#rank-desc").html(resp.rank_desc);
  19. }
  20. });
  21. }
  22. function renderList(data) {
  23. var dom = rankTpl({list: data});
  24. $("#rankListBody").html(dom);
  25. $(".table-body").niceScroll({cursorcolor:"#D9D9D9",cursorwidth:"8",cursoropacitymin:1,cursorborder:'none'});
  26. }
  27. function renderEmpty() {
  28. $('#rankListBody').html('<tr><td colspan="6" style="text-align:center;padding: 30px 0;">暂无数据,请稍后重试</td></tr>');
  29. }
  30. function initEvent() {
  31. //选择不同的位置
  32. $(".hero-rank").on("click", ".mod-tabs-trigger li", function(e) {
  33. if($(this).hasClass('selected')) {
  34. return;
  35. }
  36. $(this).addClass('selected').siblings().removeClass('selected');
  37. var type = $(this).attr("data-type");
  38. if(type == 'all') {
  39. curList = baseList;
  40. } else {
  41. curList = _.filter(baseList, function(item) {
  42. return item.position == type;
  43. });
  44. curList = _.sortBy(curList, 'win_ratio').reverse();
  45. }
  46. renderList(curList);
  47. });
  48. //排序
  49. $(".hero-rank").on("click", "thead .sort", function() {
  50. var ratio = $(this).attr("data-ratio");
  51. if($(this).hasClass('sort--down')) {
  52. $(this).attr('class', 'sort sort--up');
  53. curList = _.sortBy(curList, ratio);
  54. } else {
  55. $(this).attr('class', 'sort sort--down');
  56. curList = _.sortBy(curList, ratio).reverse();
  57. }
  58. renderList(curList);
  59. });
  60. //搜索
  61. $("#searchKey").on("blur", function() {
  62. var input = $(this).val();
  63. filterList(input);
  64. }).on("input", function() {
  65. var input = $(this).val();
  66. filterList(input);
  67. });
  68. }
  69. function filterList(value) {
  70. var searchList = [];
  71. searchList = _.filter(curList, function(item) {
  72. var cp = item.champion;
  73. if(cp.display_name && cp.display_name.match(value)) {
  74. return item;
  75. } else if(cp.name && cp.name.match(value)) {
  76. return item;
  77. } else if(cp.title && cp.title.match(value)) {
  78. return item;
  79. }
  80. });
  81. renderList(searchList);
  82. }
  83. function init() {
  84. getRank();
  85. initEvent();
  86. }
  87. init();
  88. })