How to load image + code into dropdown from database Laravel 5.7 - laravel

I want to populate stored images from database to fill dropdown the images are in airline_images folder the image path is stored in database i want user when click on the airlines dropdown it will show the stored airliens with its images
here is the blade code :
<div class="form-group col-md-2">
<label>#lang('site.airline')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="airline"
id="airline">
#foreach ($airlines as $airline)
<option value="{{ $airline->code }}">{{ $airline->name }} <img src="{{ $airline->image_path }}" style="width: 100px" class="img-thumbnail" alt="">
</option>
#endforeach
</select>
</div>

You may consider using 3rd party jQuery library like ddSlick
Usage
$('select#airline').ddslick({
data: ddData,
width: '100%',
imagePosition: "left",
selectText: "Select your favorite airline",
onSelected: function (data) {
console.log(data);
}
});
PS : The plugin requires specific data format
var ddData = [
{
text: "Tunisair",
value: 1,
selected: false,
description: "Description goes here",
imageSrc: "http://path_to_img"
},
{
text: "Turkish airlines",
value: 2,
selected: true,
description: "Description goes here",
imageSrc: "http://path_to_img"
},
{
text: "Lufthansa",
value: 3,
selected: false,
description: "Description goes here",
imageSrc: "http://path_to_img"
},
//...
];

Related

How to write an array in Alpine.js

I have the following code which should loop over an array to populate an HTML select element but getting zero data?
<div class="w-full" x-data="{classes: [
{
room: 'online',
id: 1,
},
{
room: 'in class',
id: 2,
}
]}">
<select name="" id="">
<template x-for="class in classes" :key="class.id">
<option x-text="class.room" :value="class.room"></option>
</template>
</select>
</div>
Please check this. I renamed class to cls (may be class word is conflicting though did not see any error in concole)
https://codepen.io/rajdeepdebnath/pen/MWyNoKj?editors=1010
<div class="w-full" x-data="{classes: [
{
room: 'online',
id: 1,
},
{
room: 'in class',
id: 2,
}
]}">
<select name="" id="">
<template x-for="cls in classes" :key="cls.id">
<option x-text="cls.room" :value="cls.room"></option>
</template>
</select>
</div>

How to show image preview when uploading an image using Vue js?

I'm trying to show a preview image when a user uploads an image. For some reason my code works when I use an <input> with regular Bootstrap class of form-control, but when I use Bootstrap-Vue's <b-form-file> I get an error: Error in render: "TypeError: Cannot read property 'name' of undefined"
Data:
data() {
return {
options: [
{ text: "Full Time", value: "Full Time" },
{ text: "Part Time", value: "Part Time" },
],
profileData: {
photo: '',
employment_type: "Full Time",
date_of_birth: "",
experience: "",
skills: "",
},
};
},
my method:
methods: {
attachImage() {
this.profileData.photo = this.$refs.profilePhoto.files[0];
let reader = new FileReader();
reader.addEventListener('load', function() {
this.$refs.profilePhotoDisplay.src = reader.result;
}.bind(this), false);
reader.readAsDataURL(this.profileData.photo);
},
}
Bootstrap Code, this code works:
<div role="group" class="form-group">
<label for="photo" class="d-block">Upload Profile Photo (optional)</label>
<div v-if="profileData.photo.name">
<img src="" ref="profilePhotoDisplay" class="w-100px" />
</div>
<input
id="photo"
v-on:change="attachImage"
ref="profilePhoto"
type="file"
class="form-control"
>
</div>
Bootstrap-Vue Code, this gives me the error:
<div v-if="profileData.photo.name">
<img src="" ref="profilePhotoDisplay" width="140" />
</div>
<b-form-group label="Upload Profile Photo (optional)" label-for="image">
<b-form-file
id="photo"
v-on:change="attachImage"
ref="profilePhoto"
type="file"
></b-form-file>
</b-form-group>

How to empty input fields from a pop-up window after submitting - Vue - laravel?

