如何在更改Bootstrap列表组时更改另一个列表组?

vnjpjtjt  于 2023-04-18  发布在  Bootstrap
关注(0)|答案(1)|浏览(112)

我有两个列表组,第二个是基于JSON数据和过滤的(代码中有两个if语句)
这个fiddle https://jsfiddle.net/andernathan/fL3cwoa2/32/是我尝试做的事情的一个非常简化的版本。
当你点击“你好”的第二个列表显示“你好”和“你好他们”。当你点击“你好”,我想第二个列表只显示“你好他们”(我的第二个“如果”语句)
谢谢...一如既往!

const activities1 = [];
const activities2 = [];

var json = {
  "Tasks": [{
    "Subject": "Howdy their"
  }, {
    "Subject": "Howdy there"
  }, {
    "Subject": "Howdy they're"
  }]
}

var obj = json.Tasks;

for (var i = 0; i < obj.length; i++) {
  activitySub = (obj[i].Subject);

  if (activitySub != "Howdy their") {
    activities1.push({
      activitySub,
    })
  }
  if (activitySub === "Howdy their") {
    activities2.push({
      activitySub,
    })
  }
}
console.log(JSON.parse(JSON.stringify(activities1)));
console.log(JSON.parse(JSON.stringify(activities2)));

const html = activities1.map(obj => {
  let item = document.querySelector('#template').innerHTML;
  item = item.replace('{sub}', obj.activitySub);
  return item;
});

document.querySelector('#list').innerHTML = html.join('');
<ul class="list-group">
  <li class="active">
    <a href="#"> Howdy</a>
  </li>
  <li>
    <a href="#"> Howdee</a>
  </li>
</ul>

<ul class="list-group">
  <div id="list"></div>
  <template id="template">
       <h5 class="media-heading">{sub}</h5>
      </template>
</ul>
pbossiut

pbossiut1#

嗯,也许我太激动了,把它弄得太复杂了,但也许这种更通用的方法会有所帮助。
你可以从这个标记开始

<ul class="list-group" id="listOne">
  <li class="active">
    <a href="#">
      Howdy
    </a>
  </li>
  <li>
    <a href="#">
      Howdee
    </a>
  </li>
</ul>
<ul class="list-group" id="listTwo"></ul>

用javascript实现

var visibleIndex = 0;

// added a second prop to the object to determine when it should be visible
var json = {"Tasks":[{"subject":"Howdy their", "showOn" : 0},{"subject":"Howdy there", "showOn" : 1},{"subject":"Howdy they're", "showOn" : 1}]}
var taskList = json.Tasks;

// store elements in vars to save queries for subsequent clicks
var listOne = document.getElementById('listOne');
var listTwo = document.getElementById('listTwo');
var listLinks = listOne.querySelectorAll('a');

// click lsitener for list One
// Just do something when index is changed
listLinks.forEach( (link, index) => {
    link.addEventListener('click', () => {
        var clickedIndex = index;
    if (clickedIndex !== visibleIndex) {
        visibleIndex = parseInt(clickedIndex);
        rebuildList();
    }
  })
});

function rebuildList() {
    // set clicked element to active
    listLinks.forEach( (link, index) => {
    if (index === visibleIndex) {
        link.parentElement.classList.add('active');
    } else {
        link.parentElement.classList.remove('active');
    }
  });
  
  // remove all elements from list two
  while( listTwo.firstChild ){
    listTwo.removeChild( listTwo.firstChild );
    }
  
  // add all elements where showOn is equal to visibelIndex
  taskList.forEach( taskObject => {
    if (taskObject.showOn === visibleIndex) {
        addToSecondList(taskObject.subject)
    }
  });
}

// Yeah, we use vanilla javascript to create elements
function addToSecondList(text) {
    var newItem = document.createElement('li');
  newItem.textContent = text;
  listTwo.appendChild(newItem);
}

// initially  build list with visibleIndex 0, perhaps not needed
rebuildList(visibleIndex);

相关问题