从零开始写项目第三篇【在线聊天和个人收藏夹】(二)

简介: 笔记

HTML和CSS,我用得是Bootstrap的。

<!--搜索网站-->
    <div class="row " style="position: absolute; margin:0px auto;
text-align:center; ">
        <div class="col-lg-6 gover_search">
            <div class="input-group gover_search_form clearfix">
                <input class="form-control input-lg " type="text" placeholder="请输入您自定义的网站名..." id="condition">
                <span class="input-group-btn">
                    <button class="btn btn-primary btn-lg" type="button" id="search">搜索</button>
                </span>
                <div class="search_suggest" id="gov_search_suggest">
                    <ul>
                    </ul>
                </div>
            </div>
        </div>
    </div>
<!--搜索网站-->
    <div class="row " style="position: absolute; margin:0px auto;
text-align:center; ">
        <div class="col-lg-6 gover_search">
            <div class="input-group gover_search_form clearfix">
                <input class="form-control input-lg " type="text" placeholder="请输入您自定义的网站名..." id="condition">
                <span class="input-group-btn">
                    <button class="btn btn-primary btn-lg" type="button" id="search">搜索</button>
                </span>
                <div class="search_suggest" id="gov_search_suggest">
                    <ul>
                    </ul>
                </div>
            </div>
        </div>
    </div>

实现效果:

3.jpg


增加网站


在增加网站的时候,我们希望以单一的名字来进行添加,因为我们是通过命名来进行搜索对应的网站的。

所以在添加网站的时候需要先判断有没有该命名才能继续添加、如果存在命名了,就不让它添加了。

代码如下:

/**
     * 插入数据进索引库中,在插入之前先判断该“命名”索引是否存在
     *
     * @param client
     * @param indexName
     * @param type
     * @param json
     * @param webSiteName
     * @return
     */
    public static String insertIndexData(TransportClient client, String indexName, String type, String json, String webSiteName) {
        //在插入之前、查看一下有没有该数据、如果有就不允许插入了【本站只允许命名是唯一的】
        List<String> list = queryIndexByCondition(client, indexName, type, webSiteName);
        if (list != null && list.size() > 0) {
            return "hasWebSiteName";
        } else {
            client.prepareIndex(indexName, type).setSource(json).get();
            return "success";
        }
    }


删除与查询网站


添加完网站之后,我们可以查询当前用户所有的网站,并可以删除

查询用户所有的网站我还使用到了Elasticsearch的分页功能:

/**
     * 根据用户id查询出索引记录、并对其进行分页
     *
     * @param client
     * @param indexName
     * @param type
     * @param userId
     * @param currentPage
     * @return
     */
    public static Map<String, String> queryIndexById(TransportClient client, String indexName, String type, String userId, Integer currentPage) {
        QueryBuilder query = QueryBuilders.matchQuery("userId", userId);
        //接收返回的数据
        Map<String, String> map = new HashMap();
        //计算出开始取的页数
        int startIndex = (currentPage - 1) * EsUtilsPro.PAGE_SIZE;
        //查询出总记录数
        Long totalRecordCount = ElasticsearchUtils.queryTotalCount(client, indexName, type, userId);
        //查询出总页数
        Long totalPageCount = totalRecordCount % EsUtilsPro.PAGE_SIZE == 0 ? totalRecordCount / EsUtilsPro.PAGE_SIZE : totalRecordCount / EsUtilsPro.PAGE_SIZE + 1;
        SearchResponse response = client.prepareSearch(indexName).setTypes(type).setQuery(query).setFrom(startIndex).setSize(EsUtilsPro.PAGE_SIZE).execute()
                .actionGet();
        SearchHits hits = response.getHits();
        //返回一个最高匹配那个对象就好了。
        if (hits.totalHits() > 0) {
            for (SearchHit hit : hits) {
                map.put(hit.getId(), hit.getSourceAsString());
            }
        }
        map.put("currentPage", String.valueOf(currentPage));
        map.put("totalRecordCount", String.valueOf(totalRecordCount));
        map.put("totalPageCount", String.valueOf(totalPageCount));
        return map;
    }

查询总记录数

/**
     * 根据用户id查询该用户拥有的总记录数
     *
     * @param client
     * @param indexName
     * @param type
     * @param userId
     * @return
     */
    public static Long queryTotalCount(TransportClient client, String indexName, String type, String userId) {
        QueryBuilder query = QueryBuilders.matchQuery("userId", userId);
        SearchResponse response = client.prepareSearch(indexName).setTypes(type).setQuery(query).execute().actionGet();
        return response.getHits().totalHits();
    }

使用Map集合的原因是因为我们要在查询的基础上实现删除的功能,要想删除就必须知道当前记录的id,因此就使用到了Map集合了。

