Take a value of a cell[0] of a . date()

Asked

Viewed 128 times

-2

I was able to pull all the data from my selected line. But I want to access only the value of a property, for example id_produto. How do I do?

I’ve managed to print out all the data on the console:

inserir a descrição da imagem aqui

$( document ).ready(function() {	
	var table = $('#tbl_estoque').DataTable( {
		"ajax": "data.php",
		"bPaginate":true,
		"bProcessing": true,
		"pageLength": 5,

		
		"columns": [
			{ mData: 'cod_produto' } ,
			{ mData: 'cat_produto' },
			{ mData: 'descricao_produto' },
			{ mData: 'qnt_total' },
			{ mData: 'qnt_vendido' },
			{ mData: 'valor_produto' },
			{ mData: 'valor_total' },
			{defaultContent: "<button>Click!</button>"}
		]
	});	
	$('#tbl_estoque tbody').on( 'click', 'tr', function () {
    console.log( table.row( this ).data() );

} );
	$('#btn_load').click( function () {
		table.ajax.reload(null, false);
	});	
});

After placing the console.log( table.Row( this ).data('id_product') ); Look at the console as it turned out: inserir a descrição da imagem aqui

  • Instead of using a photo, post the code in text format using code block formatting. :)

  • Sorry! I put the code.

1 answer

1


As shown by documentation of the method data, you can pass a key as first argument and the corresponding value will be returned.

You can also use the notation to access properties of a object (obj.propName).

An example of the two possibilities mentioned:

const $el = $('time')

console.log($el.data())

// Usando a API do jQuery:
console.log($el.data('country'))

// Através da notação de objeto:
console.log($el.data().timezone)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<time data-country="BR" data-timezone="-3">22:00</time>

So you can do:

console.log( table.row( this ).data('id_produto') );
  • did not work, posted the result in the question

  • Try to do so: table.row( this ).data().id_produto. Works?

  • I gave it right. But when I do: Document.getElementById("category"). value = table.Row( this ).data(). cat_product; in which case it writes in my select of id=category the value of the cell. IT DOES NOT WRITE IN SELECT. Only input text. What can it be?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.