2014-01-16 117 views
2

全局使用以下JavaScript代碼它在每個網頁自動插在我的web應用程序:如何覆蓋JavaScript屬性

<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": "widgets.csv" 
... 
"sFileName": "widgets.tsv" 
... 
"sFileName": "widgets.pdf" 

正在這樣做可能對overrride的sFileName屬性?如果是這樣,這將如何完成?

+1

我m不知道如何在'dataTable'方法中獲得對傳入對象的引用,所以,會是bett呃如果將它分配給一個變量,然後將該變量作爲參數傳遞給'dataTable'方法。 '$('#cemetracDataTable')。dataTable(myObj)' – Givi

回答

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"; 
+0

它工作。這是WordPress的,所以我必須將對象分配'var myObj = ...'放入標題中,對象屬性覆蓋頁面本身,最後在頁腳中調用方法。 –

2

可以使用.組JavaScript對象屬性:

沿aButtons[0].sFileName = "widgets.csv"在你的情況下,東西線。