JavaScript – Data View


What is DataView ?

A data view is a view on a data table.

What is DataView in Javascript ?

DataView is a Javascript built-in function provides an interface to read and write more than one number types into an ArrayBuffer.

DataView provides a low-level interface for reading and writing multiple number types in a binary ArrayBuffer, without caring about the platform’s end.

Why DataView function is used in javascript ?

DataView enables you to create different views of the data stored in a DataTable,

DataView is used in data-binding applications.

DataView is used to expose the data in a table with different sort orders.

DataView also allows us to filter the data by row state or based on a filter expression.

Javascript full course free !!!

Click to Learn More about – Javascript online learning

Syntax :

new DataView(buffer, byteOffset, byteLength,)

buffer:

An ArrayBuffer that is already existing to store the new DataView object.

byteOffset (optional):

offset(in bytes) in the buffer is used to start a new view of the buffer. By default, the new view starts from the first byte.


byteLength (optional):


It represents the number of elements in the byte array. By default, the buffer’s length is considered as the length of the view.

Return value:

It returns a new DataView object which will represent the specified data buffer.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">

			// Creating an ArrayBuffer with a size in bytes 
			var buffer = new ArrayBuffer(16); 

			// Creating views 
			var a = new DataView(buffer); 

			//creating view from byte 0 for the next 4 bytes 
			var b = new DataView(buffer,0,4);  

			//creating view from byte 12 for the next 2 bytes 
			var c = new DataView(buffer,12,2); 

			// Putting 1 in slot 0 
			a.setInt8(0, 1);  

			// Putting 2 in slot 12 
			a.setInt8(12, 2) 

			//printing the views 
			document.write("The value of a.getInt8(0) = " + a.getInt8(0)+'<br>'); 
			document.write("The value of b.getInt8(0) = " + b.getInt8(0)+'<br>'); 
			document.write("The value of c.getInt8(0) = " + c.getInt8(0)+'<br>'); 
		</script>
	</head>
	<body></body>
</html>

Output :

The value of a.getInt8(0) = 1

The value of b.getInt8(0) = 1

The value of c.getInt8(0) = 2

There are few DataView method in Javascript listed below,

SnoMethod / FunctionDescription
1DataView.getFloat32()DataView.getFloat32() method is used to get 32-bit float number at a specified location.
2DataView.getFloat64()DataView.getFloat64() method is used to get 64-bit float(double) number at a specified location.
3DataView.getInt16()DataView.getInt16() method is used to gets a signed 16-bit integer(short) number at a specified location.
4Dataview.getInt32()DataView.getInt32() method is used to gets a signed 32-bit integer(long) number at a specified location.
5DataView.getInt8()DataView.getInt8() method is used to gets a signed 8-bit integer(byte) number at a specified location.
6DataView.getUint16()DataView.getUint16() method is used to gets a unsigned 16-bit integer(unsigned short) number at a specified location.
7DataView.getUint32()DataView.getUint32() method is used to gets a unsigned 32-bit integer(unsigned long) number at a specified location.
8DataView.getUint8()DataView.getUint8() method is used to gets a unsigned 8-bit integer(unsigned byte) number at a specified location.