I've been stuck with the same problem for 2 days. now I'm working how to display a value from database which is many to many relation.
the case now I need to search bill no from bill table in database which i have done it but i'm stuck with displaying the value which the value of bill no from bill table, collection name from collection table, organization name from organization table relate account id in account table relate to bill table in account id, and need to display customer name full name(first+last).
this my billsController
public function actionLoadRequest($order_number)
{
if (!Yii::$app->request->isAjax) {
return $this->asJson([
"status" => false,
"message" => "Unknown request method.",
"data" => []
]);
}
$data = Bill::find()->where(['bill_no'=>$order_number])->one();
return $this->asJson([
"status" => true,
"message" => "",
"data" => $data
]);
}
this is my view
<template v-if="isLoading">
<tr>
<td colspan="7" class="center aligned">Processing...</td>
</tr>
</template>
<template v-else>
<template v-if="data.length > 0">
<tr v-for="$bill in data">
<td>{{bill.bill_no}}</td>
<td>{{bill.collection}}</td>
<td>{{bill.organization}}</td>
<td>{{account.first_name + " " + account.last_name}}</td>
<td>{{bill.currency + " " +
bill.Amount}}
</td>
</tr>
</template>
<template v-if="data.length === 0">
<tr>
<td colspan="7" class="center aligned">No request data.</td>
</tr>
</template>
</template>
this is my javascript
methods: {
loadRequest: function () {
console.log(this.bill_no);
if (this.bill_no === "") {
alert("Nothing to search. Please enter bill number.");
return;
}
let self = this;
self.isLoading = true;
$.ajax({
url: "<?= Url::to(["/admin/bills/load-request"]) ?>",
method: 'GET',
type: 'GET',
dataType: 'json',
data: {order_number: this.bill_no}
}).done(function (data) {
if (data.status) {
self.data = data.data;
} else {
alert(data.message);
self.data = [];
}
console.log(data);
self.isLoading = false;
}).fail(function (jqXHR) {
if (jqXHR.responseJSON) {
alert("Nothing to search. Please enter bill number.");
} else {
alert("Nothing to search. Please enter bill number.");
}
self.isLoading = false;
});
},
},
Edit-----------------------------
I have solve my problem for displaying data from database by change the code in my controller
$data = Bill::find()
->where(['bill_no'=>$bill_number])
->asArray()
->all();
but I have a problem with a relationship as I still new with vue js.
My suggestion is that using of Pjax. In Pjax you have history of the AJAX events without refreshing the page.
And too, you can have clean code in your Action, View and JS file.
You can see help full in link : Yii2 Pjax Tutorial
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments