简体   繁体   English

更改数组值的最简单方法是什么 javascript

[英]What is the easiest way of changing the values of an array javascript

Hello I'm working on a navigation bar,你好,我正在做一个导航栏,

I keep the all the data in an array, when a user clicks on the link I would like to adjust the data in the array to make that link current.我将所有数据保存在一个数组中,当用户单击链接时,我想调整数组中的数据以使该链接成为当前链接。

This is what I have so far but I cant think of a way to make it work...这是我到目前为止所拥有的,但我想不出一种方法让它发挥作用......

export default{
  data()
  {
    return {
      sidebarOpen: false,
      displayUserName: store.user.username,
      navigation : [
      { name: 'Dashboard', to: '/dash/Home', requiresSecondaryBar: true,icon: HomeIcon, current: true, locked: false },
      { name: 'My courses', to: '/dash/MyCourses', requiresSecondaryBar: true,icon: CalendarIcon, current: false, locked: !store.details.hasCourse },
      { name: 'Courses', to: '/dash/Courses', requiresSecondaryBar: false,icon: UserGroupIcon, current: false, locked: false},
      { name: 'Messages', to: '/dash/Messages', requiresSecondaryBar: true,icon: MagnifyingGlassCircleIcon, current: false, locked: false },
      { name: 'Community', to: '/dash/Community', requiresSecondaryBar: true,icon: MegaphoneIcon, current: false, locked: false},
      { name: 'Resources', to: '/dash/Resources', requiresSecondaryBar: true,icon: MapIcon, current: false, locked: false },
      { name: 'Flip', to: '/dash/Flip', requiresSecondaryBar: false,icon: MapIcon, current: false, locked:false },
      ]
    }
  },
  methods: {
    setCurrent(current)
    {
      console.log(this.navigation)
      let navigation = this.navigation

      navigation = navigation.map((names) => {
        return {
          name: names.name,
          to: `/dash/${names.name}`,
          requiresSecondaryBar: names.requiresSecondaryBar,
          icon: names.icon,
          current: this.checkCurrent(current),
          locked: names.locked
        }
      })
      console.log(navigation)
    },
    checkCurrent(current)
    {
      if(current = this.navigation.name)
      {
        return true
      }
      else{
        return false
      }
    }
  }
}

              <nav class="mt-5 flex-1" aria-label="Sidebar">
                <div class="space-y-1 px-2">
                    <router-link v-for="item in navigation" :key="item.name" :to="item.to" @click="setCurrent(true)" :class="[item.current ? 'bg-gray-100 text-gray-900' : 'text-black hover:bg-gray-50 hover:text-gray-900', 'group flex items-center px-2 py-2 text-base font-medium rounded-md']">
                        <component :is="item.icon" :class="[item.current ? 'text-black' : 'text-black group-hover:text-gray-500', 'mr-4 h-6 w-6 router-link']" aria-hidden="true" />
                        {{ item.name }} 
                        <LockClosedIcon v-if="item.locked" :class="['ml-16 items-end rounded-full bg-pink-100 px-0.9 py-0.5  text-pink-800 w-6 h-6']"/>

                      </router-link>
                </div>
              </nav>

Let me see if I understand you, try this in your template @click you must pass the v-for item.让我看看我是否理解你,在你的模板中试试这个 @click 你必须传递 v-for 项目。

@click="setCurrent(item)"

And in yout set current function, you will loop your navigation array and check if that item is the same在你设置当前 function 时,你将循环你的导航数组并检查该项目是否相同

 setCurrent(nav_item)
{
  console.log(this.navigation)
  let navigation = this.navigation

  navigation = navigation.map((names) => {
    return {
      name: names.name,
      to: `/dash/${names.name}`,
      requiresSecondaryBar: names.requiresSecondaryBar,
      icon: names.icon,
      current: names.name == nav_item.name
      locked: names.locked
    }
  })
  console.log(navigation)
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过JSON将Javascript数组发送到PHP的最简单方法是什么? - What is the easiest way to send a Javascript array via JSON to PHP? 查看数组是否有数组的最简单方法?使用Javascript - easiest way to see if array has array? Javascript 找到值之间匹配的最简单方法是什么 - what is the easiest way to finding a match between values 在JavaScript数组中检查真实值的最简单方法 - Easiest way to check for a truthy value in JavaScript array 使用jQuery的ajax()函数将javascript数组或其值传递给servlet的最简单方法 - Easiest way to pass a javascript array or its values to a servlet using jQuery's ajax() function 更改JavaScript数组值 - Changing JavaScript array values 使用javascript,更改锯齿状数组中元素长度的有效方法是什么? - Using javascript, what is an efficient way of changing the length of elements in a jagged array? Android-将值从HTML或Javascript传递到Java的最简单方法? - Android - Easiest way to pass values from HTML or Javascript to Java? 在Javascript Web脚本和Erlang服务器之间进行通信的最简单方法是什么 - What is the easiest way to communicate between Javascript web script to an Erlang server 使用javascript读取/操作查询字符串参数的最简单方法是什么? - What is the easiest way to read/manipulate query string params using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM