view-components / grids

MIT License
86 stars 17 forks source link

setValueCalculator in view-components/grids #43

Open anp241 opened 6 years ago

anp241 commented 6 years ago

I have used the following code to return the value to the view-components/grids - setValueCalculator, but the below code is setting the same value in all rows of the grid - either the first row value of database table or the last value of database table.

public function getzonename()
    {
        $result=array();
        $zoneids = state_masters::all(['zone_id']);
        $array_length = count($zoneids);
        $iteration    = 0;
        $zonenames = array();
        $idval="";
        $count = 0;

        while($iteration < $array_length)
        {
            $idval = $zoneids[$iteration];
            $zonenames[] = DB::select('select zone_name from zone_masters where id = ?',[$idval->zone_id]);

            $iteration++;

        }

          return $zonenames;

    }

--------------------------------------------------------------------------------------------------------

public function shownewgrid(Request $request)
    {
        $provider = new EloquentDataProvider(state_masters::class);

           $input = new InputSource($_GET);

        $columns = [
                        'Zone Name' => new Column('Zone Name'),
                        'state_name' => new Column('state_name'),
                        'id' => new Column('Update'),
                       'deleterec' => new Column('Delete record'),
                         new PageSizeSelectControl($input->option('page_size', 10), [2, 5, 10]),// allows to select page size
                         new PaginationControl($input->option('page', 1), 5), // 1 - default page, 5 -- page size
                         new CsvExport($input->option('csv'))

                     ];

              $grid = new Grid( $provider, $columns);

                $columns['Zone Name']->setValueCalculator(function($row)
                {
                     $row = StateController::getzonename();

                     for ($i=0;$i<sizeof($row);$i++) 
                     {

                        return $row[$i][0]->zone_name;
                                     } 

                });

            $columns['id']->setValueFormatter(function ($id, $row) 
            {
            return "<a href='". url('/editstaterecord')."/{$row->id}'>Update</a>";  
            })
            ->getDataCell()
            ->setAttribute('onclick', 'window.location = $(this).find(\'a\').attr(\'href\'));')
            ->setAttribute('style', 'cursor:pointer');

           $columns['deleterec']->setValueFormatter(function ($id, $row) 
           {
             return "<a href='". url('/deletestaterecord')."/{$row->id}'>Delete</a>";
           })
           ->getDataCell()

           ->setAttribute('onclick', 'return confirm("Are you sure you want to delete this item?");')
           ->setAttribute('style', 'cursor:pointer');

            //  but also you can add some styling:
            $customization = new BootstrapStyling();
            $customization->apply($grid);

            $items = zone_masters::all(['id', 'zone_name']);

              return view('masters.state', compact('grid','items'));
    }

How do I update the code

wdog commented 6 years ago

your code always return the first record

return $row[$i][0]->zone_name;