2012-07-09 102 views
0

我正在使用此處找到的tokeninput控件http://loopj.com/jquery-tokeninput/ - 我相信它很流行。如何使用數據庫中的值預先填充tokenput

我有下面的代碼,很好地填充作者姓名的輸入框 - 但我想預先填充控件與數據庫中找到的值,當用戶在編輯會話,即找到作者已被發現的記錄已經(看起來是這樣):

enter image description here

下面的代碼:

$("#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 /) - 但它需要p從那個列表中重新填寫,作者已經發現了這個記錄 - 這是我無法想象的。

我可以看到,你可以在JavaScript中使用預填充(我曾留言出來),我有我的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> 

任何幫助或指針讚賞。

回答

5

你可以在你的輸入使用HTML5 data-*屬性視圖內把您要預先填充作者列表:

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

然後:

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

輝煌 - 這麼簡單和優雅 - 現在我知道如何用數據填充元素!上帝知道爲什麼我無法在互聯網上找到一個體面的教程 - 感謝幫助達林! – Vidar 2012-07-10 10:53:18

+0

你也可以使用data-pre ..並且tokeninput會自動填充它。 – FrEaKmAn 2012-11-11 19:55:56

+0

數據(作者)的作者是什麼?我試圖找到在我的代碼中使用的正確密鑰。 – 2018-02-07 21:15:47

相關問題