yii 1.1中的CGridView将日期排序为数字而不是日期

时间:2017-05-31 12:33:28

标签: php yii yii2 yii-components yii1.x

我是YII 1.1框架的新手,我必须调试代码。这里的小部件CGridView将日期排序为数字而不是日期。那是01-02-2017,10-01-2017,15-01-2017,21-12-2016。 它应按月份和年份分类,即21-12-2016,10-01-2017,15-01-2017,01-02-2017。

我的观点代码如下: -

<article class="listBox list-view">
                    <?php
                    $this->widget('zii.widgets.grid.CGridView', array(
                        'id' => 'child-invoice-payments-grid',
                        'htmlOptions' => array('class' => 'table-responsive'),
                        'itemsCssClass' => 'table invoiceTable',
                        'summaryText' => '',
                        'dataProvider' => new CArrayDataProvider($response, array(
                           'id' => 'id',
                            'sort' => array(
                                //'defaultOrder'=>'date_of_payment ASC',
                                'attributes' => array(
                                    'date_of_payment','id', 'payment_mode','type','amount'
                                )
                            ),
                            'pagination' => array(
                                'pageSize' => 10
                            )
                        )),
                        'enablePagination' => true,
                        'pagerCssClass' => 'text-center',
                        'pager' => array(
                            'header' => '',
                            'maxButtonCount' => 4,
                            'firstPageLabel' => '<<',
                            'prevPageLabel' => '<',
                            'nextPageLabel' => '>',
                            'lastPageLabel' => '>>',
                            'pageSize' => 5,
                            'htmlOptions' => array(
                                'class' => 'pagination',
                            )
                        ),
                        'columns' => array(
                            array(
                                'name' => 'id',
                                'type' => 'raw',
                                'header' => 'Transaction ID',
                                'value' => 'CHtml::link($data["id"], $data["url"])'
                            ),
                            **array(
                                'name' => 'date_of_payment',
                                'type' => 'raw',
                                'header' => 'Payment Date',
                                'value' => 'CHtml::link($data["date_of_payment"], $data["url"])'
                            )**,
                            array(
                                'name' => 'payment_mode',
                                'type' => 'raw',
                                'header' => 'Payment Mode',
                                'value' => 'CHtml::link($data["payment_mode"], $data["url"])'
                            ),
                            array(
                                'name' => 'type',
                                'type' => 'raw',
                                'header' => 'Payment Type',
                                'value' => 'CHtml::link($data["type"], $data["url"])'
                            ),
                            array(
                                'name' => 'amount',
                                'type' => 'raw',
                                'header' => 'Amount',
                                'value' => 'CHtml::link($data["amount"], $data["url"])'
                            )
                        )
                    ));
                    ?>
                </article>

并且控制器的代码如下: -

public function actionPayments($child_id) {
    $response = array();
    $criteria = new CDbCriteria();
    $criteria->condition = "child_id = :child_id AND (invoice_type = 0 OR invoice_type = 1)";
    $criteria->params = array(':child_id' => $child_id);
    $invoiceModel = ChildInvoice::model()->findAll($criteria);
    if (!empty($invoiceModel)) {
        foreach ($invoiceModel as $invoice) {
            $transactionModel = ChildInvoiceTransactions::model()->findAllByAttributes(array(
                'invoice_id' => $invoice->id, 'credit_note_id' => NULL, 'payment_id' => NULL));
            if (!empty($transactionModel)) {
                foreach ($transactionModel as $transaction) {
                    $temp = array();
                    $temp['id'] = "Invoice Payment-" . $transaction->id;
                    $temp['amount'] = $transaction->paid_amount;
                    $temp['date_of_payment'] = $transaction->date_of_payment;
                    $temp['payment_mode'] = customFunctions::getPaymentOptionName($transaction->payment_mode);
                    $temp['type'] = "Invoice Payment";
                    $temp['url'] = Yii::app()->createUrl('childInvoice/view', array('child_id' => $invoice->child_id,
                        'invoice_id' => $invoice->id));
                    $response[] = $temp;
                }
            }
        }
    }
    $creditNotesModel = ChildInvoice::model()->findAllByAttributes(array('child_id' => $child_id,
        'invoice_type' => 3, 'is_deposit' => 1), array('order' => 'invoice_date'));
    if (!empty($creditNotesModel)) {
        foreach ($creditNotesModel as $creditNote) {
            $temp = array();
            $temp['id'] = "Credit Note-" . $creditNote->invoiceUrn;
            $temp['amount'] = sprintf("%0.2f", -$creditNote->total);
            $temp['date_of_payment'] = $creditNote->invoice_date;
            $temp['payment_mode'] = $creditNote->description;
            $temp['type'] = "Deposit";
            $temp['url'] = Yii::app()->createUrl('childInvoice/updateCreditNote', array(
                'id' => $creditNote->id, 'child_id' => $creditNote->child_id));
            $response[] = $temp;
        }
    }
    $paymentsModel = Payments::model()->findAllByAttributes(array('branch_id' => Yii::app()->session['branch_id']), array(
        'order' => 'date_of_payment'));
    if (!empty($paymentsModel)) {
        foreach ($paymentsModel as $payments) {
            if (in_array($child_id, explode(",", $payments->child_id))) {
                $temp = array();
                $temp['id'] = "Payment-" . $payments->id;
                $temp['amount'] = sprintf("%0.2f", $payments->amount);
                $temp['date_of_payment'] = $payments->date_of_payment;
                $temp['payment_mode'] = customFunctions::getPaymentOptionName($payments->payment_mode);
                $temp['type'] = "Payments";
                $temp['url'] = Yii::app()->createUrl('payments/view', array('id' => $payments->id));
                $response[] = $temp;
            }
        }
    }
    usort($response, function($i, $j) {
        $a = strtotime(date("Y-m-d", strtotime($i['date_of_payment'])));
        $b = strtotime(date("Y-m-d", strtotime($j['date_of_payment'])));
        if ($a == $b)
            return 0;
        elseif ($a > $b)
            return 1;
        else
            return -1;
    });
    $this->render('payments', array(
        '**response**' => $response,
    ));
}

所以看起来像这样的响应数组必须格式化: -

[0] => Array
    (
        [id] => Payment-8
        [amount] => 100.00
        [date_of_payment] => 06-03-2017
        [payment_mode] => Cash
        [type] => Payments
        [url] => /new_management/index.php/payments/8
    )

[1] => Array
    (
        [id] => Payment-12
        [amount] => 1500.00
        [date_of_payment] => 22-03-2017
        [payment_mode] => Bank/Standing Order
        [type] => Payments
        [url] => /new_management/index.php/payments/12
    )

[2] => Array
    (
        [id] => Payment-14
        [amount] => 150.00
        [date_of_payment] => 27-03-2017
        [payment_mode] => Cheque
        [type] => Payments
        [url] => /new_management/index.php/payments/14
    )

以下是页面截图和问题: - enter image description here

0 个答案:

没有答案