2017-05-11 2 views
-2

は、このJSONを持っており、値が取る:アレイを並べ替えたり、正しく表示する方法は?

[[name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category]]

マイJS

Category 
Name 
Category 
Name 
Category 
Name 
Category 
Name 

$(function(){ 
$('input[name="oem"]').autoComplete({ 
    minChars: 4, 
    // parse json and output the values 11 
    source: function(term, response) { 
     term = term.toLowerCase(); 
     $.getJSON('/search.json?oem='+ term, function (data) { 
      var matches = []; 
      for (i = 0; i < data.length; i++) 
       if (~data[i][0].toLowerCase().indexOf(term)) matches.push(data[i]); 
      response(matches.slice(0,11)); 
     }); 
    }, 
    // how to display 
    renderItem: function (item, search){ 
     search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 
     var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi"); 
     return '<div class="autocomplete-suggestion" data-category="' + item[3] + '">' + '<font color="bbbbbb" style="font-style: italic">' + 'Категория, ' + '</font>'+ item[3].replace(re, "<b>$1</b>") + '</div><div class="autocomplete-suggestion" data-detail="' + item[0] + '" data-make="' + item[1] + '" data-oem="' + item[1] + '">' + item[0].replace(re, "<b>$1</b>") + '</div>'; 
     // here put first the category (item[3]), then all the other elements. ITEM is the elements of the array 
     }, 
    }); 
}); 

itemは、それが表示さ[name, brand, oem, category]

が含まれていますが

あなたが取得する必要があります:

Category 
Category 
Category 
Name 
Name 
Name 
Name 
Name 

sortを通してそれを実行しようとしました。しかし、私は配列と一緒に作業する必要があると思います。 appendとすることもできます。

オートコンプリートによる検索があります。これはplug-inを使用してください。

UPD:

renderItem: function (item, search){ 
      search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 
      var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi"); 
      var str = '<div class="autocomplete-suggestion" id="category" data-slug="' + item[4] + '" data-category="' + item[3] + '">' + '<font color="bbbbbb" style="font-style: italic">' + 'Категория, ' + '</font>'+ item[3].replace(re, "<b>$1</b>") + '</div>'; 
      $('#category').append('<div class="autocomplete-suggestion" data-detail="' + item[0] + '" data-make="' + item[1] + '" data-oem="' + item[2] + '">' + item[0].replace(re, "<b>$1</b>") + '</div>'); 
      return str; 
     } 

を追加しかし、まだ唯一のカテゴリを示して使用して

実装。教えてください、何が問題なのですか?

+3

多分あなたは、いくつかの実際のデータだけではなく構造を追加します。 –

+0

更新された質問 – dmitriy

+0

これは配列内の配列なので、各サブオブジェクトを新しい配列に追加してそれに応じて表示する必要があります。 – Sagar

答えて

0

これはいかがですか?

let arr = [ 
    { 
     name: "hello world", 
     brand: 3, 
     oem: "apple", 
     category: "zoo" 

    },{ 
     name: "qwert", 
     brand: 1, 
     oem: "food", 
     category: "atest" 
    },{ 
     name: "alice", 
     brand: 2, 
     oem: "orange", 
     category: "alicee" 

    }, 
] 

console.log(arr) 

let sorted = arr.sort(function(a,b){ 

    if (a.category < b.category) { 
     return -1 
    }else if (a.category > b.category){ 
     return 1 
    } 
    return 0 
}) 

console.log('--sorted--') 
console.log(sorted) 

結果:

[ { name: 'hello world', brand: 3, oem: 'apple', category: 'zoo' }, 
    { name: 'qwert', brand: 1, oem: 'food', category: 'atest' }, 
    { name: 'alice', brand: 2, oem: 'orange', category: 'alicee' } ] 
    --sorted-- 
    [ { name: 'alice', brand: 2, oem: 'orange', category: 'alicee' }, 
    { name: 'qwert', brand: 1, oem: 'food', category: 'atest' }, 
    { name: 'hello world', brand: 3, oem: 'apple', category: 'zoo' } ] 
+0

ありがとうございますが、私は質問を更新しました。私は 'append'を使います。教えてください、何が問題なのですか? – dmitriy

関連する問題