切換頁碼保存選中數(shù)據(jù)

切換頁碼選中數(shù)據(jù)不清空
方法一:在當前頁遍歷,判斷選擇的數(shù)據(jù)在此頁中是否被選中,是否在緩存區(qū)
點擊保存時,將緩存的數(shù)據(jù)存進真正需要的地方
初始化時,將真實數(shù)據(jù)賦給暫存數(shù)據(jù)
取消時,恢復初始數(shù)據(jù)
selection 是當前選中的數(shù)據(jù)
RelateCacheCode緩存選中未保存的數(shù)據(jù)
projectList 當前頁的數(shù)據(jù)

dealSelect (selection) {
      // selection是當前選中的數(shù)據(jù)
      let ids = []
      this.RelateCacheCode.forEach(item => {
        ids.push(item.projectId)
      })
      let selectIds = []
      selection.forEach(item => {
        selectIds.push(item.projectId)
      })
      this.projectList.forEach(item => {
        if (selectIds.indexOf(item.projectId) > -1) {
          let index = ids.indexOf(item.projectId)
          // 是當前頁的數(shù)據(jù)并且被選中
          if (index === -1 || !ids.length) {
            // 不在所有操作的選中的數(shù)據(jù)里面,說明是新增的一條,加到選中數(shù)據(jù)里面
            this.RelateCacheCode.push(item)
          }
        } else {
          // 當前頁的數(shù)據(jù),但沒被選中
          let index = ids.indexOf(item.projectId)
          if (ids.length && index > -1) {
            // 判斷此數(shù)據(jù)是否在所有選中數(shù)據(jù)里,存在,則是取消勾選,應當刪掉此條
            this.RelateCacheCode.splice(index, 1)
            // 緩存選中數(shù)據(jù)也要相應減少一條
            ids.splice(ids.indexOf(item.projectId), 1)
          }
        }
      })
    },
// 回顯時使用下面判斷
              this.projectList.map((item, i) => {
                if (this.RelateCacheCode.length !== 0) {
                  let selectedId = []
                  this.RelateCacheCode.forEach((item, index) => {
                    selectedId.push(item.projectId)
                  })
                  if (selectedId.indexOf(item.projectId) !== -1) {
                    item.checked = true // 是否勾選
                  }
                }
              })

方法二:使用多維數(shù)組保存選中數(shù)據(jù),此方法不需要暫存數(shù)據(jù),但是產(chǎn)品說我要加個搜索功能,就不能使用了(不推薦)

this.SelectList[this.currentPage] = selection
// 保存時降維處理
let list = this.SelectList.flat(Infinity)
// 回顯時使用SelectCodeList的降維數(shù)組判斷(初始化時也需要賦值)
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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