突出显示搜索字词

时间:2015-01-02 10:24:52

标签: python sqlalchemy pyramid

1.如何突出显示返回的搜索项目或结果中的数据,例如ctr + f在打开的文件中进行普通项目/元素搜索的方式。 2.添加json自动完成,即在用户根据数据库数据键入搜索栏时给出建议 这是我的视图代码,但它只是返回结果,而没有真正突出显示搜索词。 任何帮助PLIZ:

@view_config(route_name =“search”,renderer ='。/ templates / search.mako',permission =“view”) def搜索(请求):     search = request.params.get(“search”,“”)
    dbsession = DBSession()

##Album Results
query = dbsession.query(Album).join(Artist).join(Genre).join(Song).\
    filter(

        or_(
            Album.album_name.like(search + "%"),
            Artist.artist.like(search + "%"),
            Genre.genre.like(search + "%"),

            )
        )
statalbum = dbsession.query(Album).join(Artist).join(Genre).join(Song).\
    filter(

        or_(
            Album.album_name.like(search + "%"),
            Artist.artist.like(search + "%"),
            Genre.genre.like(search + "%"),

            )
        ).count()

# Songs Results

dbsession = DBSession()
songs = dbsession.query(Song).\
    filter(Song.title.like(search + "%") )

statsong = dbsession.query(Song).\
    filter(Song.title.like(search + "%") ).count()

# paginate 
page_url = paginate.PageURL_WebOb(request)
albums = Page(query, 
                 page=int(request.params.get("page", 1)), 
                 items_per_page=12, 
                 url=page_url)
# paginate songs
songs = Page(songs, 
                 page=int(request.params.get("page", 1)), 
                 items_per_page=12, 
                 url=page_url)
try:
    genres = DBSession.query(Genre).order_by(Genre.genre).all()
    info = DBSession.query(Song).join(Album).filter_by(id=id).filter(Song.album_id == Album.id ).first()
    #albums = DBSession.query(Album).all() 

except DBAPIError:
    return Response(conn_err_msg, content_type='text/plain', status_int=500)
return {  'genres':genres, 'info':info, 'songs':songs, 'albums':albums, 'statsong':statsong, 'statalbum':statalbum, 'project': 'beatstore'}

1 个答案:

答案 0 :(得分:2)

  1. 对于突出显示返回的搜索项,您必须使用CSS并更改背景颜色。如下所示:

      

    .highlighted {           background-color:#FFFF00;       }

    并突出显示一个单词,您必须将其括在一个突出显示类的范围内,如下所示:

    <span class="highlighted">This is highlighted text</span>
    
  2. 至于从数据库中检索JSON,您必须从Javascript创建一个AJAX调用,该调用将调用服务器以获取建议。您可能必须在keypress事件上绑定AJAX调用;类似的东西:

      

    $(&#34; #text-field&#34;)。keyup(function(){

    $.getJSON("/url_to_suggestions", function(data) {
    
        $.each(data, function(key, val) {
    
            //Do something with key and val
    
        }
    
    ); 
    
         

    });