【问题标题】:Joomla - Populating JModelList (adding objects to items)Joomla - 填充 JModelList(向项目添加对象)
【发布时间】:2013-10-04 08:47:05
【问题描述】:

这是我扩展 JModelList 的类:

class AkceHnedModelActions extends JModelList
{
   public function getItems()
   {
      $url = '...';

      $ch = curl_init($url);
      curl_setopt_array($ch, array(
      CURLOPT_RETURNTRANSFER => TRUE
      ));

      // aby me https fungovalo na localhostu, na serveru potom odstranit
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

      // Send the request
      $getResponse = curl_exec($ch);
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

      if($getResponse == FALSE){
         die(curl_error($ch));
      }
      else
      {
         $results = array();
         $jsonObject = json_decode($getResponse, true);
         foreach ($jsonObject AS $key => $value) {
            $obj = new Action($value);
            $results[] = $obj;
         }
         $this->items = $results;
      }
      return $this;
   }
}
class Action
{
   function __construct(array $data) {
      foreach($data as $key => $val) {
         $this->{$key} = $val;
      }
   }
}

我试图在 View 的表中为每个对象拥有自己的行。我有这个代码供查看:

<table>
   <tbody>
      <?php foreach ($this->items as $i => $item): ?>
         <tr class="row"><td><?php echo $item->id ?></td>
         <td><?php print_r($item) ?></td>
         </tr>
      <?php endforeach ?>
   </tbody>
</table>

我无法让它工作。我将所有数组放在一行中。如何创建可以分配给项目的列表?

【问题讨论】:

  • 尝试将您的__construct 函数放在getItems 函数之上并放在同一个类中。它不应该有自己的类
  • 您的 AkceHnedModelActions::getItems() 方法返回应该只返回项目列表的整个对象的原因是什么?它不应该返回 $this->items 而不是 $this

标签: php templates joomla joomla2.5


【解决方案1】:

以下方法:

(array) AkceHnedModelActions::getItems()

返回应该只返回项目列表的整个对象。 我相信它应该返回 $this->items 而不是 $this。

使用当前代码,您的 JView::display() 可能会为整个对象分配一个引用:

$items = $this->get('Items'); // (object) AkceHnedModelActions
$this->assignRef('items', $items);

在你的 default.php 模板中你迭代对象的公共属性而不是列表数组的元素。

foreach ($this->items as $i => $item)

值得检查 $i 键是否是“项目”...这只会证实我的理论...

编辑:JModelItem 类的片段:

/**
 * Method to get an array of data items.
 *
 * @return  mixed  An array of data items on success, false on failure.
 *
 * @since   11.1
 */
public function getItems()
{

【讨论】:

  • 谢谢,正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2011-12-15
  • 2019-07-31
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多