My page exist of a table where I can add new rows. If you want to add a new row a pop-up window appear where the new values can be added.
This new data is then saved to the database after submitting. If I again want to add a new row the input fields, they should be cleared.
The method I use, is working but isn't very clear.
Note: My code shows only a part of the input fields, to make it more clear. My pop-up window actually contains 20 input fields.
I would like to clear them all at once instead of clearing them one by one (like I am doing now).
Because I am already doing this for defining the v-model, pushing the new data to the database directly on the page and via post axios request.
Is there a cleaner way to do this?
Thanks for any input you could give me.
This is my code:
html part
<div class="col-2 md-2">
<button class="btn btn-success btn-sx" #click="showModal('add')">Add New</button>
<b-modal :ref="'add'" hide-footer title="Add new" size="lg">
<div class="row" >
<div class="col-4">
<b-form-group label="Category">
<b-form-input type="text" v-model="newCategory"></b-form-input>
</b-form-group>
</div>
<div class="col-4">
<b-form-group label="Name">
<b-form-input type="text" v-model="newName" placeholder="cd4"></b-form-input>
</b-form-group>
</div>
<div class="col-4">
<b-form-group label="Amount">
<b-form-input type="number" v-model="newAmount" ></b-form-input>
</b-form-group>
</div>
</div>
<div class="row" >
<div class="col-8">
</div>
<div class="col-4">
<div class="mt-2">
<b-button #click="hideModal('add')">Close</b-button>
<b-button #click="storeAntibody(antibodies.item)" variant="success">Save New Antibody</b-button>
</div>
</div>
</div>
</b-modal>
</div>
js part
<script>
import { async } from 'q';
export default {
props: ['speciedata'],
data() {
return {
species: this.speciedata,
newCategory: '',
newName: '',
newAmount:'',
}
},
computed: {
},
mounted () {
},
methods: {
showModal: function() {
this.$refs["add"].show()
},
hideModal: function(id, expId) {
this.$refs['add'].hide()
},
addRow: function(){
this.species.push({
category: this.newCategory,
name: this.newName,
amount: this.newAmount,
})
},
storeSpecie: async function() {
axios.post('/specie/store', {
category: this.newCategory,
name: this.newName,
amount: this.newAmount,
})
.then(this.addRow())
// Clear input
.then(
this.newName = '',
this.newCategory = '',
this.newAmount = '',
)
.then(this.hideModal('add'))
},
}
}
</script>
in your data of vuejs app , you have to set one object for displaying modal data like modalData then to reset data you can create one function and set default value by checking type of value using loop through modalData object keys
var app = new Vue({
el: '#app',
data: {
message:"Hi there",
modalData:{
key1:"value1",
key2:"value2",
key3:"value3",
key4:5,
key5:true,
key6:"val6"
}
},
methods: {
resetModalData: function(){
let stringDefault="";
let numberDefault=0;
let booleanDefault=false;
Object.keys(this.modalData).forEach(key => {
if(typeof(this.modalData[key])==="number"){
this.modalData[key]=numberDefault;
}else if(typeof(this.modalData[key])==="boolean") {
this.modalData[key]=booleanDefault;
}else{
// default type string
this.modalData[key]=stringDefault;
}
});
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app">
{{modalData}}
<br/>
<button #click="resetModalData">Reset Modal Data</button>
</div>
update : in your case :
data:{
species: this.speciedata,
modalData:{
newCategory: '',
newName: '',
newAmount:''
}
},
and after storing data :
storeSpecie: async function() {
axios.post('/specie/store', {
category: this.newCategory,
name: this.newName,
amount: this.newAmount,
})
.then(()=>{
this.addRow();
this.resetModalData();
this.hideModal('add')
}
},
In native Javascript you get the reset() method.
Here is how it is used :
document.getElementById("myForm").reset();
It will clear every input in the form.

How can I set option value selected in v-select in vue.js 2?

I am trying to set option selected where option value is 1. But I am getting in trouble when using v-select from vuejs. This is how I am trying to do -
<v-select name="branchid" v-model="branchid"
:options="branches.map(branches => ({label: branches.label, value: branches.value}))"
:selected="branches.value === 1"></v-select>
Would someone help me please to get the option value selected when option value is 1?
I've put together a simplified version of what (I think) you're trying to do:
<template>
<div>
<div>
<select v-on:change="select($event);" value="branchid">
<option disabled value="">Please select one</option>
<option :selected="branchid === 1">1</option>
<option :selected="branchid === 2">2</option>
<option :selected="branchid === 3">3</option>
</select>
<span>Selected: {{ branchid }}</span>
<button v-on:click="selectOne">Select Option 1</button>
</div>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
branchid: 0,
branchidTwo: 1
};
},
methods: {
select: function(evt) {
this.branchid = evt.target.value;
},
selectOne: function() {
this.branchid = 1;
}
}
};
</script>
This does not use the v-model pattern. The docs explicitly state that if you use v-model, the class itself will be used as the source of truth rather than value or selected. You'll see I've added a button that will set the selected option on the select component.
Hope that helps.
Initialize it with select v-model with the value you want selected
new Vue({
el: '#example',
data: {
selected: 'A',
options: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="example">
<select v-model="selected">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
<span>Selected: {{ selected }}</span>
</div>
Source : https://v2.vuejs.org/v2/guide/forms.html#Select

Meteor: use dynamic parameters in a template call

In my Meteor app i have the TAPi18n package and aldeed:autoform. Im trying to make a form validation using i18n for the placeholder field but i don't know how to do it.
So, I was wondering if it is possible to pass a dynamic parameter (that's how I call it) to a template, using Spacebars. I mean, without using helpers in the JS files, something like this:
<template name="Publish">
<div class="col-md-8 col-lg-8 col-md-offset-2 col-lg-offset-2 well">
{{#autoForm collection="Publications" id="insertPublicationForm" type="insert"}}
<fieldset>
<legend>{{_ "publish_title"}}</legend>
<div class="col-md-12 col-lg-12">
<div class="form-group">
{{> afFieldInput name='name' placeholder={{_ "name"}} }}
</div>
<div class="form-group">
{{> afFieldInput name='description' placeholder={{_ "description"}} }}
</div>
<div class="form-group">
{{> afFieldInput name='price' placeholder={{_ "price"}} }}
</div>
</div>
<div class="form-group">
<button type="submit" id="add_publication" class="btn btn-success center-block">{{_ "publish"}}</button>
</div>
</fieldset>
{{/autoForm}}
</div>
</template>
I could register a helper for every translation but i don't like too much the idea.
I also know that i could use the label field in the SimpleSchema, like this:
Meteor.startup(function() {
Publications.attachSchema(new SimpleSchema({
name: {
type: String,
label: TAPi18n.__("name"),
max: 200
},
description: {
type: String,
label: TAPi18n.__("description")
},
price: {
type: Number,
label: TAPi18n.__("price"),
min: 0
}
}));
});
And then use the afQuickField template instead of afFieldInput.
But i don't want to use a label, i want to use the placeholder of the input.
There is any way to do this?
Well, i don't know why i didn't see it before, but i can do this in the SimpleSchema:
Meteor.startup(function() {
Publications.attachSchema(new SimpleSchema({
name: {
type: String,
label: TAPi18n.__("name"),
max: 200,
autoform: {
afFieldInput: {
placeholder: TAPi18n.__("name")
}
}
},
description: {
type: String,
autoform: {
afFieldInput: {
placeholder: TAPi18n.__("description")
}
}
},
price: {
type: Number,
min: 0,
autoform: {
afFieldInput: {
placeholder: TAPi18n.__("price")
}
}
}
}));
});
That way i can use i18n in the placeholder without making tons of helpers.
Sorry if I made someone waste time on this.

Resources