0

In a vue component I have a autocomplete dropdown:

import AutoComplete from 'primevue/autocomplete';

defined in the template as follows:

<AutoComplete
    id="account"
    v-model="activity.account"
    :invalid="inputErrors['account']"
    :suggestions="filteredAccounts"
    class="w-full"
    dropdown
    field="name"
    forceSelection
    optionLabel="label"
    optionValue="accountID"
    placeholder="Search"
    @change="clearValidationProperty('account')"
    @complete="search"
/>

The trouble is that the account name needs more detail, i.e. to display city and state in addition to the account name, so to use a computed field instead of the name. Here is the new computed field:

const accountsWithLabel = computed(() => {
  return accounts.value.map(account => ({
    ...account,
    label: `${account?.name || ''} - ${account?.city ? account.city : ''}, ${account?.state ? account.state : ''}`.trim()
  }));
});

and the search function that uses accountsWithLabel.value:

function search(event: { query: string }) {
  setTimeout(() => {
    if (!event.query.trim().length) {
      filteredAccounts.value = [...accountsWithLabel.value];
    } else {
      filteredAccounts.value = accountsWithLabel.value.filter((account) => {
        const searchableFields: string[] = [
          account?.name || "",
          account?.address || "",
          account?.email || "",
          account?.phone || "",
          account?.territory || ""
        ];
        return searchableFields.some((field: string) => {
          return field.toLowerCase().includes(event.query.toLowerCase());
        });
      });
    }
  }, 250);
}

With this change, the dropdown is very slow and looks like this:

dropdown value showing as object Object

How to fix it?

1
  • [object Object] is a symptom of trying to force an object to be displayed as a string. For some reason label is an object in your case. I could not reproduce. There may be something specific to your accounts data or other code not shown that is causing your issue. Feel free to edit/fork my linked stackblitz to create a reproducible example using more code and/or different data. Commented May 30 at 5:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.