我正在使用phonegap中的条形码扫描仪插件扫描条形码。获取值后,我想在文本字段中显示。以下是我的文本字段代码:
{
xtype : 'textfield',
id : 'barcodetextfield',
width : 300,
margin : '0 0 0 10',
labelWidth : '40%',
label : 'Enter Barcode'
}
在控制器中,我正在尝试在文本字段中设置条形码值。
window.plugins.barcodeScanner.scan(function(result) {
this.getBarcodebox.setValue(""+result.text);
}, function(error) {
alert("Scanning failed: " + error);
});
我将ref放在控制器中,如下所示:
barcodebox : '#barcodetextfield',
这是我的整个控制器代码:::
Ext.define('MyApp.controller.MyController', {
extend : 'Ext.app.Controller',
config : {
refs : {
TitlePanel : 'login',
dashboardpanel : 'dashboard',
ConsumerSignup : 'consumersignup',
AddtoWishlog : 'addwishlog',
wishlogsummary : 'wishlogsummarylist',
FeedbackSummary : 'feedbacksummarylist',
ConsumerSignin : 'Consumersignin',
barcodebox : '#barcodetextfield',
labelid : 'title'
},
views : [ 'TitlePanel', 'dashboardpanel', 'ConsumerSignup',
'AddtoWishlog', 'wishlogsummary', 'FeedbackSummary',
'ConsumerSignin' ],
control : {
"#LoginBtn" : {
tap : 'onLoginButtonTap'
},
"#wishloghomebutton" : {
tap : 'onWishlogHomeButtonTap'
},
"#feedbacksummaryhomebutton" : {
tap : 'onFeedbackHomeButtonTap'
},
"#wishlogbtn" : {
tap : 'onWishlogButtonTap'
},
"#Feedbackbtn" : {
tap : 'onFeedbackButtonTap'
},
"#signup" : {
tap : 'onSignupButtonTap'
},
"#capturebtn" : {
tap : 'onCaptureButtonTap'
},
"#consumersignuphomebutton" : {
tap : 'onConsumerSignupHomeButtonTap'
},
"#selectphoto" : {
tap : 'onSelectPhotoButtonTap'
},
"#scanbutton" : {
tap : 'onScanButtonTap'
}
}
},
slideLeftTransition : {
type : 'slide',
direction : 'left'
},
slideRightTransition : {
type : 'slide',
direction : 'right'
},
onLoginButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getDashboardpanel(),
this.slideLeftTransition);
// this.getLabelid.setHtml('Dashboard');
},
onWishlogButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getWishlogsummary(),
this.slideLeftTransition);
},
onFeedbackButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getFeedbackSummary(),
this.slideLeftTransition);
},
onSignupButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getConsumerSignup(),
this.slideLeftTransition);
},
onWishlogHomeButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getDashboardpanel(),
this.slideRightTransition);
},
onFeedbackHomeButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getDashboardpanel(),
this.slideRightTransition);
},
onConsumerSignupHomeButtonTap : function(button, e, options) {
Ext.Viewport.setActiveItem(this.getDashboardpanel(),
this.slideRightTransition);
},
onCaptureButtonTap : function(button, e, options) {
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
quality : 100,
destinationType : Camera.DestinationType.FILE_URI
});
function onPhotoURISuccess(imageURI) {
// console.log(imageURI);
var largeImage = document.getElementById('capturedimage');
largeImage.style.display = 'block';
largeImage.src = imageURI;
}
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
quality : 100,
destinationType : destinationType.FILE_URI,
sourceType : source
});
}
function onFail(message) {
alert('It is failed: ' + message);
}
},
onSelectPhotoButtonTap : function(button, e, options) {
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
quality : 50,
destinationType : navigator.camera.DestinationType.FILE_URI,
sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
});
function onPhotoURISuccess(imageURI) {
// console.log(imageURI);
var largeImage = document.getElementById('capturedimage');
largeImage.style.display = 'block';
largeImage.src = imageURI;
}
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
quality : 100,
destinationType : destinationType.FILE_URI,
sourceType : source
});
}
function onFail(message) {
alert('It is failed: ' + message);
}
},
onScanButtonTap : function(button, e, options) {
window.plugins.barcodeScanner.scan(function(result) {
this.getBarcodebox().setValue(""+result.text);
// alert(“我们有条形码\ n”+ //“结果:”+ result.text); },function(error){ 警报(“扫描失败:”+错误); }); } });
当我在对话框中显示值时,它显示正确。但无法在文本框中设置。我怎么设置条码?请帮忙。
答案 0 :(得分:2)
应:
this.getBarcodebox.setValue(""+result.text);
不是方法调用?
this.getBarcodebox().setValue(""+result.text);
这当然是假设您已在控制器中添加barcodebox
作为ref
:
refs: {
barcodebox: '#barcodtext'
}
请注意id: #barcodtext
中的拼写错误(缺少“e”)并确保其等于ref
选择器指定的值。
- 编辑
看起来this
超出了回调的范围,因此不会引用您的控制器。您可以尝试以下内容:
window.plugins.barcodeScanner.scan(function(result) {
var controller = Ext.app.Application.getController('MyController');
controller.getBarcodebox.setValue(""+result.text);
}, function(error) {
alert("Scanning failed: " + error);
});
答案 1 :(得分:1)
而不是在barcodebox : '#barcodetextfield',
中使用refs
尝试使用getCmp()
var bc = Ext.getCmp('barcodetextfield');
之后bc.setvalue(''+result.text);
我希望这有帮助..