Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone help with Autocomplete? #233

Closed
graingerkid opened this issue Feb 3, 2018 · 15 comments
Closed

Can anyone help with Autocomplete? #233

graingerkid opened this issue Feb 3, 2018 · 15 comments

Comments

@graingerkid
Copy link

graingerkid commented Feb 3, 2018

Im using a library to autocomplete keywords but I can't get it working with Emojionearea

The following fiddle has 2 textareas one with emojionearea and one without; only the latter is working for autocomplete when you type "@"

https://jsfiddle.net/ukLaz8cm/134/

Can anyone help? JS is not my primary language.

@mervick
Copy link
Owner

mervick commented Feb 3, 2018

You can use bundled textcomplete plugin,
add script below in the your header,

<script src="https://cdn.rawgit.com/yuku-t/jquery-textcomplete/v1.3.4/dist/jquery.textcomplete.js"></script>

and after that

$(document).ready(function() {
    $("#emojionearea1").emojioneArea({
        pickerPosition: "right",
        tonesStyle: "bullet",
        autocomplete: true,
        events: {
            ready: function() {
                this.editor.textcomplete([{
                    id: 'emojionearea',
                    match: /\B@([\-\d\w]*)$/,
                    search: function (term, callback) {
                        // this code must be replaced with your
                        // load mentions from ajax
                        var mentions = ['Peter', 'Tom', 'Anne'];
                        callback($.map(mentions, function (mention) {
                        return mention.indexOf(term) === 0 ? mention : null;
                    }));
                    },
                    template: function (value) {
                        return '<b>' + value + '</b>&nbsp;';
                    },
                    replace: function (value) {
                        return '<b>@' + value + '</b>&nbsp;';
                    },
                    cache: true,
                    index: 1
                }]);
            }
        }
    });
});

Example: https://jsfiddle.net/1v03Lqnu/898/
type : for autocomplete emotions
type @ for autocomplete mentions

@LouisSyfer
Copy link

Hi Mervick,

First of all congratulations for this emoji plugin. I think is the best we can find now. But I have the same problem as described is this discussion. I tried all the codes I could and I checked everything about what's showing here and textcomplete. I want to associate your plugin and a mention autocomplete code but I can't reach my database table to retrieve the datas. When I type @ it's showing [object Object]. I tried all I know to debug. With no result. If only you could help please ! If you would just reply and I'll show to you the code I did. Thanks a lot and see you soon !

@mervick
Copy link
Owner

mervick commented Jul 21, 2018

@LouisSyfer

$(document).ready(function() {
    $("#emojionearea1").emojioneArea({
        pickerPosition: "right",
        tonesStyle: "bullet",
        autocomplete: true,
        events: {
            ready: function() {
            	$.ajax('url/to')
                .success(data) {
                   this.editor.textcomplete([{
                      id: 'emojionearea',
                      match: /\B@([\-\d\w]*)$/,
                      search: function (term, callback) {
                          callback($.map(data, function (mention) {
                          return mention.indexOf(term) === 0 ? mention : null;
                      }));
                      },
                      template: function (value) {
                          return '<b>' + value + '</b>&nbsp;';
                      },
                      replace: function (value) {
                          return '<b>@' + value + '</b>&nbsp;';
                      },
                      cache: true,
                      index: 1
                  }]);
                });
            }
        }
    });
});

@LouisSyfer
Copy link

Good evening Mervick.

Thank you for the reply. I try as soon as possible and I let you know.

Thanks again and see you soon !

@LouisSyfer
Copy link

Hi Mervick,

Some news :

I should have adapted your code to fit with the most recent jQuery version :

$("#content").emojioneArea({
autocomplete: true,
events: {
ready: function() {
$.ajax({
url:'mention_autocomplete_posts.php',
success:function(data) {
this.editor.textcomplete([{
id: 'emojionearea',
match: /\B@([-\d\w]*)$/,
search: function (term, callback) {

                    callback($.map(data, function (mention) {
                    return mention.indexOf(term) === 0 ? mention : null;
                }));
                },
                template: function (value) {
                    return '<b>' + value + '</b>&nbsp;';
                },
                replace: function (value) {
                    return '<b>@' + value + '</b>&nbsp;';
                },
                cache: true,
                index: 1
            }]);
    
        }
        
        });
        
    }
}

});

Console says : Uncaught TypeError: Cannot read property 'textcomplete' of undefined

I tried to install inside the js file :

{

                    id: css_class,
                    match: /\B@([\-\d\w]*)$/,
                    search: function (term, callback) {
                    		// this code must be replaced with your
                        // load mentions from ajax
                       $.ajax({
                        url:'mention_autocomplete_posts.php',
                        success:function(mentions) {
                            
                        callback($.map(mentions, function (mention) {
                            return mention.indexOf(term) === 0 ? mention : null; 
                        }));
                    },
                    template: function (value) {
                        return '<b>' + value + '</b>&nbsp;';
                    },
                    replace: function (value) {
                        return '<b>@' + value + '</b>&nbsp;';
                    },
                    cache: true,
                    index: 1
                   });
                }
            }

