Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

JQUERY JTABLE


Go to End


29 Posts   35682 Views

Avatar
sergieboy

Community Member, 33 Posts

2 January 2012 at 11:33pm

Thanks for your reply and helpfull info. If it works, I'll let you know.

Avatar
sergieboy

Community Member, 33 Posts

3 January 2012 at 2:35am

I mapped the expected fields to the fields of the People table:
fields: {
PersonId: {
key: true,
create: false,
edit: false,
list: false
},
Name: {
title: 'Author Name',
width: '40%'
},
Age: {
title: 'Age',
width: '20%'
},
RecordDate: {
list: false,
create: false,
edit: false
}
JTableExampleID: {
list: false,
create: false,
edit: false
}
ID: {
list: false,
create: false,
edit: false
}

The result that JTable expects should look like this :
---------------------------------------------------------------------------------------------------------
function showdata(){
$myset = DataObject::get('People');
$myArray = $myset->toArray();
if ($myArray){
$json = '{Result: "OK", Records : $myArray }';
}
else{
$json = '{Result: "ERROR", Message : "error occured" }';
}
return $json;
}
------------------------------------------------------------------------------------------------------------
This controller should be called from within the customscript by defining:

actions: {
listAction: 'http://localhost/silverstripe/new-jtableexample/showdata'
}
But helas, jumping from the customscript to the controllerfunction showdata() doesn't work.

Avatar
MarcusDalgren

Community Member, 288 Posts

3 January 2012 at 3:26am

showdata is not returning valid JSON.
I don't know what browser you're using but you need some kind of tool so you can watch the AJAX request that jTable does when it loads the records. That way you can check if the correct URL is being called, otherwise you're never going to be able to troubleshoot this properly. If you're using FireFox you can install Firebug and watch the AJAX requests from the console.

$myArray is an array of objects so running json_encode() on it won't work either I think, that's why you have to use JSONDataFormatter.

$json = '{Result: "OK", Records : $myArray }'; 

I'm not sure how long you've been coding PHP but when you're using ' as a string delimiter variables aren't evaluated but are treated as literal strings so you're actually sending back exactly the text you have there.
A much better way of treating your data is to do this instead:

if ($myArray){
	$json = $json = array("Result" => "OK", "Records"  => $myArray);
}
else{
	$json = array("Result" => "ERROR", "Message"  => "error occured");
} 
return json_encode($json); 

But like I said before, json_encode doesn't work on objects and $myArray is an array of objects.

Avatar
sergieboy

Community Member, 33 Posts

3 January 2012 at 4:25am

Thanks.
Concerning PHP : forced to learn it since 2 months. History of 12 y of Java, Java EE and .Net (C#).
New needs pushed me to investigate PHP + Joomla (drop after 1 week), PHP + Zend (very robust but no ready to go platform for web solutions) and finally discovered by hasard something called Silverstripe. Nobody in my environment has heard of it. But it fascinates me. Has also some drawbacks for my needs (concerning ORM) but no platform is a 100% hit for a specific besoin.

PHP is so difficult for me since I feel very,very uncomfortable with the weak typing ...I'm Java-drilled...

Thanks for your help.

Avatar
sergieboy

Community Member, 33 Posts

5 January 2012 at 4:43am

In the context of getting this JTable plugin at work, I still have a persistent problem with the mutation of the DataObjectSet
into an array with the method toArray(). I have a DataObject called People with properties : PersonId, Name, Age, RecordDate.
From the controller, I want to retrieve all the records in People into a DataObjectSet using $result = DataObject::get('People');

Then, I need to make an array of it. If I use the method toArray(), I have following result:

[{"destroyed":false,"class":"People"},{"destroyed":false,"class":"People"}]

This is not what I need. I would need the following format :

[ {"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"}, {"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"} ]

So, toArray() method misled me. How do I get to the needed format ?

Avatar
MarcusDalgren

Community Member, 288 Posts

6 January 2012 at 3:25am

So like I talked about before toArray() just gives you an array of objects which isn't what you need.
I haven't seen your people class but I'm going to assume that the fields you need in the JSON exist on the dataobject.
With that in mind, I'd do something like this:

function showdata(){
	$myset = DataObject::get('People');
	$jsonArray = array();
	foreach ($myset as $person) {
		$jsonArray[] = array(
			"PersonId" => $person->PersonId,
			"Name" => $person->Name,
			"Age" => $person->Age,
			"RecordDate" => $person->RecordDate
		);
	}   
	if (count($jsonArray) > 0){
		$json = array("Result" => "OK", "Records" => $jsonArray);
	}
	else {
		$json = array("Result" => "ERROR", "Message" => "error occured");
	} 
	return json_encode($json);
} 

Avatar
sergieboy

Community Member, 33 Posts

6 January 2012 at 8:53pm

Thanks for your cristal input. JSON return is now what JTable's expecting.
But it still doesn't work, and when I launch it by FireFox with FireBug on it, it gives the error : JQUERY not defined.
I tried for several hours to look where this error could be originated.
All the JS (JQUERY) and CSS are in place.
But this error remains.
Screenshots in att.

Attached Files
Avatar
martimiz

Forum Moderator, 1391 Posts

7 January 2012 at 12:03am

Does loading jquery.table after the main jquery/jquery-ui library (in your controller) help?