如何覆盖JavaScript属性

时间:2014-01-16 21:42:36

标签: javascript jquery jquery-datatables

全局使用以下JavaScript它会自动插入到我的Web应用程序的每个网页中:

    <script type="text/javascript">
    $(document).ready(function() {
        $('#cemetracDataTable').dataTable( {
            "sDom": 'T<"clear">lfrtip',
            "oTableTools": {
                "sSwfPath": "http://fubar.com/wp-content/TableTools-2.1.5/media/swf/copy_csv_xls_pdf.swf",
                "aButtons": [
                    "copy",
                    "print",
                    {
                        "sExtends":    "collection",
                        "sButtonText": "Save",
                        "aButtons": [
                            {
                                "sExtends":     "csv",
                                "sButtonText":  "Excel (CSV)",
                                "bFooter":      false,
                                "sTitle":       "My title",
                                "sFileName":    "download.csv" // <-- override
                            },
                            {
                                "sExtends":     "xls",
                                "sButtonText":  "Excel (TSV)",
                                "bFooter":      false,
                                "sFileName":    "download.tsv" // <-- override
                            },
                            {
                                "sExtends":     "pdf",
                                "bFooter":      false,
                                "sFileName":    "download.pdf" // <-- override
                            }
                        ]
                    }
                ]
            }
        } ).columnFilter();
    } );
</script>

我希望能够覆盖某个网页上的sFileName属性,例如:

...
"sFileName":    "widgets.csv"
...
"sFileName":    "widgets.tsv"
...
"sFileName":    "widgets.pdf"

这样做可能吗?如果是这样,将如何做?

2 个答案:

答案 0 :(得分:2)

您可以使用.设置javascript对象属性:

在您的情况下,aButtons[0].sFileName = "widgets.csv"的某些内容。

答案 1 :(得分:1)

如果将对象引用存储到变量会更好。

Demo

var myObj = {
    sDom: 'T<"clear">lfrtip',
    oTableTools: {
        sSwfPath: "http://fubar.com/wp-content/TableTools-2.1.5/media/swf/copy_csv_xls_pdf.swf",
        aButtons: [
            "copy",
            "print", {
                sExtends: "collection",
                sButtonText: "Save",
                aButtons: [ {
                        sExtends: "csv",
                        sButtonText: "Excel (CSV)",
                        bFooter: false,
                        sTitle: "My title",
                        sFileName: "download.csv" // <-- override
                    }, {
                        sExtends: "xls",
                        sButtonText: "Excel (TSV)",
                        bFooter: false,
                        sFileName: "download.tsv" // <-- override
                    }, {
                        sExtends: "pdf",
                        bFooter: false,
                        sFileName: "download.pdf" // <-- override
                    }
                ]
            }
        ]
    }
};

然后将其作为参数传递

$(document).ready(function () {
    $('#cemetracDataTable').dataTable(myObj).columnFilter();
});

如果您想更改属性sFileName值,则应该像这样引用它们:

myObj.oTableTools.aButtons[2].aButtons[0].sFileName = "widgets.csv";
myObj.oTableTools.aButtons[2].aButtons[1].sFileName = "widgets.tsv";
myObj.oTableTools.aButtons[2].aButtons[2].sFileName = "widgets.pdf";