Related
Auto suggest uses extra spaces in the html, because of the reason the next elements like H1, H2 and paragraph are going down when I start typing in the suggestion box
App.vue
<template>
<div id="app">
<Autocomplete
:items="[
'Apple',
'Banana',
'Orange',
'Mango',
'Pear',
'Peach',
'Grape',
'Tangerine',
'Pineapple',
]"
/>
<p>p1</p>
<h1>h1</h1>
<h1>h2</h1>
</div>
</template>
<script>
import Autocomplete from "./components/AutoComplete";
import AutoCompleteSearch from "./directives/auto-complete-search.js";
import axios from "axios";
export default {
name: "App",
directives: {
AutoCompleteSearch,
},
components: {
Autocomplete,
},
data() {
return {
data: [],
};
},
methods: {
async getUser() {
await axios
.get("https://jsonplaceholder.typicode.com/users")
.then((res) => (this.data = res.data))
.catch((err) => console.log(err));
},
},
mounted() {
this.data = [{ name: "Elie" }, { name: "John" }];
},
};
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
AutocompletSearch.js -- directive
import Vue from "vue";
/**
* Add the list attribute to the bound input field.
*
* #param element
* #param binding
* #returns {HTMLElement}
*/
const addAttribute = (element, binding) => {
const { id } = binding.value;
element.setAttribute("list", id);
return element;
};
/**
* The search data for auto-completions
*
* #param binding
* #returns {Object|Array}
*/
const searchData = (binding) => {
const { data } = binding.value;
return data;
};
/**
* Construct the datalist HTMLElement
*
* #param element
* #param binding
* #returns {HTMLDataListElement}
*/
const constructDataList = (element, binding) => {
const datalist = document.createElement("datalist");
datalist.id = binding.value.id;
const data = searchData(binding);
data.forEach((item) => {
const { autoCompleteValue } = binding.value;
const option = document.createElement("option");
option.value = item[autoCompleteValue];
datalist.appendChild(option);
});
return datalist;
};
/**
* #param el the element where to bind the directive
* #param binding {id & autoCompleteValue & data}
*/
const init = (el, binding) => {
const element = addAttribute(el, binding);
const wrapper = element.parentNode;
const datalist = constructDataList(element, binding);
wrapper.appendChild(datalist);
};
export default Vue.directive("auto-complete-search", {
update(el, binding, vnode) {
const vm = vnode.context;
const bindingData = binding.value;
const componentName = vm.$options.name;
const customCallbackName = bindingData.callbackName;
console.log("component updated");
// call the function to fetch auto complete dat
vm.$options.methods.getUser();
if (!vm.$options.methods.getUser && customCallbackName === undefined)
return console.error(`the getUser() is neither defined or overriden in ${componentName} component, on
the auto-complete-search directive`);
if (binding.value.apply) {
init(el, binding);
}
if (!binding.value.apply) {
el.removeAttribute("list");
}
}
});
AutoComplete.vue
<template>
<div class="autocomplete">
<input
type="text"
#input="onChange"
v-model="search"
#keydown.down="onArrowDown"
#keydown.up="onArrowUp"
#keydown.enter="onEnter"
/>
<ul id="autocomplete-results" v-show="isOpen" class="autocomplete-results">
<li class="loading" v-if="isLoading">Loading results...</li>
<li
v-else
v-for="(result, i) in results"
:key="i"
#click="setResult(result)"
class="autocomplete-result"
:class="{ 'is-active': i === arrowCounter }"
>
{{ result }}
</li>
</ul>
</div>
</template>
<script>
export default {
name: "AutoComplete",
props: {
items: {
type: Array,
required: false,
default: () => [],
},
isAsync: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
isOpen: false,
results: [],
search: "",
isLoading: false,
arrowCounter: -1,
};
},
watch: {
items: function (value, oldValue) {
if (value.length !== oldValue.length) {
this.results = value;
this.isLoading = false;
}
},
},
mounted() {
document.addEventListener("click", this.handleClickOutside);
},
destroyed() {
document.removeEventListener("click", this.handleClickOutside);
},
methods: {
setResult(result) {
this.search = result;
this.isOpen = false;
},
filterResults() {
this.results = this.items.filter((item) => {
return item.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
});
},
onChange() {
this.$emit("input", this.search);
if (this.isAsync) {
this.isLoading = true;
} else {
this.filterResults();
this.isOpen = true;
}
},
handleClickOutside(event) {
if (!this.$el.contains(event.target)) {
this.isOpen = false;
this.arrowCounter = -1;
}
},
onArrowDown() {
if (this.arrowCounter < this.results.length) {
this.arrowCounter = this.arrowCounter + 1;
}
},
onArrowUp() {
if (this.arrowCounter > 0) {
this.arrowCounter = this.arrowCounter - 1;
}
},
onEnter() {
this.search = this.results[this.arrowCounter];
this.isOpen = false;
this.arrowCounter = -1;
},
},
};
</script>
<style>
.autocomplete {
position: relative;
}
.autocomplete-results {
padding: 0;
margin: 0;
border: 1px solid #eeeeee;
height: 120px;
overflow: auto;
}
.autocomplete-result {
list-style: none;
text-align: left;
padding: 4px 2px;
cursor: pointer;
}
.autocomplete-result.is-active,
.autocomplete-result:hover {
background-color: #4AAE9B;
color: white;
}
</style>
Please help me out this, auto suggestion can show like popup-modal instead of using extra spaces in the html. like
Here is my code link - https://codesandbox.io/s/vuejs-autocomplete-input-forked-fh31tr
After checking out your codesandbox just add these 3 css properties to your autocomplete class:
position: absolute;
width: 100%;
background: white;
I'm on Nuxt 2.15.4 and using Quagga2. I want to create a scanner like mobile scanners where the camera is full screen and there is a blue rectangle that the barcode must be placed inside of, and after scanning there will be a red line through the barcode canvas. With Quagga.js I can manipulate the CSS and area property in canvas but it cause 2 problems:
The actual scanning zone will be messed up and I don't know where the scanning exactly happens.
The successful red line-through style will be messed up.
Also, after a successful scan I need the red line-through to remain and not disappear and no scanning happen for a period of time (so I can decide to continue scanning or do something with result).
Here is the code I implemented on in app (also need help on activating torch and zoom):
<template>
<section id="container" class="container">
<div id="interactive" class="viewport"></div>
{{code}}
</section>
</template>
<script>
import Quagga from '#ericblade/quagga2';
export default {
data() {
return {
code:[]
};
},
methods:{
async checkDevice() {
let md = navigator.mediaDevices;
if (!md || !md.enumerateDevices) return false;
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.some((device) => "videoinput" === device.kind);
},
initQuagga(){
Quagga.init({
inputStream : {
name : "Live",
type : "LiveStream",
area: {
top: "0%",
right: "0%",
left: "0%",
bottom: "0%",
},
},
locate: false,
decoder : {
readers : [
"ean_reader",
],
}
},(err)=>{
if(err){
return
}
// this.checkCapabilities();
this.startQuagga()
});
},
// checkCapabilities(){
// var track = Quagga.CameraAccess.getActiveTrack();
// var capabilities = {};
// if (typeof track.getCapabilities === 'function') {
// capabilities = track.getCapabilities();
// }
// this.applySettingsVisibility('zoom', capabilities.zoom);
// this.applySettingsVisibility('torch', capabilities.torch);
// },
// applySetting: function(setting, value) {
// var track = Quagga.CameraAccess.getActiveTrack();
// if (track && typeof track.getCapabilities === 'function') {
// switch (setting) {
// case 'zoom':
// return track.applyConstraints({advanced: [{zoom: parseFloat(value)}]});
// case 'torch':
// return track.applyConstraints({advanced: [{torch: !!value}]});
// }
// }
// },
startQuagga(){
Quagga.start()
Quagga.onProcessed(function (result) {
let drawingCtx = Quagga.canvas.ctx.overlay,
drawingCanvas = Quagga.canvas.dom.overlay;
if (result) {
if (result.boxes) {
drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
result.boxes.filter(function (box) {
return box !== result.box;
}).forEach(function (box) {
Quagga.ImageDebug.drawPath(box, {x: 0, y: 1}, drawingCtx, {color: "#00F", lineWidth: 2});
});
}
if (result.box) {
Quagga.ImageDebug.drawPath(result.box, {x: 0, y: 1}, drawingCtx, {color: "#008", lineWidth: 2});
}
if (result.codeResult && result.codeResult.code) {
Quagga.ImageDebug.drawPath(result.line, {x: 'x', y: 'y'}, drawingCtx, {color: 'red', lineWidth: 3});
}
}
})
Quagga.onDetected(this.onDetected);
},
onDetected(data) {
let barCodeData = data.codeResult.code;
console.log(barCodeData);
},
},
async mounted(){
let data = await this.checkDevice();
if (data) {
this.initQuagga();
}
},
beforeDestroy(){
Quagga.stop()
}
}
</script>
<style lang="scss">
#container {
width: 640px;
margin: 20px auto;
padding: 10px;
}
#interactive.viewport {
width: 640px;
height: 480px;
}
#interactive.viewport canvas, video {
float: left;
width: 640px;
height: 480px;
}
#interactive.viewport canvas.drawingBuffer, video.drawingBuffer {
margin-left: -640px;
}
</style>
I am trying to convert from pell rich text editor to vue component. I have downloaded pell.js and converted it to vue component but I meet some issues now.
I transfer all datas and methods from pell to vue component.
And I called this.init function in created() method. And it shows that this.defaultActions which defined in datas() is not defined in init functions.
Please give me any advice. Thanks..
Here is my vue component
<template>
<div class="content">
<h1>pell</h1>
<div id="editor" class="pell"></div>
<div style="margin-top:20px;">
<h3>Text output:</h3>
<div id="text-output"></div>
</div>
<div style="margin-top:20px;">
<h3>HTML output:</h3>
<pre id="html-output"></pre>
</div>
</div>
</template>
<script>
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
export default {
data: ()=> ({
defaultParagraphSeparatorString : 'defaultParagraphSeparator',
formatBlock : 'formatBlock',
defaultActions: {
bold: {
icon: '<b>B</b>',
title: 'Bold',
state: function state() {
return this.queryCommandState('bold');
},
result: function result() {
return this.exec('bold');
}
},
italic: {
icon: '<i>I</i>',
title: 'Italic',
state: function state() {
return this.queryCommandState('italic');
},
result: function result() {
return this.exec('italic');
}
},
underline: {
icon: '<u>U</u>',
title: 'Underline',
state: function state() {
return this.queryCommandState('underline');
},
result: function result() {
return this.exec('underline');
}
},
strikethrough: {
icon: '<strike>S</strike>',
title: 'Strike-through',
state: function state() {
return this.queryCommandState('strikeThrough');
},
result: function result() {
return this.exec('strikeThrough');
}
},
heading1: {
icon: '<b>H<sub>1</sub></b>',
title: 'Heading 1',
result: function result() {
return this.exec('formatBlock', '<h1>');
}
},
heading2: {
icon: '<b>H<sub>2</sub></b>',
title: 'Heading 2',
result: function result() {
return this.exec('formatBlock', '<h2>');
}
},
paragraph: {
icon: '¶',
title: 'Paragraph',
result: function result() {
return this.exec('formatBlock', '<p>');
}
},
quote: {
icon: '“ ”',
title: 'Quote',
result: function result() {
return this.exec('formatBlock', '<blockquote>');
}
},
olist: {
icon: '#',
title: 'Ordered List',
result: function result() {
return this.exec('insertOrderedList');
}
},
ulist: {
icon: '•',
title: 'Unordered List',
result: function result() {
return this.exec('insertUnorderedList');
}
},
code: {
icon: '</>',
title: 'Code',
result: function result() {
return this.exec('formatBlock', '<pre>');
}
},
line: {
icon: '―',
title: 'Horizontal Line',
result: function result() {
return this.exec('insertHorizontalRule');
}
},
link: {
icon: '🔗',
title: 'Link',
result: function result() {
var url = window.prompt('Enter the link URL');
if (url) this.exec('createLink', url);
}
},
image: {
icon: '📷',
title: 'Image',
result: function result() {
var url = window.prompt('Enter the image URL');
if (url) this.exec('insertImage', url);
}
}
},
defaultClasses: {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
},
}),
created(){
console.log("this.defaultActions", this.defaultActions);
this.init(
{
element: document.getElementById('editor'),
defaultParagraphSeparator: 'p',
// actions: [
// 'bold',
// 'italic',
// 'underline',
// 'strikethrough'
// ],
onChange: function (html) {
document.getElementById('text-output').innerHTML = html
document.getElementById('html-output').textContent = html
}
}
);
},
methods:{
addEventListener(parent, type, listener) {
return parent.addEventListener(type, listener);
},
appendChild(parent, child) {
return parent.appendChild(child);
},
createElement(tag) {
return document.createElement(tag);
},
queryCommandState(command) {
return document.queryCommandState(command);
},
queryCommandValue(command) {
return document.queryCommandValue(command);
},
exec(command) {
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
return document.execCommand(command, false, value);
},
init(settings){
Object.keys(this.defaultActions).map(function (action) {
console.log("action", action)
});
var actions = settings.actions ? settings.actions.map(function (action) {
if (typeof action === 'string') return this.defaultActions[action];
else if (this.defaultActions[action.name]) return _extends({}, this.defaultActions[action.name], action);
return action;
}) : Object.keys(this.defaultActions).map(function (action) {
console.log("action", action)
console.log("sss", this.defaultActions)
// return this.defaultActions[action];
});
var classes = _extends({}, this.defaultClasses, settings.classes);
var defaultParagraphSeparator = settings[this.defaultParagraphSeparatorString] || 'div';
var actionbar = this.createElement('div');
actionbar.className = classes.actionbar;
this.appendChild(settings.element, actionbar);
var content = settings.element.content = this.createElement('div');
content.contentEditable = true;
content.className = classes.content;
content.oninput = function (_ref) {
var firstChild = _ref.target.firstChild;
if (firstChild && firstChild.nodeType === 3) this.exec(this.formatBlock, '<' + defaultParagraphSeparator + '>');else if (content.innerHTML === '<br>') content.innerHTML = '';
settings.onChange(content.innerHTML);
};
content.onkeydown = function (event) {
if (event.key === 'Enter' && this.queryCommandValue(this.formatBlock) === 'blockquote') {
setTimeout(function () {
return this.exec(this.formatBlock, '<' + defaultParagraphSeparator + '>');
}, 0);
}
};
this.appendChild(settings.element, content);
actions.forEach(function (action) {
var button = this.createElement('button');
button.className = classes.button;
button.innerHTML = action.icon;
button.title = action.title;
button.setAttribute('type', 'button');
button.onclick = function () {
return action.result() && content.focus();
};
if (action.state) {
var handler = function handler() {
return button.classList[action.state() ? 'add' : 'remove'](classes.selected);
};
this.addEventListener(content, 'keyup', handler);
this.addEventListener(content, 'mouseup', handler);
this.addEventListener(button, 'click', handler);
}
this.appendChild(actionbar, button);
});
if (settings.styleWithCSS) this.exec('styleWithCSS');
this.exec(this.defaultParagraphSeparatorString, defaultParagraphSeparator);
return settings.element;
}
}
}
</script>
<style>
.content {
box-sizing: border-box;
margin: 0 auto;
max-width: 600px;
padding: 20px;
}
#html-output {
white-space: pre-wrap;
}
.pell {
border: 1px solid rgba(10, 10, 10, 0.1);
box-sizing: border-box; }
.pell-content {
box-sizing: border-box;
height: 300px;
outline: 0;
overflow-y: auto;
padding: 10px; }
.pell-actionbar {
background-color: #FFF;
border-bottom: 1px solid rgba(10, 10, 10, 0.1); }
.pell-button {
background-color: transparent;
border: none;
cursor: pointer;
height: 30px;
outline: 0;
width: 30px;
vertical-align: bottom; }
.pell-button-selected {
background-color: #F0F0F0; }
</style>
You should use arrow functions in the block mapping actions, preserves this from the surrounding scope
var actions = settings.actions
? settings.actions.map(action => { // arrow function here
if (typeof action === "string") return this.defaultActions[action];
else if (this.defaultActions[action.name])
return _extends({}, this.defaultActions[action.name], action);
return action
})
: Object.keys(this.defaultActions).map(action => { // arrow function here
console.log("action", action)
console.log("sss", this.defaultActions);
// return this.defaultActions[action];
});
Just for reference, you can also use the sample Vue code given in the pell repository
(or parts thereof, e.g styles)
<template>
<div>
<h6>Editor:</h6>
<div id="pell" class="pell" />
<h6>HTML Output:</h6>
<pre id="pell-html-output"></pre>
</div>
</template>
<script>
import pell from 'pell'
export default {
methods: {
ensureHTTP: str => /^https?:\/\//.test(str) && str || `http://${str}`
},
mounted () {
pell.init({
element: document.getElementById('pell'),
onChange: html => {
window.document.getElementById('pell-html-output').textContent = html
},
actions: [
'bold', 'italic', 'underline', 'strikethrough', 'heading1', 'heading2',
'paragraph', 'quote', 'olist', 'ulist', 'code', 'line',
{
name: 'image',
result: () => {
const url = window.prompt('Enter the image URL')
if (url) pell.exec('insertImage', this.ensureHTTP(url))
}
},
{
name: 'link',
result: () => {
const url = window.prompt('Enter the link URL')
if (url) pell.exec('createLink', this.ensureHTTP(url))
}
}
]
})
}
}
</script>
<style>
.pell {
border: 2px solid #000;
border-radius: 0;
box-shadow: none;
}
#pell-html-output {
margin: 0;
white-space: pre-wrap;
}
</style>
This is DownloadExcel.vue:
<template>
<div class="hello2">
<button #click="downloadExcesl">Sffubmit</button>
</div>
</template>
<script>
import Vue from 'vue';
import JsonExcel from 'vue-json-excel';
Vue.component('downloadExcel', JsonExcel);
export default {
data() {
return {
json_fields: {
'Complete name': 'name',
'City': 'city',
'Telephone': 'phone.mobile',
'Telephone 2': {
field: 'phone.landline',
callback: (value) => {
return `Landline Phone - ${value}`;
}
},
},
json_data: [
{
'name': 'Tony Peña',
'city': 'New York',
'country': 'United States',
'birthdate': '1978-03-15',
'phone': {
'mobile': '1-541-754-3010',
'landline': '(541) 754-3010'
}
},
{
'name': 'Thessaloniki',
'city': 'Athens',
'country': 'Greece',
'birthdate': '1987-11-23',
'phone': {
'mobile': '+1 855 275 5071',
'landline': '(2741) 2621-244'
}
}
],
json_meta: [
[
{
'key': 'charset',
'value': 'utf-8'
}
]
],
}
}
}
</script>
this is App.vue:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<span>asdasdas</span>
<download-excel
:data = "json_data"
:fields = "json_fields"
worksheet = "My Worksheet"
name = "filename.xls">
Download Excel (you can customize this with html code!)
<el-button type="warning">导出excel</el-button>
</download-excel>
<img src="download_icon.png">
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import DownloadExcel from './components/DownloadExcel'
export default {
name: 'app',
components: {
HelloWorld,
DownloadExcel
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
but console shows those errors:
vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "json_data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: found in
---> <App> at src/App.vue
<Root>
https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
Same for json_fields and downloadExcesl
Please help me to solve this. How can I fix this? I searched a lot. But could not fix it.
Please help me to solve this. How can I fix this? I searched a lot. But could not fix it.
Please help me to solve this. How can I fix this? I searched a lot. But could not fix it.
This is also HelloWorld.vue:
<template>
<div class="hello">
<label>File
<input type="file" id="file" ref="file" v-on:change="handleFileUpload()"/>
</label>
<button v-on:click="submitFile()">Submit</button>
<select name="bank-dropdown" #change="onBankChange($event)" class="form-control">
<option value="ziraat">ZIRAAT</option>
<option value="2">On Demand Leave</option>
</select>
<select name="currency-dropdown" #change="onCurrencyChange($event)" class="form-control">
<option value="euro">EURO</option>
<option value="2">On Demand Leave</option>
</select>
<span id="error" style="color: red;">{{message}}</span>
<button #click="controlExcel">stsrt controll</button>
<span style="color: red;">{{temp}}</span>
<span style="color: red;">{{temp2}}</span>
<!-- <button #click="downloadExcel">download after control</button>-->
<div>
<json-excel :data="dataForExcel"></json-excel>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'HelloWorld',
props: {
msg: String
}
,
data() {
return {
dataForExcel: [
{ colA: "Hello", colB: "World" },
{
colA: "Multi-line",
/* Multi-line value: */
colB:
"This is a long paragraph\nwith multiple lines\nthat should show in a single cell."
},
{ colA: "Another", colB: "Regular cell" }
],
message: 'qq',
temp: 'wqeqwe',
temp2: 'wqeqwe2'
// ,
// json_fields: {
// 'Complete name': 'name',
// 'City': 'city',
// 'Telephone': 'phone.mobile',
// 'Telephone 2' : {
// field: 'phone.landline',
// callback: (value) => {
// return `Landline Phone - ${value}`;
// }
// },
// },
// json_data: [
// {
// 'name': 'Tony Peña',
// 'city': 'New York',
// 'country': 'United States',
// 'birthdate': '1978-03-15',
// 'phone': {
// 'mobile': '1-541-754-3010',
// 'landline': '(541) 754-3010'
// }
// },
// {
// 'name': 'Thessaloniki',
// 'city': 'Athens',
// 'country': 'Greece',
// 'birthdate': '1987-11-23',
// 'phone': {
// 'mobile': '+1 855 275 5071',
// 'landline': '(2741) 2621-244'
// }
// }
// ],
// json_meta: [
// [
// {
// 'key': 'charset',
// 'value': 'utf-8'
// }
// ]
// ],
}
},
created() {
this.bank = "ziraat";
this.currency = "euro";
// this.use(cors());
},
methods: {
onBankChange(event) {
this.bank = event.target.value;
console.log(" this.bank" + this.bank)
},
onCurrencyChange(event) {
this.currency = event.target.value;
console.log(" this.currency" + this.currency)
}
// ,
// downloadExcel(){
// // const spread = this._spread;
// // const fileName = "SalesData.xlsx";
// // // const sheet = spread.getSheet(0);
// // const excelIO = new Excel.IO();
// // const json = JSON.stringify(spread.toJSON({
// // includeBindingSource: true,
// // columnHeadersAsFrozenRows: true,
// // }));
// // excelIO.save(json, function(_blob_){
// // saveAs(blob, fileName);
// // }, function (_e_) {
// // console.log(e)
// // });
//
// }
,
controlExcel() {
const self = this;
// axios.post('http://localhost:8082/control-excel', {}, {
// responseType: 'blob'
// }).then(res => {
// let blob = new Blob([res.data], {type: 'application/ms-excel;charset=utf-8'});
// let downloadElement = document.createElement('a');
// let href = window.URL.createObjectURL(blob); //创建下载的链接
// downloadElement.href = href;
// downloadElement.download = 'forbidden-words.xls'; //下载后文件名
// document.body.appendChild(downloadElement);
// downloadElement.click(); //点击下载
// document.body.removeChild(downloadElement); //下载完成移除元素
// window.URL.revokeObjectURL(href); //释放掉blob对象
// })
this.request = {
bank: this.bank,
cellValuesOfAllExcel: this.cellValuesOfAllExcel,
currency: this.currency
};
axios.post('http://localhost:8082/control-excel/',
this.request,
).then((response) => {
console.log('SUCCESS!! + response', response);
self.message = "successfully controlled and checked. please download"
debugger;
this.cellValuesOfAllExcelAfterControl = response.data;
this.temp = this.cellValuesOfAllExcelAfterControl;
console.log('SUCCESS!! + this.cellValuesOfAllExcelAfterControl', this.cellValuesOfAllExcelAfterControl);
})
.catch(function (response) {
console.log('FAILURE!!+ response', response);
self.message = "errorly uploaded"
});
}
,
handleFileUpload() {
this.file = this.$refs.file.files[0];
console.log("this.file", this.file)
},
submitFile() {
const self = this;
let formData = new FormData();
/*
Add the form data we need to submit
*/
formData.append('file', this.file); //todo get "file" string from external
/*
Make the request to the POST /single-file URL
*/
axios.post('http://localhost:8082/import/'
+ this.bank
+ '/'
+ this.currency,
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then((response) => {
console.log('SUCCESS!! + response', response);
self.message = "successfully uploaded please start control process"
debugger;
this.cellValuesOfAllExcel = response.data;
this.temp = this.cellValuesOfAllExcel;
console.log('SUCCESS!! + this.cellValuesOfAllExcel', this.cellValuesOfAllExcel);
})
.catch(function (response) {
console.log('FAILURE!!+ response', response);
self.message = "errorly uploaded"
});
}
}}
</script>
<!--<!– Add "scoped" attribute to limit CSS to this component only –>-->
<!--<style scoped>-->
<!--h3 {-->
<!-- margin: 40px 0 0;-->
<!--}-->
<!--ul {-->
<!-- list-style-type: none;-->
<!-- padding: 0;-->
<!--}-->
<!--li {-->
<!-- display: inline-block;-->
<!-- margin: 0 10px;-->
<!--}-->
<!--a {-->
<!-- color: #42b983;-->
<!--}-->
<!--</style>-->
json_data and json_fields are both declared in your DownloadExcel.vue component data, not in your App.vue. Just remove it from the template declaration:
<!-- App.vue -->
<download-excel>
<!-- ... -->
</download-excel>
Or move it to App.vue
If you wish to pass both fields from the parent to the child, just declare it as a prop
I recommend you taking a look on how vue data flow works
I loop over an array and display a list of radio buttons. I am trying to disable all other radio inputs except for the one selected after one is clicked. All I have been able to manage is to disable all of the radios or the one that is clicked.
I was able to accomplish dynamically adding a class in the v-for loop, so I tried disabling the inputs in the same manner, but everything gets disabled:
:disabled="{'disabled':answer.answerID == isChecked}"
I also tried using a method, but didn't have any luck there either:
/* List data */
"answers":[
{
"answerID": "1",
"answerName": "Blueberries"
},
{
"answerID": "2",
"answerName": "Apples"
},
{
"answerID": "3",
"answerName": "Bananas"
},
{
"answerID": "4",
"answerName": "Pineapple"
},
{
"answerID": "5",
"answerName": "Strawberries"
}
]
/* Component code */
<template>
<input
v-for="(answer, index) in answers"
:key="index"
type="radio"
class="mg-input mg-answer"
ref="mgAnswer"
name="mg-answer"
:value="answer.answerName"
v-model="answerVal"
:disabled="disableAnswer(answers, index)"
:class="{'mg-checked':answer.answerID == isChecked}"
#click="
isChecked = answer.answerID
checkAnswer(answers, index, $event)" />
</template>
<script>
export default {
data: function () {
return {
// Class definer for is checked or not
isChecked: undefined,
// Answer Data Properties
answerVal: '',
checkedAnswerID: '',
checkedAnswerElem: '',
}
},
methods: {
checkAnswer: function (arr, i, event) {
let mgAnswer = this.$refs.mgAnswer
this.checkedAnswerID = arr[i].answerID
this.checkedAnswerElem = mgAnswer[i]
if (mgAnswer[i].answerName !== this.answerVal) {
this.answerVal = ''
}
},
disableAnswer: function (arr, i) {
if (arr[i].answerName !== this.answerVal) {
return true
}
}
}
}
</script>
disableAnswer(answers, index) is evaluated only once upon rendering. Initially, answerVal is null, as no answers have been selected yet, so disableAnswer returns true, causing all radio buttons to be disabled immediately.
A quick fix is to add answerVal as a function argument (i.e., disableAnswer(answers, index, answerVal)), so that the function is re-evaluated when answerVal changes. We have to also modify disableAnswer to ignore null values of answerVal, which would occur at initialization:
disableAnswer(arr, i, answerVal) {
if (!answerVal) {
// not yet set
return;
}
if (arr[i].answerName !== answerVal) {
return true
}
}
new Vue({
el: '#app',
data() {
return {
// Class definer for is checked or not
isChecked: undefined,
// Answer Data Properties
answerVal: '',
checkedAnswerID: '',
checkedAnswerElem: '',
"answers":[
{
"answerID": "1",
"answerName": "Blueberries"
},
{
"answerID": "2",
"answerName": "Apples"
},
{
"answerID": "3",
"answerName": "Bananas"
},
{
"answerID": "4",
"answerName": "Pineapple"
},
{
"answerID": "5",
"answerName": "Strawberries"
}
]
};
},
methods: {
checkAnswer: function (arr, i, event) {
let mgAnswer = this.$refs.mgAnswer
this.checkedAnswerID = arr[i].answerID
this.checkedAnswerElem = mgAnswer[i]
if (mgAnswer[i].answerName !== this.answerVal) {
this.answerVal = ''
}
},
disableAnswer: function (arr, i, answerVal) {
if (!answerVal) return;
if (arr[i].answerName !== answerVal) {
return true
}
}
}
})
input {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border-radius: 50%;
width: 16px;
height: 16px;
border: 2px solid #999;
transition: 0.2s all linear;
outline: none;
margin-right: 5px;
position: relative;
top: 4px;
}
input[disabled] {
border: 1px solid #ccc;
}
.mg-checked {
border: 6px solid black;
}
<script src="https://unpkg.com/vue#2.5.17"></script>
<div id="app">
<input
v-for="(answer, index) in answers"
:key="index"
type="radio"
class="mg-input mg-answer"
ref="mgAnswer"
name="mg-answer"
:value="answer.answerName"
v-model="answerVal"
:disabled="disableAnswer(answers, index, answerVal)"
:class="{'mg-checked':answer.answerID == isChecked}"
#click="
isChecked = answer.answerID
checkAnswer(answers, index, $event)" />
{{answerVal}}
</div>
You might also find useful a minor refactoring of your code: demo
You can do something like this,
check if input is clicked or if not clicked in that case make disabled set to false.
<input :disabled="disableAnswer(answer)" />
Js
disableAnswer : function(answer){
if(this.answerVal=="" ||this.answerVal==answer.answerName) {
return false;
} else {
return true;
}
}
Added Snippet:
function callMe(){
var vm = new Vue({
el : '#root',
data : {
answerVal:"",
answers:[
{
"answerID": "1",
"answerName": "Blueberries"
},
{
"answerID": "2",
"answerName": "Apples"
},
{
"answerID": "3",
"answerName": "Bananas"
},
{
"answerID": "4",
"answerName": "Pineapple"
},
{
"answerID": "5",
"answerName": "Strawberries"
}
]
},
methods: {
disableAnswer(item){
if(this.answerVal=="" ||this.answerVal==item) {
return false;
} else {
return true;
}
}
}
})
}
callMe();
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.11/dist/vue.js"></script>
<div id='root'>
<input
v-for="(answer, index) in answers"
:key="index"
type="radio"
class="mg-input mg-answer"
ref="mgAnswer"
name="mg-answer"
:value="answer.answerName"
v-model="answerVal"
:disabled="disableAnswer(answer.answerName)"
/>
</div>