Authored by xuqi

trigger input search

@@ -70,43 +70,46 @@ function searchResult() { @@ -70,43 +70,46 @@ function searchResult() {
70 i = 0, 70 i = 0,
71 html = ''; 71 html = '';
72 72
73 - // 遍历首字母搜索  
74 - $.each(brandsData, function(k, v) {  
75 - if ($.isArray(v)) { 73 + if (keyword !== '') {
  74 +
  75 + // 遍历首字母搜索
  76 + $.each(brandsData, function(k, v) {
  77 + if ($.isArray(v)) {
  78 +
  79 + // 遍历品牌,进行匹配
  80 + $.each(v, function(i, brand) {
  81 + if (brand.name.toLowerCase().indexOf(keyword) > -1) {
  82 + result[k] = result[k] || [];
  83 + result[k].push(brand);
  84 + }
  85 + });
  86 + }
  87 + });
76 88
77 - // 遍历品牌,进行匹配 89 + // 根据搜索结果生成 HTML
  90 + $.each(result, function(k, v) {
  91 + var brandHtml = ['<div class="brand-list bar-', i, '">'];
  92 +
  93 + i++;
  94 + brandHtml.push('<div class="title-bar"><h2>');
  95 + brandHtml.push(k);
  96 + brandHtml.push('</h2></div>');
78 $.each(v, function(i, brand) { 97 $.each(v, function(i, brand) {
79 - if (brand.name.toLowerCase().indexOf(keyword) > -1) {  
80 - result[k] = result[k] || [];  
81 - result[k].push(brand); 98 +
  99 + brandHtml.push('<p><a href="' + brand.url + '">' + brand.name);
  100 + if (brand.isNew) {
  101 + brandHtml.push('<i class="icon-hot">HOT</i>');
82 } 102 }
  103 + if (brand.isHot) {
  104 + brandHtml.push('<i class="icon-new">NEW</i>');
  105 + }
  106 + brandHtml.push('</a></p>');
83 }); 107 });
84 - }  
85 - }); 108 + brandHtml.push('</div>');
  109 + html += brandHtml.join('');
86 110
87 - // 根据搜索结果生成 HTML  
88 - $.each(result, function(k, v) {  
89 - var brandHtml = ['<div class="brand-list bar-', i, '">'];  
90 -  
91 - i++;  
92 - brandHtml.push('<div class="title-bar"><h2>');  
93 - brandHtml.push(k);  
94 - brandHtml.push('</h2></div>');  
95 - $.each(v, function(i, brand) {  
96 -  
97 - brandHtml.push('<p><a href="' + brand.url + '">' + brand.name);  
98 - if (brand.isNew) {  
99 - brandHtml.push('<i class="icon-hot">HOT</i>');  
100 - }  
101 - if (brand.isHot) {  
102 - brandHtml.push('<i class="icon-new">NEW</i>');  
103 - }  
104 - brandHtml.push('</a></p>');  
105 }); 111 });
106 - brandHtml.push('</div>');  
107 - html += brandHtml.join('');  
108 -  
109 - }); 112 + }
110 113
111 // 插入 dom,绑定事件 114 // 插入 dom,绑定事件
112 $('.search-result').html(html); 115 $('.search-result').html(html);
@@ -118,7 +121,6 @@ if ($('.brand-search-page').length) { @@ -118,7 +121,6 @@ if ($('.brand-search-page').length) {
118 121
119 $keyword.on('input', function() { 122 $keyword.on('input', function() {
120 if ($keyword.val().length) { 123 if ($keyword.val().length) {
121 - searchResult();  
122 $icon.css('color', '#000'); 124 $icon.css('color', '#000');
123 $(this).closest('.search-box').css('width', '11.25rem'); 125 $(this).closest('.search-box').css('width', '11.25rem');
124 $('.search-action').show(); 126 $('.search-action').show();
@@ -127,6 +129,7 @@ if ($('.brand-search-page').length) { @@ -127,6 +129,7 @@ if ($('.brand-search-page').length) {
127 $(this).closest('.search-box').css('width', '12.5rem'); 129 $(this).closest('.search-box').css('width', '12.5rem');
128 $('.search-action').hide(); 130 $('.search-action').hide();
129 } 131 }
  132 + searchResult();
130 }).focus(); 133 }).focus();
131 134
132 clearTextHammer = new Hammer($('.clear-text')[0]); 135 clearTextHammer = new Hammer($('.clear-text')[0]);