I have serialize object returning from Controller to blade view in laravel like:
"a:3:{i:0;s:1:"2";i:1;s:1:"4";i:2;s:1:"6";}"
from my blade view I use this JS code block to get those value as array.
var branches = {{unserialize($preliminary->branches)}};
But in there I'm getting error saying
expression expected
any suggestions to solve this situation..?
Run json_encode on top of your unserialize.
E.g.
var branches = {{json_encode(unserialize($preliminary->branches))}};
unserialize is giving you a PHP object, which you are trying to inject directly into JS. By passing it through json_encodeyou convert it to a string javascript can grok.
Related
I have a bunch of CSS properties stored in a MySQL database accessed via PHP. I need to make these properties available to JavaScript after the page has finished loading.
So what I did is foreach row, put the values in a Javascript object like so:
foreach ($cellcontent as $cellproperty) {
echo 'var '.$cellproperty->cell_id.' = {cellwidth:"'.$cellproperty->cell_width.'"};';
}
(For simplicity's sake I've only included one object property here but in reality there are many more.)
My problem is that at runtime, via JavaScript I get the cell_id reference which is somewhere in the html page like so:
var dacell = $(this).closest("div");
var cellid = dacell.attr("id");
So at this point, cellid is equal to the name of my var from the php output.
But when I try to get the property of my object (cellwidth) via JavaScript it doesn't work. Says its undefined when I try to see the value in an alert:
alert(cellid.cellwidth);
I think I'm just not referencing the actual object at this point and just trying to get a property of what has now become a string.
Is there a way to get back the reference to the object itself?
var cellid = dacell.attr("id");
The variable cellid is a string. Your hopes would be that the variable your are looking is in the global namespace which you can access via the following:
window[cellid].cellwidth
It's an awfull practice to pollute the global namespace with so much stuff.
Fetch all the values you need to inject into the JS, create an associative Array and inject it as a single JSON into the Page.
Nevermind everyone. The eval() javascript function fixed it all.
Instead of doing:
alert(cellid.cellwidth);
I did:
alert(eval(cellid).cellwidth);
and everything worked.
Thanks for all your time.
Cheers,
Erick P.
I have a JS function which I want to loop round a c# object passed from the controller. I then want to compare it to a HTML input value - which I have passed into the JS function as a string.
Function is called like
searchReleases($("[name=release]").val());
function searchReleases(givenName) {
var list;
var data = givenName;
#foreach (var a in #Model.Releases) {
if (#a.Name.Contains(givenName)) {
list+= a.Name + "=";
}
}
}
However I can't access the JS variable givenName within the IF statement.
I have spent a while trying to find an answer on google but have yet found a workable solution.
You should understand that C# is a server-side code and JS is executed on client-side.
So if you generate view in ASP.MVC you have access only to C# data. All information which you want to print on your page will be statically generated to HTML file.
Then you can pass this HTML page to browser and inside it, it would be executed JS code. But only based on statically generated data or JS variables.
You can't read JS from C#, but you can read C# from JS. So if it's a read-only operation, than convert your C# object to JSON and access it from the JS code.
Try this answer : Asp.net mvc passing a C# object to Javascript
I have some JavaScript code that needs to be able to access fields of an array of objects that is contained within my model. I currently have this:
var model = #Html.Raw(Json.Encode(Model));
for(var i = 0; i < model.testobject.length; i++) {
console.log(model.testobject[i]);
}
Which prints out the fields within each object of testobject. But say I have a field, ID, in my testobject class. How do I then access that? Doing this:
console.log(model.testobject[i].ID);
Does not work. Do I have to somehow encode that specific instance of testobject before accessing it's fields?
And yes, before anyone says it I know this should be contained within the controller. As it currently stands though, that's not possible for this project.
This is the general structure of what is printed out:
Object {field: value}
Edit:
I attempted to use JSON.stringify on my model.IdentifiApprovalConfigurations and it seems I got a little close to reaching my solution. This is what it looks like now:
console.log(JSON.stringify(model.testobject[i]).ID);
However, this prints out undefined.
Edit 2:
Oops, seems the ID field I'm trying to access isn't being populated before I send them to my view which is my own issue. JSON.stringify works though, and I understand why it wasn't working earlier.
Final edit:
JSON.parse(JSON.stringify(model.testobject[i])).Value
I had to stringify and then parse my JSON to access the value.
As I'm still starting out with web development, I forgot that I needed to convert my object into a JSON object. This:
JSON.parse(JSON.stringify(model.testobject[i])).Value
Is the final piece of code that allows me to stringify an object, parse the JSON and then access fields within that object.
I am using laravel 4 and I want to pass a data with a view.
I have used this code in a controller.
$view = View::make('settings.editEvent');
$view->bounderyData = $bounderyData;
And I want to check whether this data exists or not in the view settings/editEvent.blade.php
Tried using this..
<script>
if('{{$bounderyData.length()}}'!=null)
console.log('exists');
</script>
Error :
Array to string conversion error
How can I check the existence ?
Do not assign the data to the View variable, but instead, pass it along using with as Laravel requests you to use:
$view = View::make('settings.editEvent')
->with('bounderyData', $bouderyData);
Actually both of the snippets work the same way. You can either pass data using with() method or by assigning it to view as property. So it doesn't really matter. But it looks like you are using some weird syntax because you are trying to access method length() using dot syntax inside Blade echo statement. Try:
if({{count($bounderyData)}}!=null)
console.log('exists');
or something similar. Remember that everything inside {{}} is going to be echo'ed by PHP. So if you have some sort of array there you may either want to count number of elements or maybe cast it to JSON and then decode it inside Javascript. If you still have problem, let us know what is the issue.
I have a JavaScript function which returns a set of objects:
return Func("{{id}}", "{{name}}")
I have a problem with passing strings containing quotes, for instance "Dr.Seuss' "ABC""BOOk"" is invalid syntax.
I tried {{name|safe}} but to no avail. Any solution for this?
If I'm right in assuming that's a JavaScript function call you're trying to interpolate Django templating into, try the escapejs filter instead of safe.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs