How to know the column name on yii2 Gridview

How to know the column name on yii2 Gridview

By : JoanieL
Date : November 20 2020, 11:01 PM
like below fixes the issue As you said, $item is out of scope, and as said in php doc about anonymous functions (not specific to yii) :
code :
'value' => function($model) use ($item) {
    return $model->{$item->name};

Share : facebook icon twitter icon
yii2 adding column in the gridview from relation and sorting in gridview

yii2 adding column in the gridview from relation and sorting in gridview

By : user3452815
Date : March 29 2020, 07:55 AM
will help you I figured out the sorting by changing the search method to the following...
code :
 public function search($params)
    $query = TblTargetCities::find();


    $dataProvider = new ActiveDataProvider([
        'query' => $query,

    $dataProvider->sort->attributes['city'] = [
    // The tables are the ones our relation are configured to
    // in my case they are prefixed with "tbl_"
    'asc' => ['lib_Cities.city_name' => SORT_ASC],
    'desc' => ['lib_Cities.city_name' => SORT_DESC],


    if (!$this->validate()) {
        // uncomment the following line if you do not want to any records when validation fails
        // $query->where('0=1');
        return $dataProvider;

        'id' => $this->id,
        'grouping' => $this->grouping,
        'launch_year' => $this->launch_year,

   /* $query->andFilterWhere(['like', 'city_code', $this->city_code])
        ->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
        ->andFilterWhere(['like', 'cluster', $this->cluster])
        ->andFilterWhere(['like', 'priority', $this->priority]);   */

    $query->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
        ->andFilterWhere(['like', 'cluster', $this->cluster])
        ->andFilterWhere(['like', 'priority', $this->priority])
        ->andFilterWhere(['like', 'lib_cities.city_name', $this->city_code]);

    return $dataProvider;
YII2 add a sum column in the gridview

YII2 add a sum column in the gridview

By : user3488030
Date : March 29 2020, 07:55 AM
wish help you to fix your issue How to add a sum column in the gridview?? I have a table with the a grant_amount, and lcc_amount. I want to add grant and lcc and display it in the gridview. How is that done? I have successfully displayed already the data from the table to the gridview perfectly. , You need set new column and set value, label property like that:
code :
'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                    'label' => 'Sum',
                    'value' => function ($model) {
                        return $model->grant_amount + $model->lcc_amount;
Remove first column (#) of Yii2 GridView

Remove first column (#) of Yii2 GridView

By : Jitendra Kumar
Date : March 29 2020, 07:55 AM
this will help Remove ['class' => 'yii\grid\SerialColumn'], from columns => [ ... ] of GridView.
Sum of a gridview column in a textbox in yii2

Sum of a gridview column in a textbox in yii2

By : Akshay Sharma
Date : March 29 2020, 07:55 AM
hope this fix your issue Could be you can use showFooter for gridView
Firts you ca can calculate the value using the dataProvider
code :
        $yourTotal =0;
        $numRow = 0;
        foreach ($dataProvider->models as $key => $value) {
            $yourTotal += $value['your_attribute'];
        echo GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [   'attribute' => 'your_attribute',
                'label' => 'Your Lable',
                'footer' => $yourTotal,
                'footerOptions' => ['style' => 'text-align:right;'],        
<?= Html::textInput('your_name', $yourTotal , options[]); ?>
<div id='my_id'> <?=  $yourTotal ?> </div>
Yii2 - How to make a sum of my column in GridView?

Yii2 - How to make a sum of my column in GridView?

By : RAM
Date : March 29 2020, 07:55 AM
I wish this helpful for you How do I sum up the values of a column in my GridView and display the total? , A bit better and dynamic way :
1) create function in model
code :
public static function getTotal($provider, $columnName)
    $total = 0;
    foreach ($provider as $item) {
      $total += $item[$columnName];
  return $total;  
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'showFooter' => true,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         'attribute' =>'amount',
         'footer' => ModelName::getTotal($dataProvider->models, 'amount'),

        ['class' => 'yii\grid\ActionColumn'],
]) ?>
Related Posts Related Posts :
  • how to display controller array values in mvc concept?
  • While loop without explicit increment / counter (basic)
  • updated values are displaying only after refreshing the page
  • PHP: update on DB if i pass it through get url
  • Select * from table where columns contain a keyword
  • Wildcard to route to a controller
  • Unknown component ID: authClientCollection
  • make standings table soccer league
  • Laravel pusher error: JSON returned from webapp was invalid, yet status code was 200
  • How to get most downloads records
  • What argument does my function need to echo get_results() query results
  • Laravel - Toggle value in database with checkbox and ajax
  • $_POST is not working in web services?
  • How to Count the number of objects in elequont Object inside a group by in a already queried collection?
  • how to add custom column in db table when using 'into outfile' query
  • cant load more than 100 words in json android from sql
  • PHP: How to order array by a value of child array
  • composer dump-autoload not working on controller Laravel 5
  • How select random column value from Mysql database using PHP
  • Showing data with specific parameter in Wordpress Query
  • Why die with connect_error() is not working?
  • PHP foreach for child of multidimentional array?
  • how to load another page when clicking a button in another page and pass values to the loaded page in jquery?
  • Laravel eloquent updating tinyint (boolean)
  • How to Check/Uncheck single radio button
  • obtain values from MySQL table using PHP and display results in jQuery function as "option"
  • PHP complex while-loop logic
  • Show a post from selected categories wordpress
  • PHP DateTime: How can i re write code to run on PHP 5.1.6
  • Skip values in PHP XPath
  • ANDROID&PHP - Why am I getting this JSONArray parsing error
  • if row equals parameter from url
  • Set up Phalcon 3.0.x with Php 7.0.x in ubuntu16.04
  • Server picking up old files
  • CSS loading after HTML loads
  • Getting an error for double and boolean even boolean given
  • Create shipment using fedex api (php)
  • How to get the variable value from one controller to another controller?
  • Bind doesn't work on User model
  • Sort JSON date by date - PHP
  • apache log is filled with erros and warnings almost 25 gb each time
  • Chrome adding tags (#shadow-root (open))
  • Split an array based on the value of another array
  • CodeIgniter Calendar Preferences template not working
  • .htaccess mode rewrite does not affect page url
  • PHP Laravel 5 : Trait not found
  • php count values column
  • Ajax POST not recognised as part of current guest session, Laravel 5.3
  • Why is my contact form PHP script not working? No error log
  • Sorting racers / players by places
  • cURL empty request data if https connection
  • PHP/MYSQL - Multiple connections to the same database?
  • PHP: Handle only parents on array
  • PHP IF How to include multiple conditions?
  • Basic PHP contact form receiving email but no message
  • Change price location in Woocommerce Single Product Layout
  • Turn string to array in PHP (all data recorder as string)
  • Sorting issues with mySQL JOIN
  • Get keys from multidimentional array by value
  • php accessing variables in constructor from included file
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com