eisenbraun / columns

A jQuery plugin that converts JSON data into searchable, sortable, HTML tables
http://eisenbraun.github.io/columns
MIT License
105 stars 47 forks source link

Can you attach a callback function to template? #11

Open xjsender opened 9 years ago

xjsender commented 9 years ago

Need to generate the template according to related value type or something else, if template can be attached with a callback, it will be great feature, thanks.

xjsender commented 9 years ago

Just like this, I don't know what will be caused by this, I just update the source code to below, and get the effect as the below picture.

The source code part:

e.each(t.schema, function(e, n) {
    n.hide || (
        n.template ? (
            typeof(n.template) === "function" ?
            i.push("<td>" + n.template(r[n.key]) + "</td>") :
            i.push("<td>" + t.chevron(n.template, r) + "</td>")
        ) : i.push("<td>" + r[n.key] + "</td>")
    )
});

The usage:

schema.push({
    "header": key,
    "key": key,
    "template": function(value) {
        if (type.isNull(value)) {
            return "";
        } else if (type.isObject(value) && value.records) {
            return "<pre>" + JSON.stringify(value.records, null, "  ") + "</pre>";
        } else {
            var idRegExp = new RegExp(/[a-zA-Z0-9]{15,18}/ig);
            if (idRegExp.test(value)) {
                return "<a href='/{0}'>{0}</a>".format(value);
            }

            return value;
        }
    }
})

The demo image