2017-10-12 89 views
0

我正在使用jQuery Typeahead plugin,但如果我嘗試設置初始數據,則會發生錯誤,並且輸入的值爲false錯誤 - 缺少字符串 - jQuery Typeahead

這裏是我的配置:

var typeaheadOptions = { 
     dynamic: true, 
     display: ['title'], 
     template: '<span>{{title}}</span>', 
     cache: true, 
     debug: true, 
     multiselect: { 
      matchOn: ['id'], 
      data: [{title: 'title1'}, {title: 'title2'}], 
      callback: { 
       onCancel: function(node, item) {removeItem(item)} 
      } 
     }, 
     source: { 
      posts: { 
       ajax: { 
        method: 'GET', 
        url: remoteURL, 
        data: {search: '{{query}}'} 
       } 
      } 
     }, 
     callback: { 
      onClick: function(node, a, item) {addItem(item)} 
     } 
    }; 

這裏是錯誤:

ERROR - Missing string": { 
    arguments: "", 
    function: "helper.namespace()", 
    message: "ERROR - Missing string", 
    node: "#media-kit-post" 
} 

這是什麼樣子:

Wrong item title

我缺少的東西?

+0

你從'remoteURL'返回的數據格式是什麼? –

+0

類似於'multiselect.data'屬性。 '[{title:'一些標題'}]' – sydev

回答

0

我找到了解決方案,並希望與大家分享:

在初始data陣列中的項目需要一個額外的鍵 - 值對:matchedKey: 'title'

因此,這裏是我的工作options對象:

var typeaheadOptions = { 
     dynamic: true, 
     display: ['title'], 
     template: '<span>{{title}}</span>', 
     cache: true, 
     debug: true, 
     multiselect: { 
      matchOn: ['id'], 
      data: [{title: 'title1', matchedKey: 'title'}, {title: 'title2', matchedKey: 'title'}], 
      callback: { 
       onCancel: function(node, item) {removeItem(item)} 
      } 
     }, 
     source: { 
      posts: { 
       ajax: { 
        method: 'GET', 
        url: remoteURL, 
        data: {search: '{{query}}'} 
       } 
      } 
     }, 
     callback: { 
      onClick: function(node, a, item) {addItem(item)} 
     } 
    }; 

附加鍵 - 值對確實是在typeahead documentationmultiselect的例子,但在周圍的文字不再贅述。