简体   繁体   English

如何在表格的每一行中保留选中的按钮

[英]How to leave button selected in each row of a table

I have a doubt, I think it's even simple.我有疑问,我认为这甚至很简单。 I have a table that receives data from a JSON, each row of that table is a JSON field.我有一个从 JSON 接收数据的表,该表的每一行都是一个 JSON 字段。 On each line I added two buttons (Like/Dislike) to check the search quality.在每一行我添加了两个按钮(喜欢/不喜欢)来检查搜索质量。 However, when I click on the like, for example, and I click on the like of the second line, it does not keep the like of the first line selected.但是,例如,当我单击类似的按钮并单击第二行的类似按钮时,它不会保持选中第一行的类似按钮。

The method should work like this, after returning the data, the user will go through line by line evaluating the search, choosing between like or dislike.该方法应该这样工作,返回数据后,用户将 go 逐行评估搜索,在喜欢或不喜欢之间做出选择。 After that, we will take this assessment and save it.之后,我们将进行此评估并保存。

However, it is not keeping the evaluation option selected in each line.但是,它并没有保持在每一行中选择评估选项。

The code already has integration with VUE代码已经与 VUE 集成

Follow File HTML关注文件 HTML

<template>
<div v-for="regList in myJson" :key="regList" class="container" >
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <table>
    <thead>
      <tr>
        <th>Documento</th>
        <th colspan="2">Avalie a Busca</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="countryList in regList[2]" :key="countryList">
        <td style="visibility: visible">
          {{countryList}}
        </td>
        <td>
          <button class="btn btn-1" type="button"><i class="fa fa-thumbs-up"></i></button>
        </td>
        <td>
          <button class="btn btn-2" type="button"><i class="fa fa-thumbs-down"></i></button>
        </td>
      </tr>
    </tbody>
  </table>
  </div>  
</template>

在此处输入图像描述

EDIT 1编辑 1

Nikola Pavicevic's solution seems to be adequate, but I needed to detail my code a little more. Nikola Pavicevic 的解决方案似乎足够了,但我需要更详细地说明我的代码。 I have a Vue file running the code.我有一个运行代码的 Vue 文件。 The project is for the user to type a term, send it to the backend, which will return a Json, with an autocomplete phrase and also information from another API. In the frontend I separate this sentence from the rest of the JSON and present the autocomplete with the JSON inside the Table.该项目是供用户键入一个术语,将其发送到后端,它将返回一个 Json,带有一个自动完成短语以及另一个 API 的信息。在前端,我将这句话与 JSON 的 rest 分开并呈现使用表内的 JSON 自动完成。 The data I show in the table is just a description.我在表中显示的数据只是一个描述。 I'll also leave a model of how the JSON goes to the table.我还会留下一个 model,说明 JSON 是如何进入表格的。

Part.部分。 Vue:视图:

<script>
export default {
  name: 'Autocomplete',
  data: function() {
    return {
      autoComplete: "",
      maxChars: 75,
      connection: null, 
      myJson: []
    }
  },
  mounted() {
    const url = "ws://localhost:8000/"
    this.connection = new WebSocket(url);
    this.connection.onopen = () => console.log("connection established");
    this.connection.onmessage = this.receiveText;
  },
  methods: {
       sendText() {
      const inputText = this.$refs.editbar.textContent;
      this.connection.send(inputText);
    },
    receiveText(event) {
      let result = JSON.parse(event.data)
      this.autoComplete = result.shift();
      this.myJson = result
    }
  }
}
</script>

Return Json in table:返回表中的 Json:

[
    {
        "1": 0.471,
        "2": {
            "82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    },
    {
        "1": 0.47,
        "2": {
            "686": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    }
]

EDIT 2 - Problem Soluction OK编辑 2 - 问题解决方案确定

Following Nikola's instructions, I ended up solving the problems.按照尼古拉的指示,我最终解决了问题。 I just had to adjust to my situation.我只需要适应我的情况。 Follow code:遵循代码:

The ForEach I had to leave inside the Receive Text, as it is the place where my JSON is supplied with information ForEach 我不得不留在接收文本中,因为它是我的 JSON 提供信息的地方

receiveText(event) {
      let result = JSON.parse(event.data)
      this.autoComplete = result.shift();
      this.myJson = result
      this.selected = []
      this.myJson.forEach((m, i) => {return this.selected.push({i: i, state: null})})
    }

For the rest, I followed the same guidelines as Nikola, just adjusting the code I already had and implementing the suggestions.对于 rest,我遵循与 Nikola 相同的指导方针,只是调整我已有的代码并实施建议。

If I understood you correctly maybe like following snippet:如果我对您的理解正确的话,您可能会喜欢以下片段:

 const app = Vue.createApp({ data() { return { myJson: [{"1": 0.471, "2": {"82": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}}, {"1": 0.47, "2": {"686": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}}], selected: [] }; }, mounted() { this.myJson.forEach((m, i) => { return this.selected.push({i: i, state: null}) }) }, methods: { setState(idx, state) { this.selected[idx].state = state } } }) app.mount('#demo')
 .active { background: gold;important. }:btn { border-radius; 4px: border; none: padding. ;5em 1em: cursor; pointer. }:btn-1 { background; lime. }:btn-2 { background; crimson. }:btn:hover { background; gold. }:fa { color; white; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script> <div id="demo"> <div v-for="(regList, idx) in myJson":key="regList" class="container" > <table> <thead> <tr> <th>Documento</th> <th colspan="2">Avalie a Busca</th> </tr> </thead> <tbody> <tr v-for="countryList in regList[2]":key="countryList"> <td style="visibility: visible"> {{countryList}} </td> <td> <button @click="setState(idx, true)" class="btn btn-1":class="selected[idx]?.state && 'active'"> <i class="fa fa-thumbs-up"></i> </button> </td> <td> <button @click="setState(idx, false)" class="btn btn-2":class="selected[idx]?.state === false && 'active'"> <i class="fa fa-thumbs-down"></i> </button> </td> </tr> </tbody> </table> </div> {{selected}} </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM