如何在数据加载到属性中时移除子字符串?

huangapple go评论50阅读模式
英文:

How do you remove a subsring when data is loaded into a prop?

问题

我正在使用Bootstrap表格显示一些存储在数据库中的错误消息。我使用Axios从数据库中获取这些消息。在表格行中显示错误消息时,我使用子字符串将输出最小化为30个字符,因为它们通常超过1000个字符。然后,我有一个模态组件,它将数组作为prop,并在单击表格中特定消息时输出相同的错误消息。

问题在于,我不希望在单击表格中的消息之一时,模态框显示子字符串。我希望在模态框中弹出消息时不带子字符串,同时保持它在表格中,以便用户在单击子字符串消息时能够看到完整的消息。

我该如何实现这一点?

英文:

I am using bootstrap table to display some error messages that are stored in a database. I fetch them with Axios.
When showing the error messages in the table row, I use a substring to minimize the output to 30 characters, as they can often be over 1000 characters long.
Then I have a modal component that takes in the array as a prop and output the same error message when you click on a specific message in the table.

The problem is that I do not want the modal to show the substring when I click on one of the messages in the table. I would like the message to pop up in the modal WITHOUT the substring while still keeping it in the table, so that the user is able to see the full message when click on the substringed message.

How can I accomplish this?

Parent:

<template>
  <b-container>
    <b-card class="mt-4">
      <h5>{{ $t('events') }}</h5>
      <b-table
        :items="errors"
        :fields="fields"
        :per-page="[5, 10]"
        selectable
        :select-mode="'single'"
        @row-selected="onRowSelected"
        @row-clicked="showModal"
        sort-desc
      />
    </b-card>
    <error-log-entry-modal ref="errorLogEntryModal" :selected-error-log="selectedRows"/>
    

  </b-container>
</template>

<script>
  import {axiosService} from '@/services/error';
  import ErrorLogEntryModal from '@/components/error-log/ErrorLogEntryModal';

  export default {
    components: {
      ErrorLogEntryModal,

    },

    data() {
      return {
        errors: null,
        selectedRows: []

      };
    },
    computed: {
      fields() {
        return [
          {
            key: 'errorMessage',
            label: this.$t('message'),
            sortable: true
          },
        ]
      },

    },
    methods: {
      load(){
        if
          errorService.getErrorLogs().then(result => {
            result.data.forEach(log => log.errorMessage = log.errorMessage.substring(0,30));
            this.errors = result.data

          })
  
      },
      onRowSelected(fields){
        this.selectedRows = fields
      },

      showModal(){
        if (this.selectedRows) {
          this.$refs.errorLogEntryModal.show()
        }
      },
    },
    created() {
      this.load()
    }
  };
</script>

child:

<template>
  <b-modal
    modal-class="error-log-modal"
    v-model="showModal"
    size="xl"
    title="Error Log">
    <b-col class="lg-12" v-for="log in selectedErrorLog">
      {{ log.errorMessage }}
    </b-col>
  </b-modal>
</template>

<script>

  export default {
    props: {
      selectedErrorLog: Array
    },
    data() {
      return {
        showModal: false,
      };
    },
    methods: {
      show(){
        this.showModal = true
      }
    }
  };
</script>

答案1

得分: 3

以下是翻译好的内容:

似乎你想要撤销将 errorMessage 设为其初始值的子字符串。但这是不可能的,一旦数据丢失,就无法恢复。

如果你想在表格中显示缩短的消息,但在模态框中显示完整的消息,你可以在另一个属性中存储缩短的消息,而不是覆盖 errorMessage 属性:

errorService.getErrorLogs().then(result => {
    result.data.forEach(log => log.errorMessageShort = log.errorMessage.substring(0, 30));
    this.errors = result.data;
})

然后你可以在表格中使用 errorMessageShort,在模态框中使用 errorMessage

英文:

Sounds like you want to undo making errorMessage a substring of its initial value. But that is not possible, once data is gone, it is lost.

If you want to show a shortened message in the table, but the full message in the modal, you can just store the shortened message in another property instead of overriding the errorMessage property:

          errorService.getErrorLogs().then(result => {
            result.data.forEach(log => log.errorMessageShort = log.errorMessage.substring(0,30));
            this.errors = result.data
          })

Then you can use errorMessageShort in the table and errorMessage in the modal.

答案2

得分: 0

不需要在修剪后重新分配错误消息。您可以保留错误消息不变,在渲染时仅显示前30个字符,但当将此字符串作为属性传递时,它将作为整个值传递。

我的意思是-

移除这段代码-

log.errorMessage = log.errorMessage.substring(0,30));

并简单地保留-

this.errors = result.data

在渲染时,像这样显示错误消息-

<div v-for="error in errors">{{ error.substring(0, 30) }}</div>

当您需要将错误传递给模态组件时,可以按原样传递,因为它实际上没有修剪。

<Modal :error="error" />

这种方法更像是在修剪和完整文本之间切换。通过这样做,您无需使用多个变量单独存储修剪过的错误和完整错误。

英文:

You don't need to re-assign the error message after trimming that. You can keep the error message as it is, and at the time of rendering, display only 30 characters but when you will pass this string as a prop, it will be passed as a whole value.

My meaning is-

Remove this code-

log.errorMessage = log.errorMessage.substring(0,30));

and simply keep-

this.errors = result.data

At the time of rendering, show the error message like this-

&lt;div v-for=&quot;error in errors&quot;&gt;{{ error.substring(0, 30) }}&lt;/div&gt;

And when you need to pass the error to the modal component, you can pass it as it is because it didn't trim actually.

&lt;Modal :error=&quot;error&quot; /&gt;

This approach is moreover like toggling between trimmed and full text. By doing this, you don't need to use multiple variables to store trimmed errors and full errors individually.

huangapple
  • 本文由 发表于 2023年2月16日 16:20:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定