【问题标题】:Sorting for Custom column in grid view in YiiYii中网格视图中自定义列的排序
【发布时间】:2015-07-15 09:10:48
【问题描述】:

我必须检查其他表格中的产品数量并在当前网格视图中显示。所以我在模型中创建了一个函数来获取该列的计数。

但我的问题是如何在网格视图中对自定义列 (checked_kpl) 进行排序

这是我的代码。

型号

public function search() {
    $criteria = new CDbCriteria;

    $criteria->compare('id', $this->id, true);
    $criteria->compare('purchase_order_id', $this->purchase_order_id);
    $criteria->compare('product_id', $this->product_id);
    $criteria->compare('unit_price', $this->unit_price, true);
    $criteria->compare('qty', $this->qty, true);
    $criteria->compare('cost_price', $this->cost_price, true);
    $criteria->compare('customs_percent', $this->customs_percent, true);
    $criteria->compare('discount_percent', $this->discount_percent, true);
    $criteria->compare('notes', $this->notes, true);
    $criteria->compare('created_at', $this->created_at, true);
    $criteria->compare('updated_at', $this->updated_at, true);

   return new CActiveDataProvider($this, array(
    'criteria' => $criteria,
   ));
   }
}




public function getCheckedKpl() {
    $checked_kpl = 0;
    if (!empty($this->purchaseOrderArrivals)) {
        foreach ($this->purchaseOrderArrivals as $eachArrival) {
            $checked_kpl += $eachArrival->qty;
        }
    }
    return $checked_kpl;
}

注意: - purchaseOrderArrivals 是另一种模型。我已经与这个模型建立了关系。 - getCheckedKpl 函数让我计算产品数量。

VIEW - 在视图中,我将此代码放在 gridview 小部件中以显示列。

    array(
            'name' => 'checked_kpl',
            'value' => '$data->getCheckedKpl()',
            'type' => 'raw',
            'class' => 'DataColumn'
         ),

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: php sorting gridview yii


    【解决方案1】:
    class Model extends CActiveRecord {
      // Adding attribute to work with SQL query
      public $checked_kpl;
    
      public function attributeLabels(){
        // Prettify column name
        return array( 'checked_kpl' => 'Checked kpl' );
      }
    
      public function search() {
        $criteria = new CDbCriteria;
        // Count subquery like this
        // SELECT COUNT(*) as checked_kpl, id FROM {{table}} GROUP BY param
        // Condition like this
        // ( q1.id=t.id )
        $criteria->join = "LEFT JOIN (/* **HERE IS A COUNT SUBQUERY** */) as q1 ON(/* **HERE IS A CONDITION** */)";
        $criteria->select = array( '*', new CDbExpression("q1.checked_kpl as checked_kpl") );
    
        // ... your criteria here
    
        // Adding custom sort data
        $sort = new CSort();
        $sort->attributes = array(
            'checked_kpl' => array(
                'asc' => 'q1.checked_kpl',
                'desc' => 'q1.checked_kpl DESC'
            )
        );
    
        return new CActiveDataProvider( $this, array(
            'criteria' => $criteria,
            'sort' => $sort,
        ) );
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多