$(document).ready(function() {
    var table = $('#relationtable').DataTable({
        "columnDefs": [
            {
                "targets": [ 0 ],
                "visible": false,
                "searchable": false
            },{
                "searchable": false,
                "orderable": false,
                "targets": 1
            }],
            "order": [[ 1, 'asc' ]]
    });
    table.on( 'order.dt search.dt', function () {
        table.column(1, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();

    $('#relationtable tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    } );
 
     
  
    $('#submitRelationButton').click( function () {
        // window.location.href = url_back;
        
        var coutodrelation= table.rows('.selected').data().length;
        var test=[];
        var value = $("#productid").val(); 
        for(var i=0;i<coutodrelation;i++){
            test.push(table.rows('.selected').data()[i][0]);
        }
            console.log(test);
            
            $.ajaxSetup({
                headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                     }
                });
            
            $.ajax({
                type: "POST",
                url: checkdata,
                data:{'relationdata':test,'productid':value},
                dataType: "json",
                success:function(response) {
                    if(response.inInsert){
                        console.log("sucess");
                    }
                    else{
                        console.log("error");
                    }
                }
              });         
	});
} );







