(function ($) {
Drupal.behaviors.sample_barcode_print = {
attach: function (context, settings) {
$('input#sample_barcode_print',context).once('sample_barcode_print',function(){
//https://www.drupal.org/node/756722
document.getElementById('sample_barcode_print').οnclick=function(){
if (!!window.ActiveXObject || "ActiveXObject" in window){
alert('Barcode Print');
var data=Drupal.settings.sample_barcode_print.testdata;
alert(data);
barcode=new Object();
barcode.X=16;
barcode.nameY=100;
barcode.classY=150;
barcode.descriptionY=200;
barcode.samplenoY=28;
barcode.stylenoY=60;
barcode.typeY=84;
barcode.seasonY=108;
barcode.customY=132;
barcode.divisionY=156;
barcode.colorY=180;
barcode.sizeY=204;
barcode.dateY=228;
barcode.barcodeX=40;
barcode.barcodeY=264;
barcode.name="标 题 :";
barcode.class1="分 类 :";
barcode.description="描 述 :";
barcode.smapleno="样板单号:";
barcode.styleno="款 号 : ";
barcode.type="类 型 : ";
barcode.season="季 度 : ";
barcode.custom="客 户 : ";
barcode.division="跟 单 :";
barcode.color="颜 色 : ";
barcode.size="尺 寸 : ";
barcode.date="日 期 : ";
barcode.barcode="00000000000";
barcode.name1="标 题 :";
barcode.class2="分 类 :";
barcode.description1="描 述 :";
barcode.smapleno1="样板单号:";
barcode.styleno1="款 号 : ";
barcode.type1="类 型 : ";
barcode.season1="季 度 : ";
barcode.custom1="客 户 : ";
barcode.division1="跟 单 :";
barcode.color1="颜 色 : ";
barcode.size1="尺 寸 : ";
barcode.date1="日 期 : ";
barcode.barcode1="00000000000";
postionx=85;
if(data[0][0]=='samplebarcode'){
for(var key in data){
if ( key % 2 ==0){
barcode.smapleno+=data[key][1];
barcode.styleno+=data[key][2];
barcode.type+=data[key][3];
barcode.season+=data[key][5];
barcode.custom+=data[key][6];
barcode.division+=data[key][7];
barcode.color+=data[key][8];
barcode.size+=data[key][9];
barcode.date+=data[key][4];
barcode.barcode=data[key][10];
if (key==data.length-1) {samplebarcodeprint(barcode,postionx);}
}
else{
barcode.smapleno1+=data[key][1];
barcode.styleno1+=data[key][2];
barcode.type1+=data[key][3];
barcode.season1+=data[key][5];
barcode.custom1+=data[key][6];
barcode.division1+=data[key][7];
barcode.color1+=data[key][8];
barcode.size1+=data[key][9];
barcode.date1+=data[key][4];
barcode.barcode1=data[key][10];
samplebarcodeprint(barcode,postionx);
barcode.smapleno="样板单号:";
barcode.styleno="款 号 : ";
barcode.type="类 型 : ";
barcode.season="季 度 : ";
barcode.custom="客 户 : ";
barcode.division="跟 单 :";
barcode.color="颜 色 : ";
barcode.size="尺 寸 : ";
barcode.date="日 期 : ";
barcode.barcode="00000000000";
barcode.smapleno1="样板单号:";
barcode.styleno1="款 号 : ";
barcode.type1="类 型 : ";
barcode.season1="季 度 : ";
barcode.custom1="客 户 : ";
barcode.division1="跟 单 :";
barcode.color1="颜 色 : ";
barcode.size1="尺 寸 : ";
barcode.date1="日 期 : ";
barcode.barcode1="00000000000";
} //单双页结束
}//for end
}else{
for(var key in data){
if ( key % 2 ==0){
barcode.name+=data[key][1];
barcode.barcode=data[key][2];
barcode.class1+=data[key][0];
barcode.description+=data[key][3];
if (key==data.length-1) {userbarcodeprint(barcode,postionx);}
}
else{
barcode.name1+=data[key][1];
barcode.barcode1=data[key][2];
barcode.class2+=data[key][0];
barcode.description1+=data[key][3];
userbarcodeprint(barcode,postionx);
barcode.name="标 题 :";
barcode.class1="分 类 :";
barcode.description="描 述 :";
barcode.barcode="00000000000";
barcode.class2="分 类 :";
barcode.description1="描 述 :";
barcode.barcode1="00000000000";
}
}
}
document.getElementById('sample_barcode_print').disabled=true;
} //if IE end
else{
alert('Support IE Only');
}
}
function samplebarcodeprint(mybarcode,postionX){
OpenPrinter();
PSKPrn.PTKSetCoordinateOrigin(5,5);
PSKPrn.PTKPcxGraphicsDel("*");
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.samplenoY, 24, 0, "宋体", 1, 560, false, false, false, "A1", mybarcode.smapleno);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.stylenoY, 24, 0, "宋体", 1, 560, false, false, false, "A2", mybarcode.styleno);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.typeY, 22, 0, "宋体", 1, 500, false, false, false, "A3", mybarcode.type);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.seasonY, 22, 0, "宋体", 1, 500, false, false, false, "A4", mybarcode.season);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.customY, 22, 0, "宋体", 1, 500, false, false, false, "A5", mybarcode.custom);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.divisionY, 22, 0, "宋体", 1, 500, false, false, false, "A6", mybarcode.division);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.colorY, 22, 0, "宋体", 1, 500, false, false, false, "A7", mybarcode.color);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.sizeY, 22, 0, "宋体", 1, 500, false, false, false, "A8", mybarcode.size);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.dateY, 22, 0, "宋体", 1, 500, false, false, false, "A9", mybarcode.date);
PSKPrn.PTKDrawBarcode(mybarcode.barcodeX+postionX, mybarcode.barcodeY, 0, '1', 2, 2, 90, 66, mybarcode.barcode); //N的ASCII码是78,B的ASCII码是66
postionX+=360;
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.samplenoY, 24, 0, "宋体", 1, 560, false, false, false, "A10", mybarcode.smapleno1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.stylenoY, 24, 0, "宋体", 1, 560, false, false, false, "A11", mybarcode.styleno1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.typeY, 22, 0, "宋体", 1, 500, false, false, false, "A12", mybarcode.type1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.seasonY, 22, 0, "宋体", 1, 500, false, false, false, "A13", mybarcode.season1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.customY, 22, 0, "宋体", 1, 500, false, false, false, "A14", mybarcode.custom1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.divisionY, 22, 0, "宋体", 1, 500, false, false, false, "A15", mybarcode.division1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.colorY, 22, 0, "宋体", 1, 500, false, false, false, "A16", mybarcode.color1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.sizeY, 22, 0, "宋体", 1, 500, false, false, false, "A17", mybarcode.size1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.dateY, 22, 0, "宋体", 1, 500, false, false, false, "A18", mybarcode.date1);
PSKPrn.PTKDrawBarcode(mybarcode.barcodeX+postionX, mybarcode.barcodeY, 0, '1', 2, 2, 90, 66, mybarcode.barcode1);
PSKPrn.PTKPrintLabel(1, 1);
ClosePrinter();
}
function userbarcodeprint(mybarcode,postionX){
OpenPrinter();
PSKPrn.PTKSetCoordinateOrigin(5,5);
PSKPrn.PTKPcxGraphicsDel("*");
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.nameY, 24, 0, "宋体", 1, 560, false, false, false, "A1", mybarcode.name);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.classY, 24, 0, "宋体", 1, 560, false, false, false, "A2", mybarcode.class1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.descriptionY, 24, 0, "宋体", 1, 560, false, false, false, "A3", mybarcode.description);
PSKPrn.PTKDrawBarcode(mybarcode.barcodeX+postionX, mybarcode.barcodeY, 0, '1', 2, 2, 90, 66, mybarcode.barcode); //N的ASCII码是78,B的ASCII码是66
postionX+=360;
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.nameY, 24, 0, "宋体", 1, 560, false, false, false, "A10", mybarcode.name1);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.classY, 24, 0, "宋体", 1, 560, false, false, false, "A11", mybarcode.class2);
PSKPrn.PTKDrawTextTrueTypeW(mybarcode.X+postionX, mybarcode.descriptionY, 24, 0, "宋体", 1, 560, false, false, false, "A12", mybarcode.description1);
PSKPrn.PTKDrawBarcode(mybarcode.barcodeX+postionX, mybarcode.barcodeY, 0, '1', 2, 2, 90, 66, mybarcode.barcode1);
PSKPrn.PTKPrintLabel(1, 1);
ClosePrinter();
}
function OpenPrinter(){
//这里可以调用控件的已知方法
PSKPrn.OpenPort("POSTEK C168/200s");
PSKPrn.PTKClearBuffer();
PSKPrn.PTKSetPrintSpeed(4);
PSKPrn.PTKSetDarkness(10);
PSKPrn.PTKSetLabelHeight(400, 16, 0, 0);
PSKPrn.PTKSetLabelWidth(720);
PSKPrn.PTKSetDirection(84);
}
function ClosePrinter(){
PSKPrn.ClosePort();
}
}
);
}
};
}(jQuery));
<?php
/**
* @file
* Defines "通过已上传的样板资料生成对应的条码"
*
* @TODO: Split admin functions into their own file.
*/
/**
* Implements hook_menu(). 执行Hook来注册一个URL
*/
function sample_barcode_print_menu(){
$items=array();
$items['samplebarcode/barcodeprint']=array(
'title'=>'Print Sample',
'description'=>'样板条码生成',
'page callback'=>'drupal_get_form',
'page arguments'=>array('sample_barcode_print'),
//页面回调的function
'access arguments'=>array('Sample Barcode Print'),
//页面调用的用户权限
'access callback'=>TRUE,
);
$items['samplebarcode/barcodesprint']=array(
'title'=>'Print Sample',
'description'=>'样板条码生成',
'page callback'=>'drupal_get_form',
'page arguments'=>array('sample_barcode_adidas_print'),
//页面回调的function
'access arguments'=>array('Sample Barcode Print'),
//页面调用的用户权限
'access callback'=>TRUE,
);
$items['samplebarcode/printuser']=array(
'title'=>'Print Operator',
'description'=>'打印操作员',
'page callback'=>'drupal_get_form',
'page arguments'=>array('sample_barcode_printeruser'),
//'access arguments'=>array('Sample Barcode Adidas'),
'access callback'=>TRUE,
);
return $items;
}
/**
* @file
* 定义一个样板单的权限
*/
function sample_barcode_print_permission(){
return array(
'Sample Barcode Print'=>array(
'title'=>t('条码打印'),
),
);
}
/**
* @file
* 定义生成按钮的表单
*/
function sample_barcode_print($form, &$form_state){
smaple_barcode_print_barshow("block_1","block_2");
$form=array();
$form['select']=array(
'#type' => 'fieldset',
'#title' => t('TSG Print Sample'),
);
$form['select']['print']= array(
'#type' => 'submit',
'#value' => t('样板条码打印'),
//'#attributes'=>array('onclick'=>'mytest()'),
'#id'=>'sample_barcode_print',
'#ajax'=>array(
'callback'=>'sample_barcode_printing',
'wrapper'=>'showexcel-wrapper',
),
);
$form['showexcel']=array(
'#type'=>'fieldset',
'#title'=>t('Print Show'),
'#prefix'=>'<div id="showexcel-wrapper">',
'#suffix'=>'</div>',
'#markup' =>'',
);
return $form;
}
function sample_barcode_adidas_print($form, &$form_state){
smaple_barcode_print_barshow("block_5","block_6");
$form=array();
$form['select']=array(
'#type' => 'fieldset',
'#title' => t('Print Sample'),
);
$form['select']['adidasprint']= array(
'#type' => 'submit',
'#value' => t('样板条码打印'),
//'#attributes'=>array('onclick'=>'mytest()'),
'#id'=>'sample_barcode_print',
'#ajax'=>array(
'callback'=>'sample_barcode_adidas_printing',
'wrapper'=>'showexcel-wrapper',
),
);
$form['showexcel']=array(
'#type'=>'fieldset',
'#title'=>t('Print Show'),
'#prefix'=>'<div id="showexcel-wrapper">',
'#suffix'=>'</div>',
'#markup' =>'',
);
return $form;
}
/**
* @
* 定义ajax对样单进行生成或导出条码操作。
*/
function sample_barcode_printing($form,&$form_state){
$barcodenum=sample_barcode_print_confirm("block_1","block_2");
$form['showexcel']['#markup']=t('@barcode pcs Barcode was Printting',array('@barcode'=>$barcodenum));
return $form['showexcel'];
}
function sample_barcode_adidas_printing($form, &$form_state){
$barcodenum=sample_barcode_print_confirm("block_5","block_6");
$form['showexcel']['#markup']=t('@barcode pcs Barcode was Printting',array('@barcode'=>$barcodenum));
return $form['showexcel'];
}
function sample_barcode_print_confirm($display1,$display2){
$views=views_get_view("view_sample_tracert");
$views->preview($display1);
$results=$views->result;
foreach($results as $key){
$node=node_load($key->nid);
$node->field_printtag['und'][0]['value']='1';
node_save($node);
}
$views=null;
$results=null;
$views=views_get_view("view_sample_tracert");
$views->preview($display2);
$results=$views->result;
$barcodenum='0';
foreach ($results as $key)
{
$barcodenum=$key->nid;
}
return $barcodenum;
}
function smaple_barcode_print_barshow($display1,$display2){
$views=views_get_view("view_sample_tracert");
$views->preview($display1);
$results=$views->result;
dpm($views);
$data=array();
$row=0;
foreach($results as $key){
$node=node_load($key->nid);
$data[$row][0]='samplebarcode';
$data[$row][1]=$key->node_field_data_field_sampletracert_title;
$data[$row][2]=$key->field_field_warehousestyleno[0]['raw']['value'];
$data[$row][3]=$key->field_field_sample_type[0]['raw']['value'];
$data[$row][4]=$key->field_field_datetime[0]['raw']['value'];
$data[$row][5]=$key->field_field_season[0]['raw']['value'];
$data[$row][6]=$key->field_field_brand[0]['rendered']['#markup'];
$data[$row][7]=$key->field_field_division[0]['raw']['value'];
$data[$row][8]=$key->field_field_color[0]['raw']['value'];
$data[$row][9]=$key->field_field_size[0]['raw']['value'];
$data[$row][10]=$key->field_field_barcode_code[0]['raw']['value'];
$row++;
}
dpm($data);
//$data 已准备好传向打印控件。
drupal_add_js(array('sample_barcode_print'=>array('testdata'=>$data),),'setting');
return ;
}
function sample_barcode_printeruser($form, &$form_state){
//设置一个菜单链接空页面,并通过Views显示数据。
}
function sample_barcode_print_init()
{
$forie = array(
'#type' => 'markup',
'#markup' => '
<object id="PSKPrn" classid="clsid:81C07687-3353-4ABA-B108-94BCE81E5CBA" codebase="PSKPrn.ocx" width="0" height="0" ></object>
<script language="text/javascript" for="PSKPrn" event="ocxEvent()"></script>
',
);
drupal_add_html_head($forie, 'sample_barcode_print');
drupal_add_js(drupal_get_path('module','sample_barcode_print').'/sample_barcode_print.js','file');
}
/**
* @
* 定义Form的提交
*/
3
down vote
The question is old, but I was also interested in it. In my opinion, the easiest way is to use the click event in your Ajax definition, because Drupal uses the mousedown event as default:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('some value'),
'#attributes' => array(
'class' => array('some-class'),
),
'#ajax' => array(
'event' => 'click', //add this line!
'callback' => 'some_callback',
'wrapper' => 'some-wrapper',
),
);
Then you just need to add a .mousedown() event to your button in your Javascript file, because it is fired before the click event:
$('.some-class').mousedown(function() {
//do something
});
If you still want your Ajax Request called with the mousedown event, you can use a custom event:
//change this line in the Ajax Defintion of your button
'event' => 'click',
//to
'event' => 'custom_event',
Then you can trigger this event in the .mousedown() event of your Javascript file:
$('.some-class').mousedown(function() {
//do something
$('.some-class').trigger('custom_event');
});
Both versions work!
最后找到解决方案http://stackoverflow.com/questions/18026951/drupal-ajax-call-from-click-of-a-link-in-form