普通表格

组合列
文本3
文本4
整数
文本1
文本2
item 1,1
item 1,2
item 1,3
item 1,4
742
item 2,1
item 2,2
item 2,3
item 2,4
687
item 3,1
item 3,2
item 3,3
item 3,4
92
item 4,1
item 4,2
item 4,3
item 4,4
244
item 5,1
item 5,2
item 5,3
item 5,4
15
item 6,1
item 6,2
item 6,3
item 6,4
90
item 7,1
item 7,2
item 7,3
item 7,4
374
item 8,1
item 8,2
item 8,3
item 8,4
93
item 9,1
item 9,2
item 9,3
item 9,4
774
item 10,1
item 10,2
item 10,3
item 10,4
630
item 11,1
item 11,2
item 11,3
item 11,4
14
item 12,1
item 12,2
item 12,3
item 12,4
973
item 13,1
item 13,2
item 13,3
item 13,4
657
item 14,1
item 14,2
item 14,3
item 14,4
143
item 15,1
item 15,2
item 15,3
item 15,4
466
item 16,1
item 16,2
item 16,3
item 16,4
899
item 17,1
item 17,2
item 17,3
item 17,4
57
item 18,1
item 18,2
item 18,3
item 18,4
773
item 19,1
item 19,2
item 19,3
item 19,4
149
item 20,1
item 20,2
item 20,3
item 20,4
756
item 21,1
item 21,2
item 21,3
item 21,4
985
item 22,1
item 22,2
item 22,3
item 22,4
814
item 23,1
item 23,2
item 23,3
item 23,4
449
item 24,1
item 24,2
item 24,3
item 24,4
927
item 25,1
item 25,2
item 25,3
item 25,4
927
item 26,1
item 26,2
item 26,3
item 26,4
712
item 27,1
item 27,2
item 27,3
item 27,4
595
item 28,1
item 28,2
item 28,3
item 28,4
307
item 29,1
item 29,2
item 29,3
item 29,4
739
item 30,1
item 30,2
item 30,3
item 30,4
532
item 31,1
item 31,2
item 31,3
item 31,4
132
item 32,1
item 32,2
item 32,3
item 32,4
630
item 33,1
item 33,2
item 33,3
item 33,4
183
item 34,1
item 34,2
item 34,3
item 34,4
152
item 35,1
item 35,2
item 35,3
item 35,4
315
item 36,1
item 36,2
item 36,3
item 36,4
350
item 37,1
item 37,2
item 37,3
item 37,4
260
item 38,1
item 38,2
item 38,3
item 38,4
327
item 39,1
item 39,2
item 39,3
item 39,4
807
item 40,1
item 40,2
item 40,3
item 40,4
421
<div id="basicTable"></div>
<script type="text/javascript">
    cola(function(model) {
        var items = [];
        for (var row = 1; row <= 40; row++) {
            var item = {
                selected: Math.random() > 0.5,
                int: Math.round(Math.random() * 1000)
            };
            for (var col = 1; col <= 6; col++) {
                item["col" + col] = "item " + row + "," + col
            }
            items.push(item);
        }
        model.set("items", items);

        model.widgetConfig({
            basicTable: {
                $type: "table",
                bind: "item in items",
                showHeader: true,
                height: 300,
                columns: [{
                    $type: "select"
                }, {
                    caption: "组合列",
                    columns: [{
                        bind: ".col1",
                        caption: "文本1"
                    }, {
                        bind: ".col2",
                        caption: "文本2"
                    }]
                }, {
                    bind: ".col3",
                    caption: "文本3"
                }, {
                    bind: ".col4",
                    caption: "文本4"
                }, {
                    bind: ".int",
                    caption: "整数",
                    align: "right"
                }]
            }
        });
    });
</script>

模板和编辑器

通过模板我们可以自由的定义每一列单元格的渲染方式。 为了实现某个模板被同时使用在多个列上,在列模板内可以使用$default来代表当前列的属性。

