【问题标题】:Summernote get mentions from phpSummernote 从 php 获得提及
【发布时间】:2018-03-07 19:55:17
【问题描述】:

我正在使用带有提及 (@username) 的 Summernote 编辑器。 (https://summernote.org/examples/#hint-for-mention) 我正在寻找一种将用户名数组加载到 Summernote 的方法。 我试过的:(在jQuery promises with summernote hints的帮助下)

$(document).ready(function()
{
    $('.editor').summernote({
        height: 300,
        hint: {
             match: /\B@(\w*)$/,
             users: function(keyword, callback) {
                 $.ajax({
                     url: '{path}/get_users/' + keyword,
                     type: 'get',
                     async: true
                 }).done(callback);
             },
             search: function (keyword, callback) {
                 this.users(keyword, callback);
             },
             content: function (item) {
                 return '@' + item;
             }
       });
   });
});

返回用户名数组的 PHP:

public function get_users($keyword='')
{
    $ar = array('jayden', 'sam', 'alvin', 'david');
    echo json_encode($ar); // returns ['jayden', 'sam', 'alvin', 'david']
}

结果:

Uncaught TypeError: items.map is not a function
    at HintPopover.createItemTemplates (summernote.js:7625)
    at Object.<anonymous> (summernote.js:7672)
    at i (jquery-3.2.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
    at A (jquery-3.2.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)

似乎 this.users 未被识别为数组... 谢谢你的建议……

【问题讨论】:

    标签: jquery arrays summernote hint


    【解决方案1】:

    您需要在ajax 调用中声明返回类型为json

    users: function(keyword, callback) {
                 $.ajax({
                     url: '{path}/get_users/' + keyword,
                     type: 'get',
                     dataType: "json",   <----here
                     async: true
                 }).done(callback);
             },
    

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2014-08-21
      • 2021-04-26
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      相关资源
      最近更新 更多