Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Sep 22, 2013 @ 16:29
    Bjarne Fyrstenborg
    0

    Only create rows which have values

    Hi...

    I have looked at the razor example from here to use Tabular Data: http://snipt.org/ujijc1

    I have ended up with this code:

    @if(Model.HasValue("productSpecs"))
        {
            <div class="tab-pane fade active in" id="tab-1">
    
            @{
                DynamicXml specs = (DynamicXml)Model.productSpecs;
                ArrayList colWidth = new ArrayList();
                var hasRowHeader = (specs.BaseElement.Elements("Body").First().Elements("Row").First().Elements("RowHead").Count() > 0);
            }
            <table border="0" class="table table-striped">
            @foreach (var item in specs)
            {
                string closeTag = "</tbody>";
                switch ((string)item.BaseElement.Name.ToString())
                {
                    case "Columns":
                        foreach (var column in item.BaseElement.Elements("Column"))
                        {
                            colWidth.Add(column.Attribute("width").Value);
                        }
                        break;
                    case "Header":
                        @:<thead>
                        closeTag = "</thead>";
                        break;
                    case "Footer":
                        @:<tfoot>
                        closeTag = "</tfoot>";
                        break;
                    case "Body":
                        @:<tbody>
                        break;
                }
    
                if (item.BaseElement.Name != "Columns")
                {
                    foreach (var itemRow in item.BaseElement.Elements())
                        {   
                        @:<tr>
                        var isFirst = true;
                        foreach (var itemDataCell in itemRow.Elements())
                        {
                            string cellTag = (itemDataCell.Name == "RowHead" || itemDataCell.Name == "ColHead") ? "th" : "td";
                            string cellClass = itemDataCell.Name.ToString() + " " + item.BaseElement.Name.ToString();
                            if (item.BaseElement.Name == "Header" && hasRowHeader && isFirst) { cellClass += " tblTitle"; }
                            @Html.Raw("<" + cellTag + " class=\"" + cellClass + "\">");
                            @Html.Raw(itemDataCell.Value);
                            @Html.Raw("</" + cellTag + ">");
                            isFirst=false;
                        }
                        @:</tr>
                    }
    
                    @Html.Raw(closeTag)        
                }
            }
            </table>
            </div>
        }

    However default is set to five rows, so when the node is published I get five empty rows in the html.
    I could just delete the rows or set default rows just to one.. but is there a way I easily can check if the cells in a row have values (cell not is empty or only contains blank spaces) before I build the html tablerow?

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft