2016-10-19 2 views
0

나는 마지막 버전에서 Select2js를 사용합니다. 나는 문제에 직면 해있다. Ajax 데이터 소스를 사용하고 소스에서 데이터를 가져와야합니다.Select2js 아약스 소스에서 데이터 가져 오기

// 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

'CSTE 연구진 ('선택 2 : 선택'?, 기능 (EVT, 데이터) {/ * ... * /}'- 무엇은'data' – Tomalak

+0

그것은'undefined'의 :( –

+0

아, 괜찮아. [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

빈센트, 제 코드를 수정합니다. 지금 시도해 볼 수 있습니까? –