<!--根据Id查询索引记录-->
    <script>
        $(function () {
            $("#queryById").click(function () {
                var currentPage = $("#currentPage").val();
                if (currentPage == null || currentPage == "") {
                    currentPage = 1;
                }
                $.ajax({
                    url: path + "/favorites/querySiteById.do",
                    type: "post",
                    data: {userId: $("#userId").val(), currentPage: currentPage},
                    success: function (responseText) {
                        //返回的是一个Map集合,我们遍历Map集合,
                        for (var index in responseText) {
                            //获取Map的value,index就代表Map的key
                            var jsonObj = responseText[index]
                            if (index.length > 16) {
                                //获取JSON对象
                                var data = eval("(" + jsonObj + ")");
                                $("#manageSiteContent").before("<tr><td><input type='text' value=" + data.webSiteAddr + " ></input></td><td><input type='text' value=" + data.webSiteName + " ></input></td><td><a href='${request.contextPath}/favorites/deleteSiteById.do?indexId=" + index + "'>删除</a></td></tr>");
                            } else {
                                if (index == "currentPage") {
                                    $("#currentPage").val(jsonObj);
                                } else if (index == "totalPageCount") {
                                    $("#totalPageCount").val(jsonObj);
                                } else {
                                    $("#totalRecordCount").val(jsonObj);
                                }
                            }
                        }
                        createPage($("#currentPage").val(), $("#totalPageCount").val(), $("#totalRecordCount").val());
                    },
                    error: function () {
                        sweetAlert("系统错误了");
                    }
                });
            });
        });
    </script>

生成分页的样式、并指定当前页数(i就作为当前的页数)

<!--生成分页样式-->
    <script>
        function createPage(totalPageCount) {
            for (var i = 1; i <= totalPageCount; i++) {
                $(".pagination").append("<li><a href='#' onclick='queryPage(" + i + ")'>" + i + "</a></li>");
            }
        }
    </script>

异步查询分页数据、在查询之前先把原有的记录删除掉。

<!--异步查询分页数据-->
    <script>
        function queryPage(currentPage) {
            //把原有的数据删除掉
            $("#manageSiteTr").siblings().empty();
            $.ajax({
                url: path + "/favorites/querySiteById.do",
                type: "post",
                data: {userId: $("#userId").val(), currentPage: currentPage},
                success: function (responseText) {
                    //返回的是一个Map集合,我们遍历Map集合,
                    for (var index in responseText) {
                        //获取Map的value,index就代表Map的key
                        var jsonObj = responseText[index]
                        if (index.length > 16) {
                            //获取JSON对象
                            var data = eval("(" + jsonObj + ")");
                            $("#manageSiteContent").before("<tr><td><input type='text' value=" + data.webSiteAddr + " ></input></td><td><input type='text' value=" + data.webSiteName + " ></input></td><td><a href='${request.contextPath}/favorites/deleteSiteById.do?indexId=" + index + "'>删除</a></td></tr>");
                        } else {
                            if (index == "currentPage") {
                                $("#currentPage").val(jsonObj);
                            } else if (index == "totalPageCount") {
                                $("#totalPageCount").val(jsonObj);
                            } else {
                                $("#totalRecordCount").val(jsonObj);
                            }
                        }
                    }
                },
                error: function () {
                    sweetAlert("系统错误了");
                }
            });
        }
    </script>

最终的效果如下:

4.jpg5.jpg

目录
相关文章
|
9月前
|
小程序
云开发(微信-小程序)笔记(十四)---- 收藏,点赞(上)
云开发(微信-小程序)笔记(十四)---- 收藏,点赞(上)
82 0
|
存储 NoSQL 安全
取代微信?RocketChat开源聊天应用搭建教程
取代微信?RocketChat开源聊天应用搭建教程
取代微信?RocketChat开源聊天应用搭建教程
|
5月前
|
前端开发
前端知识笔记(二十)———简易弹窗制作
前端知识笔记(二十)———简易弹窗制作
28 0
|
9月前
|
小程序 数据库
云开发(微信-小程序)笔记(十五)---- 收藏,点赞(下)
云开发(微信-小程序)笔记(十五)---- 收藏,点赞(下)
82 0
|
10月前
|
移动开发 算法 HTML5
提升网页阅读体验的三款优秀大纲插件(2023)
阅读长篇文章或复杂网页时,清晰的大纲结构可以帮助我们更好地理解和组织信息。在这篇博客中,我将向大家推荐三款优秀的网页大纲插件,帮助您提升阅读效率和体验。
提升网页阅读体验的三款优秀大纲插件(2023)
|
移动开发 JavaScript 前端开发
三分钟学会 H5 聊天机器人开发(附源码和在线演示)
【学习目标】 熟悉和掌握 HTML结构和CSS的相关知识 学会使用HBuilder进行APP打包 熟悉JavaScript的基本用法和jQuery的使用(提前预习)
261 0
|
小程序
视频号主页,实现一键添加个人微信功能,留客更方便,真香
在视频号主页放置添加微信的按钮,其实微信已经支持了,只不过只支持企业微信,不支持个人微信,那怎么办,只能自己实现了。
359 0
视频号主页,实现一键添加个人微信功能,留客更方便,真香
|
小程序 API Android开发
小程序开发-第三章第四节点击查看大图,保存壁纸-全栈工程师之路-中级篇
小程序开发-第三章第四节点击查看大图,保存壁纸-全栈工程师之路-中级篇
132 0
小程序开发-第三章第四节点击查看大图,保存壁纸-全栈工程师之路-中级篇
|
搜索推荐 数据可视化 API