FlexGrid for catalog

Posted by: 048alexsandr1992 on 8 March 2018, 9:24 pm EST

    • Post Options:
    • Link

    Posted 8 March 2018, 9:24 pm EST

    Hi,

    I want to use FlexGrid for catalog page.

    For example http://joxi.ru/RmzVB73CWeGWRm

    For this purpose I must use CellTemplates

    http://demos.componentone.com/ASPNET/MVCExplorer/FlexGrid/CustomCells

    what I need to know:

    1. ColumnBuilder. I must have a few columns. But, each cell must contain the entire model rather than the property. If I create multiple columns the data will be duplicate.

    2. CellTemplate. I must bind all properties in Model for use their

      in Template.

    How I can do this ?

    
    bl.Add(cb => cb.Width("*").Bind(???).CellTemplate(b => b.TemplateId("template")));
    
    
    1. Template. How I can Get this data on side template?
    
    <script id="template" type="text/template">
        ???
    </script>
    
    
  • Posted 12 March 2018, 12:28 am EST

    Hi Alex,

    Allow me some time to prepare a sample.

    ~nilay

  • Posted 12 March 2018, 1:00 am EST

    Hi,

    Of course, thanks

  • Posted 15 March 2018, 4:08 pm EST

    Hi,

    Cell Template won’t be the ideal place to perform this. Instead using item-formatter would be better.

    For starters, bind the columns with unique item id instead of properties. And create an itemformatter which would design the cell.

    Example:

    
    @(Html.C1().FlexGrid<Sale>()
        .AutoGenerateColumns(false)
        .IsReadOnly(true)
        .Bind(Model)
        .CssClass("grid")
        .Columns(columns =>
        {
            columns.Add(column => column.Binding("ID"));
            columns.Add(column => column.Binding("ID").Header("ColumnName1"));
            columns.Add(column => column.Binding("ID").Header("ColumnName2"));
            columns.Add(column => column.Binding("ID").Header("ColumnName3"));
        })
        .ItemFormatter("itemFormatter")
    )
    
    <script type="text/javascript">
    
        function itemFormatter(panel, r, c, cell) {
            if (panel.columns[c].header === "ColumnName1") {
                if (panel.cellType === wijmo.grid.CellType.Cell) {
                    cell.innerHTML = buildColumnName1Template(panel.grid.itemsSource.items, panel.getCellData(r, c));
                }
            } else if (panel.columns[c].header === "ColumnName2") {
                if (panel.cellType === wijmo.grid.CellType.Cell) {
                    cell.innerHTML = buildColumnName2Template(panel.grid.itemsSource.items, panel.getCellData(r, c));
                }
            }
        }
    
        function buildColumnName1Template(items, id) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].ID === id) {
                    var currentItem = items[i];
                    return "<div></div>"; // Add properties and design your template as string
                }
            }
        }
       function buildColumnName2Template(items, id) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].ID === id) {
                    var currentItem = items[i];
                    return "<div></div>"; // Add properties and design your template as string
                }
            }
        }
    </script>
    
    

    ~nilay

  • Posted 15 March 2018, 4:49 pm EST

    Hi

    I have question to this solution.

    1. In template I must use all properties of model. In this example binding is perform only by Id.

    2. buildColumnName1Template and buildColumnName2Template are use the some row. But the purpose of the catalog page is to build the list data into several columns using a certain template. So buildTemplate must be one, but data for different items must be write in rows while have empty cells in row. Or I must create template and then convert to table by css?

  • Posted 15 March 2018, 5:40 pm EST

    Hi,

    Let me give you an example of this approach:

    Let’s assume I have a student model:

    public class Student {
    
        public int Id { get ;set; }
        public string Name{ get ;set; }
        public string Gender{ get ;set; }
        public DateTime DateOfBirth{ get ;set; }
    }
    

    A simple grid displaying this would be:

    @(Html.C1().FlexGrid<Student>()
        .AutoGenerateColumns(false)
        .IsReadOnly(true)
        .Bind(Model)
        .CssClass("grid")
        .Columns(columns =>
        {
            columns.Add(column => column.Binding("ID").Header("Id");
            columns.Add(column => column.Binding("Name").Header("Name");
            columns.Add(column => column.Binding("Gender").Header("Gender");
            columns.Add(column => column.Binding("DateOfBirth").Header("Date of birth");
        })
    )
    

    This is fine.

    What if I need one more column where I have to display an Identity card (composed out of multiple properties of a student).

    I cannot do it like above, since I cannot bind multiple properties on a single column. In this case I would create column bound to ID.

    This is what I would do.

    @(Html.C1().FlexGrid<Student>()
        .AutoGenerateColumns(false)
        .IsReadOnly(true)
        .Bind(Model)
        .CssClass("grid")
        .Columns(columns =>
        {
            columns.Add(column => column.Binding("ID").Header("Id");
            columns.Add(column => column.Binding("Name").Header("Name");
            columns.Add(column => column.Binding("Gender").Header("Gender");
            columns.Add(column => column.Binding("DateOfBirth").Header("Date of birth");
            columns.Add(column => column.Binding("ID").Header("Identity Card");
        })
        .ItemFormatter("itemFormatter")
    )
    

    Now I can see a new “IdentityCard” column, but only ID is displayed there. Where as I need to have all the properties of the student, not just ID. This is when I would use ItemFormatter

    An itemformatter runs for every single visible cell of Grid.

    function itemFormatter(panel, r, c, cell) {
            // For Identity Card column
            if (panel.columns[c].header === "Identity Card") {
                if (panel.cellType === wijmo.grid.CellType.Cell) {
                    // Get ID of student
                    var id = panel.getCellData(r, c);
                    // Get all the properties of student
                    var student = panel.grid.itemsSource.items[r];
                    var studentId = student.ID;
                    var studentName = student.Name;
                    var studentGender = student.Gender;
                    var studentDOB = student.DateOfBirth;
                    // Compose html template which would be displayed in the grid
                    cell.innerHTML = "<div><h1>Identity Card</h1>
    Id: "+studentId +"
    
    Name: "+studentName+"
    
    Gender: "+studentGender +"
    
    Date of Birth: "+studentDOB +"
    </div>";
                }
            }
        }
    

    I hope this helps!

    ~nilay

  • Posted 15 March 2018, 7:49 pm EST

    Great thanks!!!

  • Posted 25 March 2018, 6:08 pm EST

    Hi,

    I implemented your solution.

    But I have one problem - my data showed in one column

    http://joxi.ru/52aZXwVtGpad4A

    How I can show them in a few columns?

  • Posted 26 March 2018, 4:06 pm EST

    Hi Alex,

    I am afraid I would need to see your code to answer on this. If you want to share this privately you can create an account on our support portal,

    https://supportone.componentone.com

    ~nilay

  • Posted 26 March 2018, 4:38 pm EST

    Hi,

    You can show in your example Student.

    In example you added custom format for column “Identity Card”. So only one cell on row formated.

    But my target - catalog where data is showed in a few columns

    http://joxi.ru/RmzVB73CWeGWRm

    layuot flegrid use absolute positioning so I can’t use display:flex, or etc.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels