Available only in PRO Edition
The widget is available in the Webix Pro edition.
The Barcode widget represents text values in a machine-readable format by varying the widths and spacing of parallel lines.
The widget uses Canvas for lines rendering.
To begin with, create a new HTML file and include the related Webix code files in it.
Required code files are:
A basic html page with the included code files
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Barcode</title>
<script src="../codebase/webix.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="../codebase/webix.css" type="text/css" charset="utf-8">
</head>
<body>
<script> //here you will place your JavaScript code
</script>
</body>
</html>
Next, you should create a div container to place your barcode into.
<div id="box" style="width:300px;height:200px;"></div>
Now you can initialize the widget.To create a new Barcode object, you need to use the following constructor:
Barcode constructor
barcode = new webix.ui({view:"barcode"});
If you want to place the component into the container, you should specify the container property inside of the view constructor:
Basic barcode configuration
barcode = new webix.ui({
container:"box",
view:"barcode"
});
Related sample: Barcode Initialization
You can set the following properties to configure Barcode:
webix.ui({
view:"barcode",
type: "upcA"
});
//or
var barcode = webix.ui({
view:"barcode"
});
barcode.define("type", "upcA");
webix.ui({
view:"barcode",
value: "123456789012"
});
webix.ui({
view:"barcode",
width: 250
});
If you want to set flexible width, you need to set the -1 value for this property:
webix.ui({
view:"barcode",
width: -1
});
webix.ui({
view:"barcode",
height: 180
});
If you want to set flexible height, you need to set the -1 value for this property:
webix.ui({
view:"barcode",
height: -1
});
webix.ui({
view:"barcode",
paddingX: 20
});
webix.ui({
view:"barcode",
paddingY: 10
});
webix.ui({
view:"barcode",
textHeight: 20
});
webix.ui({
view:"barcode",
color: "#000"
});
You can set the value in two ways:
var barcode = new webix.ui({
view:"barcode",
value: "123456789012"
});
// set the new value
barcode.setValue("453456789015");
Webix Barcode includes 3 definitions for most popular barcodes (EAN8, EAN13 and UPC-A):
"ean13" is the default type.
Use the type property to set the Barcode type:
barcode = webix.ui({
view:"barcode",
type: "upcA"
});
The type of Barcode can be changed dynamically via the define method. After the changes are applied, you need to call the render method to redraw Barcode with the new type:
barcode.define("type","ean8");
barcode.render();
Related sample: Built-in Types
If you need create a custom barcode, you can add the necessary type to the existing Barcode view.
Each barcode type should contain a function that returns a sequence of "0" and "1" chars based on the incoming text value. This 0-and-1 character combination will be used for bar rendering.
The Library will render black bars for "1" chars and leave gaps for the "0" ones.
To add a new type you need to use the webix.type method that takes two parameters:
webix.type( webix.ui.barcode, {
/* type configuration here */
});
Type configuration of Barcode must contain 3 properties:
Let's consider an example of the "code39" barcode configuration:
1) define type name in the type configuration:
name:"code39",
2) set an array with encodings for 43 characters, consisting of uppercase letters (A through Z), numeric digits (0 through 9) and a number of special characters (-,., $, /, +, %, and space):
encodings:[
"110101001011",
"101101001011",
...
]
3) set the template function that will convert the incoming text to uppercase and add "*" as start and stop characters:
template: function(value){
return ("*" + value + "*").toUpperCase();
}
4) define the encode method that will apply 0-1 sequences from encodings array to incoming text value
encode: function(value){
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. $/+%*";
var code ="";
// apply template method
value = this.template(value);
for(var i=0; i<value.length; i++){
var pos = chars.indexOf( value.charAt(i) );
if (pos < 0)
return "";
if (i)
code += "0";
// apply encoding for character
code += this.encodings[pos];
}
return code;
}
So, the whole "code39" type definition will be as in:
webix.type(webix.ui.barcode, {
// name
name: "code39",
// encodings for "A","B", etc. characters
encodings: [
"110101001011",
"101101001011",
...
],
// applies encoding for incoming text
encode: function(value){
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-. $/+%*";
var code ="";
// apply template method
value = this.template(value);
for(var i=0; i<value.length; i++){
var pos = chars.indexOf( value.charAt(i) );
if (pos < 0)
return "";
if (i)
code += "0";
// apply encoding for character
code += this.encodings[pos];
}
return code;
},
// returns text for code39 barcode
template: function(value){
return ("*" + value + "*").toUpperCase();
}
});
Here is an example of the "code39" usage:
barcode = webix.ui({
view: "barcode",
type: "code39",
value: "CODE 39"
});
The result of applying the "code39" type is given in the picture below:
Back to top