2012-07-09 1 views
0

나는 tokenintput 컨트롤을 여기 http://loopj.com/jquery-tokeninput/에서 찾았다. 꽤 인기가 있다고 나는 믿는다.데이터베이스에있는 값으로 tokenput을 미리 채우는 방법

필자는 입력란을 작성자 이름으로 멋지게 채우는 다음 코드를 사용하지만 사용자가 EDIT 세션에있을 때 데이터베이스에있는 값을 미리 채우고 싶습니다. 예를 들어 해당 저자를 찾을 수 있습니다. -이 필요하지만 분명히

$("#authorlist").tokenInput('/author/getauthors/', { 
     hintText: "Enter surname", 
     searchingText: "Searching...", 
     preventDuplicates: true, 
     allowCustomEntry: true, 
     highlightDuplicates: false, 
     tokenDelimiter: "*", 
     theme: "facebook" 
//  prePopulate: [{"id": 5016, "name": "Test 1" }] 
    }); 

이 이미 저자의 전체 목록은 (/ 저자/getauthors /) 도착 :

여기 enter image description here

코드입니다 : 기록은 이미 (이 같이 보입니다) 피 그 목록에서 다시 채워 넣습니다. 저자는 이미 그 기록을 발견했습니다. 그리고 그 점을 이해할 수없는 것 같습니다.

나는 당신이 (내가 주석 한) 자바 스크립트에서 미리 채울를 사용할 수있는 것을 볼 수 있으며 내 Edit.cshtml에서 발견 된 저자 값이

@foreach(var item in Model.AUTHORs.Select(model => new { model})) 
     { 
      <div type="hidden" id="authorHidden" > @item.model.FULL_NAME</div> 
     } 

그래서 그것은 단지 사건 즉, 그 값을 일종의 json 형식으로 저장하고 폼이로드되어 사용자에게 표시 될 때 준비되도록 tokeninput 컨트롤을 가져옵니다. Edit.cshtml에서 tokeninput 제어를 표시

다른 코드는 다음과 같습니다

<div class="editor-label">Authors</div> 
    <div class="authors"> 
     <div class="editor-field"> 
      <input type="text" id="authorlist" name="tiAuthors" /> 
     </div> 
    </div> 

어떤 도움이나 포인터가 많이 감사합니다. 다음

<input type="text" id="authorlist" name="tiAuthors" data-authors="@Json.Encode(Model.AUTHORs.Select(a => new { id = a.AuthorId, name = a.AuthorName })))" /> 

과 :

답변

5

당신은 당신이 미리 채워 싶은 저자의 목록을 넣어 뷰 내부에 입력상의 HTML5 data-* 속성을 사용할 수

$('#authorlist').tokenInput('/author/getauthors/', { 
    hintText: 'Enter surname', 
    searchingText: 'Searching...', 
    preventDuplicates: true, 
    allowCustomEntry: true, 
    highlightDuplicates: false, 
    tokenDelimiter: '*', 
    theme: 'facebook', 
    prePopulate: $('#authorlist').data('authors') 
}); 
+0

브릴리언트 - 너무 간단 그리고 우아한 - 지금 나는 데이터로 요소를 채우는 방법을 안다! 하느님은 내가 인터넷에서 이것에 관해보기 흉하지 않은 튜토리얼을 발견 할 수 없었던 이유를 알고있다 - 나에게 Darin를 도와 줘서 고맙다! – Vidar

+0

또한 data-pre ..를 사용할 수 있으며 tokeninput이 자동으로 채 웁니다. – FrEaKmAn

+0

데이터의 저자 ('작가')는 무엇입니까? 내 코드에서 사용할 올바른 키를 찾습니다. –