2016-10-19 12 views
0

私は最後のバージョンでSelect2jsを使用しています。私は問題に直面している。私はAjaxデータソースを使用しており、ソースからデータを取得する必要があります。Select2js ajaxソースからデータを取得

// call ajax for products 
$('.product-select select').select2({ 
    ajax: { 
     url: baseUrl +'/orders/get_products', 
     processResults: function (data) { 
      return { 
      results: data.items 
      }; 
     }, 
     select: function (data) { 
      // do nothing ... 
      console.log(data); 
     } 
    } 
}); 

$('.product-select select').on('select2:select', function (evt) { 
    // try this but evt doesn't contain data 
    console.log(evt); 
}); 

私のデータソースがある:私はid_personフィールドIは、行を選択するたびに取得したい

[{id: 3, text: 'test', id_person: 4}, {id: 5, text: 'oooo', id_person: 5}] 

は、ここに私のコードです。

+0

'.on( 'SELECT2:選択して'?、機能(EVT、データ){/ * ... * /}' - '何data' – Tomalak

+0

それは' undefined'だ:( –

+0

ああ、OK。 [documentation](https://select2.github.io/examples.html#events)は、選択したデータが 'evt.data'の中にあることを示唆しています。そうでない場合は、jsFiddle/stack snippetを作り直してください問題が発生しました – Tomalak

答えて

0

あなたはこれを試しましたか?

$('.product-select select').select2({ 
    ajax: { 
     url: baseUrl +'/orders/get_products', 
     processResults: function (data) { 
      return { 
       results: $.map(data, function (item) { 
        return { 
         text: item.text, 
         id_person: item.id_person, 
         id: item.id 
        } 
       }) 
      }; 
     }, 
     select: function (data) { 
      // do nothing ... 
      console.log(data); 
     } 
    } 
}); 

$('.product-select select').on('select2:select', function (e) { 
    console.log(e.params.data.id_person); 
}); 
+0

id_personあなたのコードは? –

+0

Vincent、私は自分のコードを編集します。 –

関連する問題