douduan9391 2018-03-12 10:49
浏览 49
已采纳

无法使用动态数据创建自动建议功能

I am using this plugin to create combination feature of autocomplete and tag. My code for input field and script related to it

<input id="form-tags-4" name="tags-4" type="text" value="">
<script type="text/javascript">
  $(function() {

    $('#form-tags-4').tagsInput({
      'autocomplete': {

        source: [
          'apple',
          'banana',
          'orange',
          'pizza'
        ]
      } 
    });
  });
</script>

It is working fine with static data, however i want that in place of static data that is stored in source dynamic data of database should get stored.

currently my dynamic data is in $normal_skill whose format looks like this

Array
(
    [0] => stdClass Object
        (
            [normal_skill] => HTML
        )

    [1] => stdClass Object
        (
            [normal_skill] => CSS
        )

    [2] => stdClass Object
        (
            [normal_skill] => Javascript
        )
)

I tried to collect the data from normal_skill and put it inside an array like this

$items = array();
foreach($normal_skill as $n_skill) {
 $items[] = $n_skill->normal_skill;
}

And in place of source i called $items, after replacing source with $items, the autosuggest and tag feature stopped working.

For testing I printed $items, it was showing data but when replaced with source then there was no data. Console is also not showing any particular error.

Can anyone please tell how i can replace source with my data and i would want it also performs letter to letter search. eg:- if i type "h" then words starting with h should come in autosuggest, however right now all the words containing "h" are getting displayed

  • 写回答

1条回答 默认 最新

  • douwo3665 2018-03-12 11:01
    关注

    Did you try passing it to the javascript with json_encode()?

    <input id="form-tags-4" name="tags-4" type="text" value="">
    <script type="text/javascript">
      $(function() {
    
        $('#form-tags-4').tagsInput({
          'autocomplete': {
            source: <?= json_encode($items) ?>
          } 
        });
      });
    </script>
    

    If its a remote source.

    $items = array();
    foreach($normal_skill as $n_skill) {
     $items[] = $n_skill->normal_skill;
    }
    exit(json_encode($items));
    

    Then in your javascript, set it to the source file:

    <script type="text/javascript">
      $(function() {
    
        $('#form-tags-4').tagsInput({
          'autocomplete': {
            source: './tags.php'
          } 
        });
      });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