javascript to JSON save dynamic property - javascript

i"m with basic knollage in javascript,
i"m trying to get data from user and use it as json properties as following
<th colspan="2"><input id="dc_name" name='dc[name]'></input></th>
$(".save_asJSON").click(function(){
var dcname = {};document.getElementById("dc_name").value
var jsonobj = {
DCes: {
dcname: {
name : dcname,
.....
}
}
};
console.log(jsonobj);
});
the fist dcname use (and here is my problem) - displayed as string dcname
the second dcname use give the value as it should.
i need the json file this way, any idea how can I achieve this
thanks

You can initialise your json with empty values and then set its value with the input like
var jsonobj = {
DCes: {}
};
$(".save_asJSON").click(function(){
var dcname = document.getElementById("dc_name").value
jsonobj.DCes[dcname] = {name: dcname}
console.log(jsonobj);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<th colspan="2"><input id="dc_name" name='dc[name]'></input></th>
<button class="save_asJSON">Click</button>

Related

get and save values from one-to-many relationship and use in javascript with Laravel

Controller code is:
$payments = array();
$pays = User::find($userid);
foreach ($pays->payments as $pay) {
$payments = $pay->payment;
}
it gives output as 18000, 19000, 9000 as stored in array.
I'm confused how to pas these values to my blade file and use in the javascript.
I'm getting only the last values of the array when using in JS.
You are overwriting value of $payments use [] to add in array
$payments = array();
$pays = User::find($userid);
foreach ($pays->payments as $pay) {
$payments[] = $pay->payment;
}
return view('myview_name',compact('payments'))
In laravel blade
#foreach($payments as $payment)
<tr> <td>{{$payment}}</td> </tr>
#endforeach
In javascript you can json_encode and JSON.parse to decode it
var payments = <?php echo json_encode($payments) ?>;
console.log(payments);
Use something like:
$payments = array();
$pays = User::find($userid);
foreach ($pays->payments as $pay) {
$payments[] = $pay->payment;
}
Or simpler:
$payments = $pays->payments->pluck("payment");
And then pass it to the view via e.g.
return view('view.name', compact('payments'));
You can then use it in the view as an array.
If you want to pass it into a JS variable you can use:
<script>
var payments = {!! json_encode($payments) !!};
</script>
Try this
<input type="hidden" id="data" value="{{ $payments }}">
In script section
$(document).ready(function() {
data = $('#data').val();
// Myabe u will need to parse the same.
// And then use it accordingly
});
UPDATE
Return the array in json format and use $.parseJSON(data); to retrieve in js.

How to assign a value array from json to particular table based on ID

Im working on project which need to assign a value in array from json to particular ID on multiple table in and .
As you see below the html that contains ID in title and odds. And you can see i was writing dead for this particular html therefore i need to retrieve from backend which the particular title and odds will be change based on the backend given value.
HTML
<tr><th class="GB1_0 name" id="t_B1_0" title="第一球 0"><input type="hidden" id="k_B1_0" value="BALL"><span class="b0">0</span></th>
<td class="GB1_0 odds" id="o_B1_0">9.93</td>
<td class="GB1_0 amount ha"><input name="B1_0" class="ba"></td>
</tr>
List of array with jSON :
How do we do this ? Kindly advice Thanks you
Is this what you are looking for?
<script>
$( document ).ready(function() {
var jsonObj = {
B1_0 : 9.93,
B1_1 : 9.95,
B1_2 : 9.94
};
$.each(jsonObj, function (index, value) {
console.log(index);
console.log(value);
$("#o_"+index).html(value);
});
});
</script>

HTML data attributes values. How to change?

I have a div, which I can't edit. Is it possible to multiply the values of the data attribute?
Code:
<div class="reviews" data-page-opts="
{
showform:1,
showsupport:0,
postid:241,
perpage:5,
paginate:1,
classes:reviews_in_content, wrapper:1,
morelink:,
on_postid:0,
num:9999,
hidecustom:0,
snippet:0,
hidereviews:0,
hideresponse:0,
ajax:0,
thispage:1
}
">
</div>
Example:
I want to multiply thispage: 1 by 5 or just to replace 1 with 5. Can I do this with JavaScript/jQuery? Thanks in advance.
Use can use jQuery to get value of data first, and then use JSON.parse(), and then modify, after you modify, you can use JSON.stringify();
Here is example code:
var data = $('div.reviews').attr('data-page-opts');
var dataObj = JSON.parse(data);
dataObj.ajax = 1;
data = JSON.stringify(dataObj);
$('div.reviews').attr('data-page-opts', data);

Saving table data to HTML5 LocalStorage

I've created a JSFiddle Here
What I'm trying to do is save table data from the following code:
$('#save').click(function () {
$("#dataTable").find('tbody')
.append($('<tr>')
.append($('<td>')
.text($('#fname').val()))
.append($('<td>')
.text($('#lName').val()))
.append($('<td>')
.text($('#mId').val()))
);
$('#fname').val('');
$('#lName').val('');
$('#mId').val('');
})
I would like to save the <tr> data, but I'm having trouble finding where to start on parsing that into a savable format.
I have written little bit code to help you.
First create a class for the record which you want to store in grid
function MemberInfo(fName,lName,memberId){
this.FirstName=fname;
this.LastName=lName;
this.MemberId=memberId;
}
the create a function which will loop through all the tr and tds to populate that array, and finally save the JSON data in localStorage
var arr=[];
$("#dataTable").find('tbody tr').each(function(index,item){
var fName=$(item).find('td').eq(0).text();
var lName=$(item).find('td').eq(1).text();
var memberId=$(item).find('td').eq(2).text();
arr.push(new MemberInfo(fName,lName,memberId))
});
localStorage.setItem("memberData",arr);
Fiddle
Maybe you can save it as a JSON string
var data = {
name : $('#fname').val(),
lastname : $('#lName').val(),
memberId : $('#mId').val()
};
localStorage.setItem('member-data', JSON.stringify(data))
then later:
var data = JSON.parse(localStorage.getItem('member-data') || {})
$('#fname').val(data.name);
$('#lName').val(data.lastName);
$('#mId').val(data.memberId);

Javascript push Object to cookies using JSON

Hi All on click button I need to add object to array and then write array to cookies.
From the start this array can be not empty so I parse cookie first.
function addToBasket(){
var basket = $.parseJSON($.cookie("basket"))
if (basket.length==0||!basket){
var basket=[];
basket.push(
{ 'number' : this.getAttribute('number'),
'type' : this.getAttribute('product') }
);
}
else{
basket.push(
{ 'number' : this.getAttribute('number'),
'type' : this.getAttribute('product') }
);
}
$.cookie("basket", JSON.stringify(basket));
}
And HTML
<button type="button" class="btn btn-success btn-lg" number="12" product="accs" onclick="addToBasket()">Add</button>
Unfortunately I'm getting Uncaught ReferenceError: addToBasket is not defined onclick.
Can't understand what am I doing wrong?
Thanks!
I simplified your code a good deal, heres a fiddle: http://jsfiddle.net/yJ6gp/
I wired the click event using jQuery and simplified some of your code (see comments). Note I changed your html a little so I could select the add basket button by class - change as desired.
$(function () {//doc ready
$.cookie.json = true; //Turn on automatic storage of JSON objects passed as the cookie value. Assumes JSON.stringify and JSON.parse:
$('.add-basket').click(function() {
var basket = $.cookie("basket") || []; //if not defined use an empty array
var $this = $(this);
basket.push({
'number': $this.attr('number'),
'type': $this.attr('product')
});
console.log(basket);
$.cookie("basket", basket);
});
});

Categories