Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Changing value in a column of DataGrid

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 702 Views
  • George Botley
    Avatar
    Community Member
    16 Posts

    Changing value in a column of DataGrid Link to this post

    Hi Guys,

    I have a news page type that makes use of two gridfield elements.

    One is Categories, the other is Articles.

    The Categories is working fine in the sense that you can add remove etc.. No problems here.

    The Articles additions works fine also, it's just the displaying of the Articles GridField that would otherwise confuse users.

    When an article is created, a user is given a dropdown to select the assigned category, which saves the ID of the Category from the DB into the record for the article, which allows the category name to be changed in the future without affecting articles assigned to it...

    The confusing part is when the user then views all of their Articles, they can see the article name, and the assigned category comes up as the ID.. How can I make that particular column fetch its value based on the actual category name within the Categories table as opposed to the ID? - It would involve a SQL query, which is fine, but I don't know how to alter the values in the displayed output of the Grid.

    Much obliged.

    George

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Changing value in a column of DataGrid Link to this post

    If you're using a GridField you need ot set $summary_fields on Categories or custom specify the header fields in gridfield. Gridfield uses $summary_fields for scaffolding geader by default

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Changing value in a column of DataGrid Link to this post

    Thanks ZenMonkey, but I didn't quite see what you meant..

    I've set that already...

    I'll give you the two grids as examples...

    Grid 1 (Categories): http://screencast.com/t/mw99fdz5QPQw

    Grid 2 (Articles): http://screencast.com/t/gMC77eyHeUK

    How do I get the Grid of Articles to show the Category Name from the associated record in the Categories grid...

    Here's the summary fields of each respective grid as you mentioned..

    Grid 1:

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array('ID' => 'ID', 'Title' => 'Title');

    Grid 2:

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array('ID' => 'ID', 'Title' => 'Title', "Category" => "Cateogry");

    Much obliged.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Changing value in a column of DataGrid Link to this post

    Okay, now I have a better idea of what's going on. You need to set that column with dot notation so instead of Category you'd use Category.Title

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Changing value in a column of DataGrid Link to this post

    Thanks zenmonkey,

    But that still didn't quite work..

    Here's a structure screenshot of my database tables for each grid field..

    The NewsArticle table: http://screencast.com/t/tS24TtTcm2Yk

    The NewsCategory table: http://screencast.com/t/r6L4ebGQH

    I'm trying to link two has_many grid fields together..

    So that the NewsArticle with Category '1' would refer to the name of the category based on the same respective ID in the NewsCategory table...

    Any pointers? - Or am i approaching this in the completely wrong way?

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Changing value in a column of DataGrid Link to this post

    Are you manually building the relationation intead of letting SS do it? If you're leeting the ORM build the realtion the column should be called CategoryID. Can you post your code for NewsArticle and NewsCategory?

    you can use http://www.sspaste.com or another paste service

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Changing value in a column of DataGrid Link to this post

    I'm not sure.. I'm quite new to using SS GridFields so I'm not sure how to do either, although I've worked with SS for a few years.

    Article: http://www.sspaste.com/paste/show/525dcd54a06b9
    Category: http://sspaste.com/paste/show/525dcd6f83886

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Changing value in a column of DataGrid Link to this post

    Okay, I'm not sure why you're setting the Category manually instead of setting it as a relationship. If you set up NewsCategory $has_many NewArticles and NewsArticle $has_one NewsCategory then all the properties from the respecitve obejct will be availble on the other Object. I've posted the cahnges on the respective sspaste links

    If you're storing the Category on the article to save an extra Database Call for the relation don't map('ID', "Title'). You should map('Title', 'Title') on the dropdown.

    map('ID', 'Title') set the selct array as 'ID' => 'Title', the key get written the database while the value shows in the drop down.

    702 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.