Not working as well. Maybe I did something wrong. Thanx anyway !

@mervick
Copy link
Owner

mervick commented Jul 22, 2018

Add this script to head

<script src="https://cdn.rawgit.com/yuku-t/jquery-textcomplete/v1.3.4/dist/jquery.textcomplete.js"></script>

@LouisSyfer
Copy link

I have done it already. That changes nothing...

@mervick
Copy link
Owner

mervick commented Jul 22, 2018

@LouisSyfer , if you want my help. please first format your code

@LouisSyfer
Copy link

I would like to. Sorry I don't know why it did not...

@mervick
Copy link
Owner

mervick commented Jul 22, 2018

@LouisSyfer
Copy link

Maybe this one is the best. But I don't know why I get [object Object] ... Sure I'm better in PHP !!

$("#content").emojioneArea({
                autocomplete:true,
                events:{
                ready: function() {
                this.editor.textcomplete([{
                    match: /(^|\s)@(\w*)$/,
                    dataType: "json",
                    search: function (term, callback) {
                        // this code must be replaced with your
                        // load mentions from ajax
                         $.getJSON("mention_autocomplete2.php", { q: term })
                        .done(function (resp) { callback(resp); })
                        .fail(function ()     { callback([]);   });
        
                        callback($.map(term, function (mention) {
                        return mention.indexOf(term) === 0 ? mention : null;
                    }));
                    },
                   template: function (value) {
                        return '<b>' + value + '</b>&nbsp;';
                    },
                    replace: function (value) {
                        return '<b>@' + value + '</b>&nbsp;';
                      
                    },
                    cache: true,
                    index: 1
                }]);
            }
        }
    });     

But maybe the problem come from the php side...


        session_start(); 
        
	include_once("config/database.php");
	include_once("includes/functions.php");
	include_once("includes/constants.php");
    

        
if(isset($_POST['term'])){


$term= $_POST['term'];
 //$term= $_GET['term'];
$query = $db->prepare('SELECT * FROM user 
                       WHERE mention_user LIKE :term'); 
$query->execute(array('term' => '%'.$term.'%'));



$array = array(); 

?>
<?php    
while($donnee = $query->fetch()) 
{
    
   array_push($array, $donnee["mention_user"]);
    //array_push($array, $donnee["id_user"]);
    

    ?>
    
        

<?php 

} ?>

<?php
echo json_encode($array);




}
?>


@mervick
Copy link
Owner

mervick commented Jul 22, 2018

Your code is terrible!

At least you have error in the php side,
in js, you send GET request with param q

$.getJSON("mention_autocomplete2.php", { q: term })

so in php you should get search param from

$_GET['q']

but you use

if (isset($_POST['term'])) {
    $term= $_POST['term'];

@LouisSyfer
Copy link

Well I think I have already tried this ($_GET[‘q ‘] instead of $_POST[‘term’]), I will try again and let you know...

@LouisSyfer
Copy link

IT'S WORKING ! Almost... I mean you can retrieve datas from your table, the autocomplete works, but like a normal one : you type @ at the beginning of the textarea but I have to make a "carriage" return if I want to mention other user.
Ex :
It' possible to do :
@mervick how are you ?
@jonhdoe are you fine ?

But you can't :
How are you @mervick ? And what about you @johndoe ?

The code :

$("#content").emojioneArea({
                autocomplete:true,
                events:{
                ready: function() {
                this.editor.textcomplete([{
                    match: /(^|\s)@(\w*)$/,
                    dataType: "json",
                    search: function (term, callback) {
                        // this code must be replaced with your
                        // load mentions from ajax
                          $.getJSON("mention_autocomplete2.php", { q: term })
                        .done(function  (resp) { callback (resp);  })
                        .fail(function (resp)     { callback([resp]); });
                        
                        callback($.map(term, function (mention) {
                        return mention.indexOf(term) === 0 ? mention : null;
                    }));
                    },
                   template: function (value) {
                        return '<b>' + value + '</b>&nbsp;';
                    },
                    replace: function (value) {
                        return '<b>@' + value + '</b>&nbsp;';
                      
                    },
                    cache: true,
                    index: 1
                }]);
            }
        }
    });     

   
<?php

        session_start(); 
        
	include_once("config/database.php");
	include_once("includes/functions.php");
	include_once("includes/constants.php");
    

$term= $_GET['q'];
$query = $db->prepare('SELECT * FROM user WHERE mention_user LIKE :q'); 
$query->execute(array('q' => '%'.$term.'%'));


$array = array(); 


?>

<?php    

while($donnee = $query->fetch())
        
{

    array_push($array, $donnee["mention_user"]);
   
?>
    
     

<?php } ?>

<?php
header('Content-Type: application/json');
echo json_encode($array);




?>

@LouisSyfer
Copy link

It's ok Mervick it's working fine THANKS A LOT !

I just deleted "index:1" at the end of the code. I'm going to test it again but for the moment THAT WORKS pefectly !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants