Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you attach a callback function to template? #11

Open
xjsender opened this issue Mar 28, 2015 · 1 comment
Open

Can you attach a callback function to template? #11

xjsender opened this issue Mar 28, 2015 · 1 comment

Comments

@xjsender
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants