我有這個過濾器功能(屬于 List 組件),然后我需要將結果傳遞給另一個組件(它們是兄弟組件)來填充我的 DataTable(父組件):
async filtroVariaveis () {
try {
this.loading = true;
this.variaveis = await this.variaveisService.listVariables();
let idGrupos = null;
if (!this.grupos[0] && this.maquinaSelecionada[0]) {
idGrupos = this.gruposOpcoes.map(m => m.id);
}
if (!this.grupos[0] && !this.maquinaSelecionada[0] && this.areaSelecionada[0]) {
const idMaquinas = this.maquinasOpcoes.map(m => m.id);
const grupo = this.gruposOpcoes.filter(a => idMaquinas.includes(a.id_maquina));
idGrupos = grupo.map(a => a.id);
}
if (this.grupos[0]) {
idGrupos = this.grupos.map(g => g.id);
}
const tipos = this.tipoVariavel.map(t => t === "Analógica" ? t = "ANALOGICA" : "DIGITAL");
const tag = this.tagInput || this.variaveis.map(t => t.tag);
const descricao = this.descricaoInput || this.variaveis.map(d => d.descricao);
this.filtroAplicado = ((await this.variaveisService.listVariables(idGrupos, tipos, tag, descricao)) || []);
} catch (err) {
console.log(err);
}
this.variaveisService 是我的 Axios 服務,它與我的 API 進行通信。
我的串列組件:
<template>
<two-cards>
<template #header>
<h4 class="card-title">Filtros</h4>
<slot>
<router-link
to="/variaveis/add"
class="btn btn-success text-white align-self-center"
>
Adicionar variável
</router-link>
</slot>
</template>
<template #one>
<form class="mt-2" @submit.prevent="filtroVariaveis">
<div class="form-row">
<div class="col form-group col-sm-6">
<label for="data-final">Tag: </label>
<b-form-input
v-model="tagInput"
id="data-final"
input-class="bg-white"
></b-form-input>
</div>
<div class="col form-group col-sm-6">
<label for="data-inicial">Descri??o: </label>
<b-form-input
v-model="descricaoInput"
id="data-inicial"
input-class="bg-white"
></b-form-input>
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-3">
<label class="mt-2">área </label>
<vue-multi-select
class="multi-100"
v-model="areaSelecionada"
@input="checarArea"
search
historyButton
:options="{ multi: false, labelName: 'nome' }"
:selectOptions="areasOpcoes"
:btnLabel="() => areaSelecionada[0] ? areaSelecionada[0].nome : 'Selecione'"
/>
</div>
<div class="form-group col-sm-3">
<label class="mt-2">Máquina </label>
<vue-multi-select
class="multi-100"
v-model="maquinaSelecionada"
:disabled="!areaSelecionada[0]"
@input="checarMaquina"
search
historyButton
:options="{ multi: true, labelName: 'nome' }"
:selectOptions="maquinasOpcoes"
:btnLabel="() => customLabel(maquinaSelecionada)"
/>
</div>
<div class="form-group col-sm-3">
<label class="mt-2">Grupo </label>
<vue-multi-select
class="multi-100"
v-model="grupos"
search
historyButton
:options="{ multi: true }"
:selectOptions="gruposOpcoes"
:btnLabel="() => customLabel(grupos)"
/>
</div>
<div class="form-group col-sm-3">
<label class="mt-2">Tipo</label>
<vue-multi-select
class="multi-100"
v-model="tipoVariavel"
search
historyButton
:options="{ multi: true }"
:selectOptions="tiposOpcoes"
:btnLabel="() => customLabel(tipoVariavel)"
/>
</div>
</div>
<div class="d-flex justify-content-end tcs-card-footer-action">
<button
class="btn btn-success tcs-btn"
type="search"
>
<SearchIcon />
</button>
</div>
</form>
</template>
<template #two>
<GenericList
title="variáveis"
:linhas="linhas"
data-table-state-name="Variável"
add-route="add_variavel"
edit-route="edit_variavel"
:cols="['#', 'Tag', 'Descri??o', 'Tipo']"
:cols-name="['id', 'tag', 'descricao', 'tipo']"
:cols-map="i => [i.id, i.tag, i.descricao, i.tipo]"
/>
</template>
</two-cards>
</template>
computed: {
linhas () {
return this.lista.map((row) => ({
href: { name: this.editRoute, params: { id: row.id }},
cols: this.colsMap(row)
}));
}
},
我的 GenericList 組件,我需要在其中放置這些資料:
<template>
<div>
<DataTable
scroll
:async="pagination"
:loading="loading"
:colunas="parsedCols"
:linhas="linhas"
:errMsg="errMsg"
:state="dataTableState"
@state-change="setStateDataTable"
>
<template #results-page v-if="pagination">
<select
class="custom-select ml-1"
:value="results"
@input="changeResults"
data-cy="resultados-pagina"
>
<option value="10">10 por página</option>
<option value="20">20 por página</option>
<option value="50">50 por página</option>
<option value="100">100 por página</option>
<option v-if="canShowAllResults" value="-1">
Mostrar todos
</option>
</select>
</template>
<template v-for="col in cols" v-slot:[col]="{ value, item }">
<slot :name="col" :value="value" :item="item">
<router-link
v-if="value && value.rota && value.nome && value.id"
:to="{ name: value.rota, params: { id: value.id } }"
title="Ir para a edi??o"
>
<edit-icon size="16" /> {{ value.nome }}</router-link
>
</slot>
</template>
</DataTable>
<Paginator
:page="page"
:pages="pages"
:count="count"
:disabled="loading"
:first="first"
:last="last"
@paginate="paginate"
/>
</div>
</template>
props: {
loading: Boolean,
title: {
type: String,
required: true
},
addRoute: {
type: String,
required: true
},
editRoute: {
type: String,
required: true
},
dataTableStateName: {
type: String,
required: true
},
lista: {
type: Array
},
linhas: {
type: Array
},
cols: {
type: Array,
required: true
},
colsMap: {
type: Function,
required: true
},
colsName: {
type: Array,
required: true
},
canShowAllResults: {
type: Boolean,
default: false
},
showInativos: {
type: Boolean,
default: false
}
},
data () {
return {
errMsg: "",
pagination: false,
page: 0,
pages: 1,
results: 20,
first: 0,
last: 0,
count: 0
};
},
computed: {
dataTableState () {
return this.$store.state.dataTables[
"dataTable" this.dataTableStateName
];
},
parsedCols () {
return this.cols.map((c, idx) => this.colsName[idx] ? c : { value: c, sortable: false }
);
}
},
我不知道如何填寫這個資料表,有人可以幫助我嗎?
uj5u.com熱心網友回復:
您可以使用$emit和$on在同級組件之間傳遞資料。請看這個例子
當您將資料傳遞到另一個組件時,您必須設定這一行:
this.$root.$emit('eventing', data);
之后,您可以像這個示例一樣將資料接收到目標組件中:
1)
mounted() {
this.$root.$on('eventing',this.functionName);
}
或者
2)
mounted() {
this.$root.$on('eventing', data => {
console.log(data);
});
}
請注意,最好設定$off成目標組件
1)
beforeDestroy() {
this.$root.$off("eventing", this.functionName);
}
或者
2)
beforeDestroy() {
this.$root.$off("eventing");
}
你可以在引數中找到你需要的資料 this.functionName
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/327507.html
標籤:javascript 节点.js Vue.js 公理
