2017-10-12 87 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的例子,但在周围的文字不再赘述。