2018-09-13 基于Vue的搜索欄功能實(shí)現(xiàn)(we-ui)


視圖代碼

<div class="new">

? ? ? ? <div class="page__bd">

? ? ? ? ? ? <div class="weui-search-bar" id="searchBar">

? ? ? ? ? ? ? ? <form class="weui-search-bar__form">

? ? ? ? ? ? ? ? ? ? <div class="weui-search-bar__box">

? ? ? ? ? ? ? ? ? ? ? ? <i class="weui-icon-search"></i>

? ? ? ? ? ? ? ? ? ? ? ? <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required="">

? ? ? ? ? ? ? ? ? ? ? ? <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? <label class="weui-search-bar__label" id="searchText" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);">

? ? ? ? ? ? ? ? ? ? ? ? <i class="weui-icon-search"></i>

? ? ? ? ? ? ? ? ? ? ? ? <span>搜索</span>

? ? ? ? ? ? ? ? ? ? </label>

? ? ? ? ? ? ? ? </form>

? ? ? ? ? ? ? ? <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>

? ? ? ? ? ? </div>

? ? ? ? ? ? <div? class="weui-cells searchbar-result" id="searchResult" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1); display: none;">

? ? ? ? ? ? ? ? <div class="weui-cells weui-cells_checkbox">

? ? ? ? ? ? ? ? <label class="weui-cell weui-check__label" v-bind:spell="item.userName" v-for='(item,index) in userList'>

? ? ? ? ? ? ? ? ? ? <div class="weui-cell__hd">

? ? ? ? ? ? ? ? ? ? ? ? <input type="checkbox" class="weui-check" name="checkbox1" id="s11" checked="checked">

? ? ? ? ? ? ? ? ? ? ? ? <i class="weui-icon-checked"></i>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? <div class="weui-cell__bd">

? ? ? ? ? ? ? ? ? ? ? ? <p>{{item.userName}}</p>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? </label>

? ? ? ? ? ? </div>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? </div>

Script代碼

mounted() {

? ? ? ? this.$ajax({

? ? ? ? ? ? method: "get",

? ? ? ? ? ? url: "../static/json/user1.json" //<---本地地址

? ? ? ? })

? ? ? ? .then(response => {

? ? ? ? ? ? let _data1 = response.data;

? ? ? ? ? ? if (response.data !== undefined) {

? ? ? ? ? ? ? ? this.userList = _data1.userList;

? ? ? ? ? ? }

? ? ? ? })

? ? ? ? .catch(function(err) {

? ? ? ? ? ? console.log(err);

? ? ? ? });


? ? //搜索框

? ? ? ? $(function() {

? ? ? ? ? ? var $searchBar = $("#searchBar"),

? ? ? ? ? ? ? ? $searchResult = $("#searchResult"),

? ? ? ? ? ? ? ? $searchText = $("#searchText"),

? ? ? ? ? ? ? ? $searchInput = $("#searchInput"),

? ? ? ? ? ? ? ? $searchClear = $("#searchClear"),

? ? ? ? ? ? ? ? $searchCancel = $("#searchCancel");

? ? ? ? ? ? function hideSearchResult() {

? ? ? ? ? ? ? ? $searchResult.hide();

? ? ? ? ? ? ? ? $searchInput.val("");

? ? ? ? ? ? }

? ? ? ? ? ? function cancelSearch() {

? ? ? ? ? ? ? ? hideSearchResult();

? ? ? ? ? ? ? ? $searchBar.removeClass("weui-search-bar_focusing");

? ? ? ? ? ? ? ? $searchText.show();

? ? ? ? ? ? }

? ? ? ? ? ? function searchName() {

? ? ? ? ? ? ? ? var searchUserName = $("#searchInput").val();

? ? ? ? ? ? ? ? if (searchUserName == "") {

? ? ? ? ? ? ? ? ? ? $("#searchResult").show();

? ? ? ? ? ? ? ? ? ? $(".weui-check__label").show();

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? $(".weui-check__label").each(function() {

? ? ? ? ? ? //給選定標(biāo)簽增加屬性作為標(biāo)識

? ? ? ? ? ? ? ? ? ? var spell = $(this).attr("spell");

? ? ? ? ? ? ? ? ? ? ? ? console.log(spell);

? ? ? ? ? ? ? ? ? ? ? ? if (spell.indexOf(searchUserName) != -1) {

$("#searchResult").show();

? ? ? ? ? ? ? ? ? ? ? ? ? ? $(this).show();

? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? $(this).hide();

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? $("#searchInput").bind("input propertychange", function() {

? ? ? ? ? ? ? ? searchName();

? ? ? ? ? ? });

? ? ? ? ? ? $searchText.on("click", function() {

? ? ? ? ? ? ? ? $searchBar.addClass("weui-search-bar_focusing");

? ? ? ? ? ? ? ? $searchInput.focus();

? ? ? ? ? ? });

? ? ? ? ? ? $searchClear.on("click", function() {

? ? ? ? ? ? ? ? hideSearchResult();

? ? ? ? ? ? ? ? $searchInput.focus();

? ? ? ? ? ? });

? ? ? ? ? ? $searchCancel.on("click", function() {

? ? ? ? ? ? ? ? cancelSearch();

? ? ? ? ? ? ? ? $searchInput.blur();

? ? ? ? ? ? });

? ? ? ? });

? ? }

json數(shù)據(jù)

{

? ? "title": "用戶",

? ? "userList": [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? "userName": "zhang_xiaozhe_張小喆"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? "userName": "zhang_yanwen_章言文"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? "userName": "zhang_xiaoxiao_章小小"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? "userName": "shi_yanyan_史妍妍"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? "userName": "wu_fanda_吳凡達(dá)"

? ? ? ? ? ? ? ? }

? ? ]

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一 眼前這個姑娘和十年前確實(shí)不太一樣了。 看著坐在他對面的冷梅,林川心里冒出的第一個想法。 “你想清楚了嗎?做完這...
    孫湘緗閱讀 766評論 0 3
  • 茍永均 種子踐行日記 時(shí)間2018年 1 月 18 日 第 17 天 身修家和 美麗中國 地方:四川達(dá)州大竹 服務(wù)...
    茍永均閱讀 137評論 0 0
  • 秒速5厘米,那是櫻花飄落的速度,那么怎樣的速度,才能走完我與你之間的距離?——新海誠《秒速五厘米》 有一天一個有智...
    小矮梅閱讀 477評論 0 1

友情鏈接更多精彩內(nèi)容