Why doesn't the value of variable show? I think it should be 1 or 0 depending on value inside of variable bayar, but why its doesn't work
I've tried to do anything that I can do but it doesn't work like it try to change several code but it doesn't work.
Here's my controller code:
public function generated(){
$data=DB::table("tb_trx_perhitungan_bunga_simpanan")
->selectRaw('trx_bulan')
->get();
return view('crud.form.cobaNampilin',compact('data'));
}
And here's my JavaScript and jQuery code:
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var a;
var array = {!! json_encode($data) !!};
var i;
var b;
$('#bulan').keyup(function(){
var bayar=parseInt($('#bulan').val());
for(i=0;i<=array.length;i++){
if(bayar==array[i].trx_bulan){
a=1;
}
else{
a=0;
}
}
$('#hasil').val(a);
});
});
</script>
I expected the output is 1 or 0
The content of var array I think should be [trx_bulan 1],[trx_bulan 2],[trx_bulan 3],[trx_bulan 4],[trx_bulan 5],[trx_bulan 6] because content of array is an array object. I read that on the website so that's why I use array[index].trx_bulan] to call the value of each index.
Here's the screenshot of table data:
So if you have a data table like:
Then consider the following code:
$(function() {
var a = 0;
/*
In laravel use:
var data = {{json_encode($data)}};
it will translate to something like:
*/
var data = [{
id: 27,
trx_bulan: 1,
trx_tahun: 2018,
tangal_proses: "2018-12-09 07:23:00",
persentase_bunga: 0.2,
id_user: 3
}, {
id: 28,
trx_bulan: 2,
trx_tahun: 2018,
tangal_proses: "2018-12-09 03:42:39",
persentase_bunga: 0.2,
id_user: 3
}, {
id: 29,
trx_bulan: 3,
trx_tahun: 2018,
tangal_proses: "2018-12-09 03:43:43",
persentase_bunga: 0.2,
id_user: 3
}, {
id: 30,
trx_bulan: 4,
trx_tahun: 2018,
tangal_proses: "2018-12-09 03:44:06",
persentase_bunga: 0.2,
id_user: 3
}, {
id: 31,
trx_bulan: 5,
trx_tahun: 2018,
tangal_proses: "2018-12-09 03:45:40",
persentase_bunga: 0.2,
id_user: 3
}, {
id: 33,
trx_bulan: 6,
trx_tahun: 2018,
tangal_proses: "2018-12-09 06:32:38",
persentase_bunga: 0.2,
id_user: 3
}];
$('#bulan').keyup(function(e) {
var bayar = parseInt($('#bulan').val());
a = 0;
$.each(data, function(k, v) {
if (bayar == v.trx_bulan) {
console.log("Hit", v);
a = 1;
}
});
$('#hasil').val(a);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="bulan" />
<br />
<input type="text" id="hasil" />
You may want to consider switching to type="number" or checking that the entry is a number value when the user types something.
Hope that helps.
It never return output because wrong loop condition, the loop for(i=0;i<=array.length;i++) will throw error and $('#hasil').val(a); will not executed.
TypeError: array[i] is undefined
It is because i index is out of range, use the < only
for(i=0; i<array.length; i++)
then if element with ID hasil is not <input> use .html()
$('#hasil').html(a);
And there are several ways to convert to JSON
#json Blade directive
var array = #json($data);
single Curly brackets
var array = {!! json_encode($data) !!};
using toJson()
var array = {!! $data->toJson() !!};
and native php
var array = <?php echo json_encode($array); ?>;
Related
I am trying to push data into a variable Data[] but all I have is an example of how to do this hard coded:
var data = [{
id: "1",
name: "Development Life Cycle",
actualStart: Date.UTC(2018, 01, 02),
actualEnd: Date.UTC(2018, 06, 15),
children: [{
id: "1_1",
name: "Planning",
actualStart: Date.UTC(2018, 01, 02),
actualEnd: Date.UTC(2018, 01, 22),
connectTo: "1_2",
connectorType: "finish-start",
progressValue: "75%"
},
// more data goes here
]
}];
I tried with following code but it doesn't seem to work:
ganttdata = [];
// DUMMY DATA
id = 10;
RFInum = 'asfd';
start= 1;
LTIOV = 2;
progressValue = 10;
ganttdata.push("{id:"+id, "parent: null", "name:"+RFInum, "actualStart: Date.UTC("+start+")", "actualEnd: Date.UTC("+LTIOV+")", "progressValue:"+progressValue+"%}");
console.log(ganttdata);
Anyone know how to write this?
You are currently inserting your data as a string and not as an object.
I recreated your approach and inserted your data as an object into the ganttdata array.
Date documentation
var ganttdata = [];
// DUMMY DATA
var id = 10;
var RFInum = 'asfd';
var start = Date.UTC(2021, 12, 31);
var LTIOV = Date.UTC(2021, 12, 31);
var progressValue = 10;
ganttdata.push({
id,
parent: null,
name: RFInum,
actualStart: start,
actualEnd: LTIOV,
progressValue: `${progressValue}%`,
});
console.log(ganttdata);
Or if you want to append some data to an existing object at a specific index:
var data = [{
id: 1,
name: 'test'
}];
console.log('before:', data);
data[0].extend = 'hello World';
data[0].foo = 'bar';
console.log('after:', data);
So basically i have this code which has a return in a function which should then display the returned variable "Price" but the alert box does not show.
If i delete all code except alert it will. I can't find any misspelled words or pieces of code. Could you help me ?
<html>
<body>
<script>
var auto = {
merk: 'BMW',
model: 1,
aantal deuren: 5,
bouwjaar: 1990,
prijs : 20000,
price: function(){
return this.prijs;
}
};
var x = auto.price();
alert(x);
</script>
</body>
You have invalid property name in the object:
aantal deuren: 5,
If the property name is not valid identifier you need to wrap it in quotes:
var auto = {
merk: 'BMW',
model: 1,
"aantal deuren": 5,
bouwjaar: 1990,
prijs: 20000,
price: function () {
return this.prijs;
}
};
var x = auto.price();
alert("prijs");
Use '' for 'aantal deuren': 5, or remove space from aantaldeuren: 5, object property
I'm making a request for x and y values from a flax/python backend using Polymer and I can read the values in console for the XMLHttpResquest response, but now I need to convert the output into a set of discrete x and y values (so it can be read by C3.js - a framework sitting on top D3 for graphing).
Here's the code I'm using to get the XMLHttpResquest response:
<paper-button affirmative hover on-tap="{{addNewGraph}}">Submit</paper-button>
Polymer("add-graphItem",{
addNewGraph: function () {
var HeaderName = this.$.graphOptionsLoad.$.headerValue.selectedItem.label;
var FunctionName = this.$.graphFunctionsLoad.$.functionValue.selectedItem.label;
console.log("The options are " +HeaderName +" and " +FunctionName);
var params = {};
if (this.$.graphOptionsLoad.$.headerValue.selectedItem) {
params['DataHeader'] = this.$.graphOptionsLoad.$.headerValue.selectedItem.label;
}
if (this.$.graphFunctionsLoad.$.functionValue.selectedItem) {
params['FunctionName'] = this.$.graphFunctionsLoad.$.functionValue.selectedItem.label;
}
this.$.sendOptions.params = JSON.stringify(params);
var x = this.$.sendOptions.go();
// this.$.sendOptions.go();
console.log(x)
// var ajax = document.querySelector("sendOptions");
var results = [];
this.addEventListener("core-response",
function(e) {
console.log(x.response);
}
);
}
});
And here's an example of the output from console.log(x.response);:
{
"graph": [
{
"Header": "MakeModeChange"
},
{
"x": [
0.0,
131.35,
26971.3,
27044.75,
27351.4,
27404.483333333334,
27419.416666666668,
33128.96666666667,
33549.13333333333,
34049.48333333333,
77464.26666666666,
77609.71666666666,
174171.85,
259166.98333333334
]
},
{
"y": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14
]
}
]
}
Ultimately I need the output to look something like:
['x', 0.0, 131.35, 26971.3, 27044.75, 27351.4, 27404.483333333334...],
['y', 1, 2, 3, 4, 5, 6]
Here's a quick and dirty way to do it - not recommended for large scale or if you don't fully trust the response values. Also, obvious, but if their api/data structures changes you're SOL.
xArr = JSON.parse(x.response).graph[1].x
xArr.unshift('x')
yArr = JSON.parse(y.response).graph[2].y
yArr.unshift('y')
You'll get both arrays you'll need, you can combine as needed
I'm having difficulty creating some code that dynamically creates graphs:
The following code snippet helps create the graph:
xGrid: false,
legend: true,
title: 'Meetings and Hours Used',
points: [
[7, 26, 33, 74, 12, 49, 33, 33, 74, 12, 49, 33],
[32, 46, 75, 38, 62, 20, 52, 75, 38, 62, 20, 52]
],
And I've replaced the points section with this:
points: <%= getJson() %>
And my code behind has function:
public string getJson()
{
var publicationTable = new List<object>{
new { points = "[1,2,3,4,5,6,7,8]," }
};
return (new JavaScriptSerializer()).Serialize(publicationTable);
}
The javascript doesn't seem to be parsing - can anyone please help me :)
Try this.
public string getJson() {
var publicationTable = new[] {
new[] { 1, 2, 3, 4, 5 },
new[] { 6, 7, 8, 9, 10 }
};
return (new JavaScriptSerializer()).Serialize(publicationTable);
}
List<List<int>> lstMainArr = new List<List<int>>();
lstMainArr.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ToList<int>());
lstMainArr.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ToList<int>());
Console.Write((new JavaScriptSerializer()).Serialize(lstMainArr));
points: <%= getJson() %>
This outputs whatever the return result type is, so for example:
points: 8 // getJson() returns int
points: test // getJson() returns a string
if you had done:
points = "[1,2,3,4,5,6,7,8],";
return points:
the result would be:
points = [1,2,3,4,5,6,7,8],
However you have:
var publicationTable = new List<object> {
new { points = "[1,2,3,4,5,6,7,8]," }
};
return (new JavaScriptSerializer()).Serialize(publicationTable);
So to determine the output, step through each outer to inner method/object.
Step 1: Serialization - Creates an empty string.
""
Step 2: Serialize List<Object> (basically a JavaScript array)
"[]"
Step 3: Serialize first anonymous Object:
"[{}]"
Step 4: Serialize first property :
"[{"points" : "[1,2,3,4,5,6,7,8],", }]"
No more properties, no more objects, serialization finished. This is not the JSON you were looking for.
I need some help! Im retrieving some values from mySQL database using an external 'grabber'.
<?php
$datapiechart = file_get_contents("url which retrieves the values from MySQL");
?>
Which results in:
[{ "Name1": 62, "Name2": 42, "Name3": 19, "Name4": 7, "Name5": 6, "Name6": 4, "Name7": 1, "Name8": 4, "Name9": 3, "Name10": 1, "Name11": 1, "Name12": 0 }]
Then I want to select the values in this array.
<SCRIPT>
dataObjectdatapiechart = <?php echo $datapiechart; ?>
</SCRIPT>
<script> dataObjectdatapiechart.Name1</script>
I don't get whats going wrong here.
dataObjectdatapiechart is an array (with only one element), so you need to access it's contents using an indexer:
var item = dataObjectdatapiechart[0]; // Retrieve the object from the array
var name1 = item.Name1;
var name2 = item.Name2;
var name3 = item.Name3;
//etc.
Use
dataObjectdatapiechart[0].Name1
The Object { "Name1": 62, "Name2": 42, "Name3": 19, "Name4": 7, "Name5": 6, "Name6": 4, "Name7": 1, "Name8": 4, "Name9": 3, "Name10": 1, "Name11": 1, "Name12": 0 }
is at 0th position of the array.