Cola-UI内部还提供了几个预定义好的列模板。 比如input-column可以把某一列渲染成可编辑的列,checkbox-column可以把某一列渲染成带有复选框的形式。

图标
转换大写
文本编辑
控制Style
操作
ITEM 1,1
Del
ITEM 2,1
Del
ITEM 3,1
Del
ITEM 4,1
Del
ITEM 5,1
Del
ITEM 6,1
Del
ITEM 7,1
Del
ITEM 8,1
Del
ITEM 9,1
Del
ITEM 10,1
Del
ITEM 11,1
Del
ITEM 12,1
Del
ITEM 13,1
Del
ITEM 14,1
Del
ITEM 15,1
Del
ITEM 16,1
Del
ITEM 17,1
Del
ITEM 18,1
Del
ITEM 19,1
Del
ITEM 20,1
Del
ITEM 21,1
Del
ITEM 22,1
Del
ITEM 23,1
Del
ITEM 24,1
Del
ITEM 25,1
Del
ITEM 26,1
Del
ITEM 27,1
Del
ITEM 28,1
Del
ITEM 29,1
Del
ITEM 30,1
Del
ITEM 31,1
Del
ITEM 32,1
Del
ITEM 33,1
Del
ITEM 34,1
Del
ITEM 35,1
Del
ITEM 36,1
Del
ITEM 37,1
Del
ITEM 38,1
Del
ITEM 39,1
Del
ITEM 40,1
Del
<div id="templateTable">
    <template name="int-template">
        <div c-widget="input; bind:$default; class:fluid" c-class="error:$default>500"></div>
    </template>
    <template name="check-mark"><span c-style="display:$default?'':'none'"><i class="large green checkmark icon"></i></span>
    </template>
    <template name="operations">
        <div c-widget="button;caption:Del;class:red;size:mini;click:{{remove(item)}}"></div>
    </template>
</div>
<script type="text/javascript">
    cola(function(model) {
        var items = [];
        for (var row = 1; row <= 40; row++) {
            var item = {
                bool: Math.random() > 0.5,
                int: Math.round(Math.random() * 1000)
            };
            for (var col = 1; col <= 6; col++) {
                item["col" + col] = "item " + row + "," + col
            }
            items.push(item);
        }
        model.set("items", items);

        model.action({
            upper: function(s) {
                return (s) ? s.toUpperCase() : "";
            },
            remove: function(item) {
                item.remove();
            }
        });

        model.widgetConfig({
            templateTable: {
                $type: "table",
                bind: "item in items",
                showHeader: true,
                height: 300,
                renderRow: function(self, arg) {
                    $(arg.dom).click(function() {
                        alert(arg.item.get("int"))
                    })
                },
                columns: [{
                    caption: "图标",
                    bind: ".bool",
                    align: "center",
                    template: "check-mark",
                    width: 40
                }, {
                    caption: "转换大写",
                    bind: "upper(item.col1)"
                }, {
                    caption: "文本编辑",
                    bind: ".col2",
                    template: "input-column"
                }, {
                    caption: "控制Style",
                    bind: ".int",
                    align: "right",
                    template: "int-template"
                }, {
                    caption: "操作",
                    align: "center",
                    template: "operations"
                }]
            }
        });
    });
</script>

分页装载

当数据模型中设定pageSize前提下,结合pager控件可以获得分页装载数据的表格

产品编号
产品名称
价格
<div c-widget="Pager; bind:pageItems">
    <a page-code="pages" class="item"></a>
</div>
<div id="pagingTable"></div>
<script type="text/javascript">
    cola(function(model) {
        model.describe("pageItems", {
            provider: {
                url: "/data/items",
                pageSize: 5
            }
        });

        model.widgetConfig({

            pagingTable: {
                $type: "table",
                bind: "item in pageItems",
                showHeader: true,
                currentPageOnly: true,
                height: 300,
                columns: [{
                    bind: ".id",
                    caption: "产品编号"
                }, {
                    bind: ".title",
                    caption: "产品名称"
                }, {
                    bind: ".originalPrice",
                    caption: "价格"
                }]
            }
        });
    });
</script>