PHP YII 2 - 如何从A-Z或Z-A中对数据进行排序?

时间:2016-10-17 07:36:43

标签: php sorting yii2

我想基于字母表在gridview中import java.util.ArrayList; import java.util.List; import com.couchbase.spark.japi.CouchbaseDocumentRDD; import com.couchbase.client.java.document.AbstractDocument; JavaSparkContext jsc = new JavaSparkContext(sc); SQLContext sql = new SQLContext(jsc); JsonDocument doc1; JsonArrayDocument jrd; List<AbstractDocument> list = new ArrayList<AbstractDocument>(); list.add(doc1); list.add(jrd); JavaRDD<AbstractDocument> jRDD = jsc.parallelize(list); CouchbaseDocumentRDD<AbstractDocument> cbRDD = CouchbaseDocumentRDD.couchbaseDocumentRDD(jRDD); cbRDD.saveToCouchbase(); 我的数据,如果ID包含数字,例如1-100或100-1,我的代码是按ID排序的。 如何从A-Z或Z-A SORT?是否可以自定义sort

我在控制器中有一个代码:

Yii2 sort

并在视图中:

public function actionItemreport()
{
    $modelFunctions = new functions;
    $modelFunctions->checkingUser( $this, Yii::$app->controller->id, Yii::$app->controller->action->id );

    $this->transfield = array( 'icitem' => $modelFunctions->getFieldTranslationTable( 'icitem' ), 'iclink' => $modelFunctions->getFieldTranslationTable( 'iclink' ) );

    $arrDays = array( '0' => $this->arrtrans[134], '1' => $this->arrtrans[135], '2' => $this->arrtrans[136], '3' => $this->arrtrans[137], '4' => $this->arrtrans[138], '5' => $this->arrtrans[139], '6' => $this->arrtrans[140] );  
    $arrSchedule = $this->getItemReportScheduler();

    $ftpSql = "SELECT * FROM icenvinit";
    $a_icenvinit = Yii::$app->db->createCommand( $ftpSql )->queryOne();

    if( $a_icenvinit['hostaddr1'] != "" )
    {
        $arr_ftp['FTP1'] = $this->arrtrans[141];
    }

    if( $a_icenvinit['hostaddr2'] != "" )
    {
        $arr_ftp['FTP2'] = $this->arrtrans[142];
    }

    switch ( intval($arrSchedule['type']) )
    {
        case 1 :    $listSchedule = "[ " . $this->arrtrans[143] . " ] : {$arrSchedule['hour']}:{$arrSchedule['minute']}";
                    break;
        case 2 :    $days = $arrDays[$arrSchedule['day']];
                    $listSchedule = "[ " . $this->arrtrans[144] . " ] : {$days} {$arrSchedule['hour']}:{$arrSchedule['minute']}";
                    break;
        case 3 :    $date = $arrSchedule['date'];
                    $listSchedule = "[ " . $this->arrtrans[145] . " ] : {$date} {$arrSchedule['hour']}:{$arrSchedule['minute']}";
                    break;
        case 4 :    $listSchedule = "[ " . $this->arrtrans[146] . " ] : Every {$arrSchedule['interval']} Hour(s)";
                    break;  
        default :   $listSchedule = $this->arrtrans[147];
                    $listTarget = "";
                    break;
    }

    $sql = "SELECT * FROM icdbval";

    $count = Yii::$app->db->createCommand( "select count(1) from icdbval" )->queryScalar();


    $arrsort['attributes'] = array( 'id' => array( 'label' => $this->arrtrans[148] ) );
    $arrsort['attributes'] += array( 'orgfield' => array( 'label' => $this->arrtrans[149] ) ); //, 'asc' => array('orgfield' => SORT_ASC)
    $arrsort['attributes'] += array( 'status' => array( 'label' => $this->arrtrans[91] ) );
    $arrsort['defaultOrder'] = array( 'id' => SORT_ASC );

    $sort = $arrsort;

    $a = 0;
    $arrview = array();

    $arrview[$a]['attribute'] = 'id';
    $arrview[$a]['label'] = $this->arrtrans[148];
    $arrview[$a]['encodeLabel'] = false;
    $arrview[$a++]['value'] = function ( $data )
    {
        return $data['id'];
    };

    $arrview[$a]['attribute'] = 'orgfield';
    $arrview[$a]['label'] = $this->arrtrans[149];
    $arrview[$a]['encodeLabel'] = false;
    $arrview[$a++]['value'] = function ( $data )
    {
        return $this->get_trans_field( $data['orgfield'] );
    };

    $arrview[$a]['attribute'] = 'status';
    $arrview[$a]['label'] = $this->arrtrans[91];
    $arrview[$a]['encodeLabel'] = false;
    $arrview[$a]['format'] = 'raw';
    $arrview[$a++]['value'] = function ( $data )
    {
        if( $data['status'] == "0" )
        {
            $select0 = "selected";
            $select1 = "";

        }
        else
        {
            $select1 = "selected";
            $select0 = "";      
        }

        $id = $data['id'];

        $ni = $this->arrtrans[150];
        $in = $this->arrtrans[151];

        $html = <<< "SCRIPT"
            <select class="_statusselect" theval="$id">
                <option value="0" $select0>$ni</option>
                <option value="1" $select1>$in</option>
            </select>
SCRIPT;
        // return $data['status'];
        return $html;
    };

    $collumnview = $arrview;

    $dataProvider = new SqlDataProvider([
        'sql' => $sql,
        'totalCount' => $count,
        'sort' => $sort,
        'pagination' => [
            'pageSize' => Yii::$app->db->createCommand( "SELECT rowpage from icenvinit" )->queryScalar(),
        ],
    ]);

    return $this->render( 'itemreport', ['error' => $error, 'success' => $success, 'collumnview' => $collumnview, 'dataProvider' => $dataProvider, 'sql' => $sql, 'count' => $count, 'listSchedule' => $listSchedule, 'arr_ftp' => $arr_ftp] );
}

1 个答案:

答案 0 :(得分:0)

在属性的选项中添加asc和desc,例如在public function testGet_ValidInput_TestAllObject() { $response = $this->client->get('/test_all'); $this->assertEquals(200, $response->getStatusCode()); $data = $response->json(); var_dump($data); } 属性中;

orgfield