第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請教如何利用JS完成簡單的商品篩選問題

請教如何利用JS完成簡單的商品篩選問題

三國紛爭 2019-03-15 18:15:24
設(shè)定一個數(shù)組,其中存放有全部商品的名稱,并在界面左側(cè)的邊框中顯示出來2.當用戶點擊左側(cè)邊框中的商品,表示選中該商品,則將該商品從左側(cè)的列表中刪除,并添加到右側(cè)的選中列表中; 3.注意,在右側(cè)的選中列表中商品名稱要按順序排序,添加新選中的商品名,也要插入到對應(yīng)的順序位置
查看完整描述

3 回答

?
手掌心

TA貢獻1942條經(jīng)驗 獲得超3個贊

先說一個最原始的方法實現(xiàn)


class Dispatcher {

    constructor() {

        this._event = [];

    }

    on(eventName, fn) {

        var subscription = {

            eventName,

            callback: fn

        };

        this._event.push(subscription);

        return this;

    }

    off(eventName, fn) {

        this._event = this._event.filter(

            subscription => !(subscription.eventName === eventName && (fn ? subscription.callback === fn : true))

        );

        return this;

    }

    emit(eventName, ...data) {

        this._event.forEach(subscription => {

            if (subscription.eventName === eventName) {

                subscription.callback(...data);

            }

        });

        return this;

    }

}


var leftList = [

    {productId: 10006},

    {productId: 10031},

    {productId: 10016},

    {productId: 10017},

];



var leftList = leftList.map((item, index) => ({...item, order: index,}));


var rightList = [];


var dispatch = new Dispatcher();


dispatch.on('select', function(product) {

    leftList = leftList.filter(_product => product !== _product);

    rightList.push(product);

});


dispatch.on('unselect', function(product) {

    rightList = rightList.filter(_product => product !== _product);

    leftList.push(product);

});


dispatch.emit('select', leftList[0]);

console.log('leftList: ', [...leftList],'\n', 'rightList', [...rightList]);


dispatch.emit('unselect', rightList[0]);

console.log('leftList: ', [...leftList],'\n', 'rightList', [...rightList]);

然后再說一個比較Vue的,左右兩列應(yīng)該算兄弟組件之間的通信,組件的代碼和視圖的代碼應(yīng)該在兩個js文件。組件的通信可以通過子組建1 -> 父組件 -> 子組建2,但是比較麻煩。


我感覺可以用Vuex了。


查看完整回答
反對 回復(fù) 2019-04-08
?
德瑪西亞99

TA貢獻1770條經(jīng)驗 獲得超3個贊

請標明右側(cè)以什么順序排列


查看完整回答
反對 回復(fù) 2019-04-08
  • 3 回答
  • 0 關(guān)注
  • 492 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號