CakePHP 3,如果字段为空则显示整个记录行,否则不显示

时间:2016-12-18 17:41:04

标签: cakephp cakephp-3.0

我本周刚刚开始使用CakePHP(实习所需),而且我开始使用PHP并不是很好。 无论如何,我需要以下帮助:

如果记录只有空字段,怎么才能显示?

Sample reference

根据上面的图片,我只希望显示带有空标题/日期的行,同时隐藏另一行。

在另一个页面上,我希望显示相同的记录,但这一次,只显示具有完全填充字段的记录,同时隐藏具有空字段的记录。

编辑:

查看:

<?php foreach ($users as $user): ?>
        <tr>
            <td><?= $this->Number->format($user->id) ?></td>
            <td><?= h($user->username) ?></td>
            <td><?= h($user->name) ?></td>
            <td><?= h($user->phone) ?></td>
            <td><?= h($user->email) ?></td>
            <td><?= h($user->role) ?></td>
            <td><?= $this->Number->format($user->status) ?></td>
            <td class="actions">
                <?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
                <?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
                <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>

型号:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('users');
    $this->displayField('name');
    $this->primaryKey('id');
}

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->notEmpty('username', 'A username is required');

    $validator
        ->notEmpty('password', 'A password is required');

    $validator
        ->allowEmpty('name');

    $validator
        ->allowEmpty('phone');

    $validator
        ->email('email')
        ->allowEmpty('email');

    $validator
        ->add('role', 'inList', [
            'rule' => ['inList', ['admin', 'editor', 'sales_user', 'field_user']],
            'message' => 'Please select a role']);

    $validator
        ->add('status', 'inList', [
            'rule' => ['inList', ['1', '2', '3']],
            'message' => 'Please select a status']);

    return $validator;
}

控制器:

public function index()
{
    $users = $this->paginate($this->Users);

    $this->set(compact('users'));
    $this->set('_serialize', ['users']);
}

这是我第一次发帖提问,如果我需要提供任何进一步的细节等,请告诉我。 谢谢。

1 个答案:

答案 0 :(得分:0)

此代码选择名称字段为空的用户。

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        presenter.onCukRulesClicked();

        final LinearLayout checkboxesContainer = (LinearLayout) findViewById(R.id.checkboxes_container);

        final LinearLayout rootView = (LinearLayout) findViewById(R.id.linearLayout2345);

        final LinearLayout leftContainer = (LinearLayout) findViewById(R.id.left_container);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            if (isChecked) {
                leftContainer.removeView(checkboxesContainer);
                leftContainer.invalidate();
                rootView.addView(checkboxesContainer);
            } else {
                rootView.removeView(checkboxesContainer);
                rootView.invalidate();
                leftContainer.addView(checkboxesContainer);
            }
    }