1

This is my very first question.

How can i run an "IF STATEMENT" in side the raw code of QZ tray where Var = print data [];

The below code works wonderful without IF STATEMENT, but the codes cannot parse once i use it.

var printData = [          
           '<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
           '^O0\n'
           '<xpml></page></xpml><xpml><page quantity="9" pitch="127.0 mm"></xpml>~MDELF,FORMAT_0\n',
           '^E10.0\n',
           '^L\n',
           'C0,0000000000000000,+1,prompt_C0\n', 
           'C1,0000000000000000,+1,prompt_C1\n', 
           'C2,000,+1,prompt_C2\n',
           'Lo,51,438,761,440\n',
           'Lo,51,678,761,680\n',
           'Lo,51,558,761,560\n',
           'Lo,51,158,761,160\n',
           'AH,320,31,1,1,0,0,'+ acs +'\n',
           'BQ2,160,742,4,8,156,0,0,C^C0\n',
           'AD,254,900,1,1,0,0,^C1\n',
            'AA,439,440,1,1,0,0,Service\n',
            'Lo,425,440,427,678\n',
            'AA,442,560,1,1,0,0,Total No of Pieces\n',
            'AA,439,684,1,1,0,0,Origin\n',
            'AB,511,684,1,1,0,0,' + origin +'\n',
            'AF,182,590,1,1,0,0,'+ destination+'\n',
            'R49,13,762,999,3,3\n',
            'E\n',
            '^KFORMAT_0\n',


            if (pcstart.length ==1)
{
premawb + postmawb +'0000'+ pcstart +'\n', 
}
            else {
premawb + postmawb +'000'+ pcstart +'\n', 
}
            pcstart + '\n',
            'E\n',
            '~P'+ copyPrint+'\n',


        qz.print(config, printData).catch(displayError);
    }
codemax
  • 11
  • 2

1 Answers1

0

How can i run an "IF STATEMENT" in side the raw code of QZ tray

You can't mid-array, but you can add a ternary operator which does the same thing for a simple if/else statement:

pcstart.length == 1 ? '0000' : '000'

... and in context...

var printData = [          
   '<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
   '...',
   '^KFORMAT_0\n',
   premawb + postmawb + (pcstart.length == 1 ? '0000' : '000') + pcstart + '\n',
   pcstart + '\n',
   'E\n',
   '~P'+ copyPrint + '\n'
];

qz.print(config, printData).catch(displayError);

You can also call a function on the array element, so you may find it more desirable to roll your own pad(...) function and then call pad on the entire number or concatenated string... e.g:

    premawb + postmawb + pad(pcstart, 4) +' \n',

I the above example, pad(...) is a function you make that can contain all the if/else statements you need and returns the formatted value.

tresf
  • 7,103
  • 6
  • 40
  • 101