10 Yii::import(
'booster.widgets.TbGridView');
11 Yii::import(
'booster.widgets.TbJsonDataColumn');
31 public $template =
"{pager}\n{items}\n{summary}\n{pager}";
55 public $pager = array(
'class' =>
'booster.widgets.TbJsonPager');
68 $this->json = Yii::app()->getRequest()->getIsAjaxRequest();
70 $this->_showSummary = (strpos($this->
template,
'{summary}')) ?
true :
false;
71 $this->
template =
'{items}';
87 $this->registerClientScript();
88 $this->renderContent();
103 if (is_string($this->pager)) {
105 }
else if (is_array($this->pager)) {
107 if (isset(
$pager[
'class'])) {
112 $pager[
'pages'] = $this->dataProvider->getPagination();
114 if (
$pager[
'pages']->getPageCount() > 1) {
118 return $widget->run();
129 foreach ($this->columns as
$i => $column) {
130 if (is_array($column) && !isset($column[
'class'])) {
131 $this->columns[
$i][
'class'] =
'booster.widgets.TbJsonDataColumn';
144 echo function_exists(
'json_encode')
150 } elseif ($this->dataProvider->getItemCount() > 0 || $this->showTableOnEmpty) {
151 echo
"<table class=\"{$this->itemsCssClass}\">\n";
152 $this->renderTableHeader();
155 $body = ob_get_clean();
156 $this->renderTableFooter();
162 $this->renderEmptyText();
174 if (!$this->_showSummary)
181 $summary = ob_get_clean();
183 'class' => $this->summaryCssClass,
193 echo $this->
renderTemplate($this->
id .
'-col-template',
'<td <%=this.attrs%>><%=this.content%></td>');
195 $this->
id .
'-row-template',
196 '<tr class="<%=this.class%>"><% var t = "#' . $this->
id .
'-col-template"; out += $.jqote(t, this.cols);%></tr>'
198 echo $this->
renderTemplate($this->
id .
'-keys-template',
'<span><%=this%></span>');
201 $this->
id .
'-pager-template',
202 '<li class="<%=this.class%>"><a href="<%=this.url%>"><%=this.text%></a></li>'
206 $this->
id .
'-summary-template',
207 '<div class="<%=this.class%>"><%=this.text%></div>'
222 return "<script type=\"text/x-jqote-template\" id=\"{$id}\">\n<![CDATA[\n{$text}\n]]>\n</script>";
230 $data = $this->dataProvider->getData();
234 return $this->renderTableBodyJSON($n);
237 echo CHtml::openTag(
'tbody');
240 for ($row = 0; $row < $n; ++$row) {
241 $this->renderTableRow($row);
244 echo
'<tr><td colspan="' . count($this->columns) .
'" class="empty">';
245 $this->renderEmptyText();
249 echo CHtml::closeTag(
'tbody');
259 protected function renderTableBodyJSON($rows)
262 'headers' => array(),
267 foreach ($this->columns as $column) {
272 $tbody[
'headers'][] = $column->renderHeaderCell();
277 for ($row = 0; $row < $rows; ++$row) {
281 foreach ($this->dataProvider->getKeys() as $key) {
282 $tbody[
'keys'][] = CHtml::encode($key);
287 $this->renderEmptyText();
288 $content = ob_get_contents();
291 $tbody[
'rows'][0][
'cols'][] = array(
292 'attrs' =>
"colspan=\"" . count($this->columns) .
"\"",
293 'content' => $content
295 $tbody[
'rows'][0][
'class'] =
" ";
298 $tbody[
'url'] = Yii::app()->getRequest()->getUrl();
308 public function renderTableRow($row)
310 if ($this->rowCssClassExpression !== null) {
311 $data = $this->dataProvider->data[$row];
312 echo
'<tr class="' . $this->evaluateExpression(
313 $this->rowCssClassExpression,
314 array(
'row' => $row,
'data' =>
$data)
316 }
else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0) {
317 echo
'<tr class="' . $this->rowCssClass[$row % $n] .
'">';
321 foreach ($this->columns as $column) {
323 $column->renderDataCell($row);
338 if ($this->rowCssClassExpression !== null) {
339 $data = $this->dataProvider->data[$row];
340 $json[
'class'] = $this->evaluateExpression(
341 $this->rowCssClassExpression,
342 array(
'row' => $row,
'data' =>
$data)
344 }
else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0) {
345 $json[
'class'] = $this->rowCssClass[$row % $n];
349 foreach ($this->columns as $column) {
350 $json[
'cols'][] = $column->renderDataCell($row);
366 if (!preg_match(
'/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
367 throw new CException(Yii::t(
369 'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'
374 $column->name = $matches[1];
376 if (isset($matches[3]) && $matches[3] !==
'') {
377 $column->type = $matches[3];
380 if (isset($matches[5])) {
381 $column->header = $matches[5];
390 public function registerClientScript()
392 $id = $this->getId();
394 if ($this->ajaxUpdate ===
false) {
397 $ajaxUpdate = array_unique(
398 preg_split(
'/\s*,\s*/', $this->ajaxUpdate .
',' . $id, -1, PREG_SPLIT_NO_EMPTY)
402 'ajaxUpdate' => $ajaxUpdate,
403 'ajaxVar' => $this->ajaxVar,
404 'pagerClass' => $this->pagerCssClass,
405 'summaryClass' => $this->summaryCssClass,
406 'loadingClass' => $this->loadingCssClass,
407 'filterClass' => $this->filterCssClass,
408 'tableClass' => $this->itemsCssClass,
409 'selectableRows' => $this->selectableRows,
410 'enableHistory' => $this->enableHistory,
411 'updateSelector' => $this->updateSelector,
412 'cacheTTL' => $this->cacheTTL,
413 'cacheTTLType' => $this->cacheTTLType,
414 'localCache' => $this->localCache
416 if ($this->ajaxUrl !== null) {
417 $options[
'url'] = CHtml::normalizeUrl($this->ajaxUrl);
419 if ($this->enablePagination) {
420 $options[
'pageVar'] = $this->dataProvider->getPagination()->pageVar;
423 foreach (array(
'beforeAjaxUpdate',
'afterAjaxUpdate',
'ajaxUpdateError',
'selectionChanged') as $prop) {
424 if ($this->{$prop} !== null) {
425 if ((!$this->{$prop} instanceof CJavaScriptExpression) && strpos($this->{$prop},
'js:') !== 0) {
426 $options[$prop] =
new CJavaScriptExpression($this->{$prop});
428 $options[$prop] = $this->{$prop};
433 $options = CJavaScript::encode($options);
435 $cs = Yii::app()->getClientScript();
436 $cs->registerCoreScript(
'jquery');
437 $cs->registerCoreScript(
'bbq');
438 if ($this->enableHistory) {
439 $cs->registerCoreScript(
'history');
442 $cs->registerPackage(
'json-grid-view');
444 $cs->registerScript(__CLASS__ .
'#' . $id,
"jQuery('#$id').yiiJsonGridView($options);");