jongyanlaw
(Siongjin Law)
November 4, 2015, 7:15am
1
Hi
I have a josn file http://xxx.com/api/feed/499912.json?multi_brand=1
And successfully get the data and display in Cgridview but only can get page 1 data only. Tat have total of 5 pages
Content of json file like below
{
items: [],
total_page: 5,
total_item: 93,
page: 1,
paging: {
next: "http://xxx.com/api/feed/499912.json?multi_brand=1&page=2 "
},
brand: {}
}
So what should i do to get all the data from total 5 pages?
My controller as below :
$data = array();
$data = file_get_contents('http://xxx.com/api/feed/499912.json?multi_brand=1');
$data = json_decode($data, true);
You should get "page" param passed to action controller when you go forward in CGridView pagination.
Then you will pass this param to json request to get the new content.
jongyanlaw
(Siongjin Law)
November 4, 2015, 8:20am
3
Sorry i’m new @_ @ can provide some sample of coding?
In action controller
public function actionGrid()
{
$page = Yii::app()->request->getQuery('page');
...
}
jongyanlaw
(Siongjin Law)
November 4, 2015, 8:24am
5
ok …i try and c…but sorry i’m really noob…so if possible can provide me more details code. Thank you very much
Post your code so I can add suggestions.
jongyanlaw
(Siongjin Law)
November 4, 2015, 8:33am
7
sample file
{
items: [],
total_page: 5,
total_item: 93,
page: 1,
paging: {
next: "http://xxx.com/api/feed/499912.json?multi_brand=1&page=2 "
},
brand: {}
}
ok in my controller
$data = array();
$data = file_get_contents('http://xxx.com/api/feed/499912.json?multi_brand=1');
$data = json_decode($data, true);
$data = array_shift($data); //move up level
$dataProvider = new CArrayDataProvider($data, array(
'keyField' => 'nid',
'pagination' => array(
'pageSize'=>20,
),
));
$this->render('index',array(
'dataProvider' => $dataProvider,
));
}
So what should i do next?
<?php
public function actionGrid()
{
// 'page' is the parameter passed from the CGridView when you change page in pagination control
$page = Yii::app()->request->getQuery('page');
$data = array();
$data = file_get_contents('http://xxx.com/api/feed/499912.json?multi_brand=1&page='.$page);
$data = json_decode($data, true);
$data = array_shift($data); //move up level
// you have to populate this from json
$totalItemFromJson = ...
$dataProvider = new CArrayDataProvider($data, array(
'keyField' => 'nid',
'pagination' => array(
'pageSize'=>20,
'currentPage' => $page
'itemCount' => $totalItemFromJson
),
));
$this->render('index',array(
'dataProvider' => $dataProvider,
));
}
}
?>
jongyanlaw
(Siongjin Law)
November 4, 2015, 8:45am
9
// you have to populate this from json
$totalItemFromJson = ...
Sorry totally noob…wat should i put here?
reykel
(Rhpalacios66)
November 5, 2015, 4:14am
10
Ok, listen bro Fabrizio answer was excellent, but probably too advanced. I suggest you to use ajax to load your JSon and later usin JQuery serialize to your Grid. I can explain a little more if you’re interested…
jongyanlaw
(Siongjin Law)
November 5, 2015, 4:15am
11
reykel:
Ok, listen bro Fabrizio answer was excellent, but probably too advanced. I suggest you to use ajax to load your JSon and later usin JQuery serialize to your Grid. I can explain a little more if you’re interested…
Please help with details coding…i totally noob @_ @ thanks for your help
reykel
(Rhpalacios66)
November 5, 2015, 4:17am
12
Any way I recommend you to study a little more the framework cause is hard to try to help you like this… One more time Fabrizio gave you an excellent and clear answer but you have to be ready to get it…
jongyanlaw
(Siongjin Law)
November 5, 2015, 6:14am
13
@_ @ i’m trying… but hopefully bro Fabrizio can help me
reykel
(Rhpalacios66)
November 5, 2015, 11:07am
14
Here my friend some piece of code with ajax and a GridView:
Hello my friend I found this post I made a few days ago, I think it can be helpfull
Quick example using Ajax with an action (on php server-side of course) and JQuery on the client side.
View:
Yii::app()->clientScript->registerScript(‘dataScript’, "
function upData_click() {
var url = ‘index.php?r=MyExample/getJSonData’;
$.ajax({
url: url,
type: ‘post’,
success: function(resp){
$(’#MyGrid-grid ’).yiiGridView(‘update’, {
resp: $(this).serialize()
});
return false;
},
error: function(){
alert('it’s been an error);
}
});
});
}
");
Controller…
public function actionGetJSonData($id)
{
$model = MyModel::model()->find(‘ID = ?’, array($id));
header("Content-type: application/json");
echo CJSON::encode($model);
}
Tell meif it work I’ll be on-line later… Good luck my friend…we’re in communication…
reykel
(Rhpalacios66)
November 5, 2015, 11:12am
15
You’re in two posts with similar bases problems… try to find a connection with the code I sent you in both posts. Check for both…