0

I have a list of checkboxes with ids, I can select the checkboxes and get all the ids by doing the following:

var order_id = [];      //array to store order ids from the checkboxes

var x = 0;  //count

checkboxes.each(function(){ //loop through the selected checkbox and store ids in array

  if ($(this).hasClass('active')) {

    order_id[x] = $(this).data('order-id');

    x++; //increment x

});//end each   

What I want to do is print out these ORDER ID numbers to my dymo label printer and in order to do that I need to use a XML file provided by Dymo. This file is located on the server, I can get the file by using $.get

$.get(url+"labels/dymo_19x51.xml", function(labelXml) {
  //framework
  var label = dymo.label.framework.openLabelXml(labelXml);

  //Order ID is set here
  label.setObjectText("TEXT", order_id[x]);

  //Label is printed here
  label.print(myDymoPrinter);

}, "TEXT");

The problem I'm having is, if I select 5 checkboxes, 5 labels will print but they will all be the same ie will print the last label selected 5 times. I can't get it to iterate through the order_id array within the $.get function. I've tried incrementing x at different points in my script, but I just can't make it happen.

ANY help is appreciated.

Many Thanks, Kahl

FSDford
  • 448
  • 5
  • 10
Kahlil N
  • 71
  • 2
  • 10
  • Does this answer your question? [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Heretic Monkey Jan 25 '21 at 15:55
  • Thanks, it kinds of did! I ended using ajax and setting async to false – Kahlil N Jan 25 '21 at 16:44

1 Answers1

0

I ended up doing this. I used AJAX and set async to false

                                var order_id = $(this).data('order-id');
                                var qty = $(this).data('qty');
                                var delivery_method = $(this).data('shipping');
                                var full_name = $(this).data('fullname');
                                var dymo_txt = full_name +" (" + order_id + ")"+"\nQty: " + qty + "\n" + delivery_method;

                                $.ajax({
                                    type: 'GET',
                                    url: ajax_object.plugin_admin_assets+"labels/dymo_19x51.label",
                                    async: false,
                                    beforeSend: function() {
                                    },
                                    success: function(response) {
                                        try {
                                            var label = dymo.label.framework.openLabelXml(response);
                                            label.setObjectText("TEXT", dymo_txt);

                                            var printers = dymo.label.framework.getPrinters();
                                            if (printers.length == 0) {
                                                throw "No DYMO printers are installed. Install DYMO printers." + dymo_txt;
                                            }

                                            var printerName = "";

                                            for (var i = 0; i < printers.length; ++i) {
                                                var printer = printers[i];
                                                //check that we are printing to the correct label writer
                                                //can also use printer.printerType to get the the first label writer or if you only have 1
                                                if (printer.modelName == "DYMO LabelWriter 450") {
                                                    printerName = printer.name;
                                                    break;
                                                }//end if
                                            }//end for

                                            if (printerName == "") {
                                                throw "No LabelWriter printers found. Install LabelWriter printer";
                                            }
                                    
                                            label.print(printerName);
                                    
                                        }//end try  
                                    }//end success
                                }); //end ajax

Kahlil N
  • 71
  • 2
  • 10
  • Setting `async: false` is deprecated: See [the documentation](https://api.jquery.com/jquery.ajax/), under `async`: "As of jQuery 1.8, the use of `async: false` with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done()." – Heretic Monkey Jan 25 '21 at 16:48