doukou4066 2014-05-23 08:51
浏览 37

在php文件中访问jquery

I've recently implemented a jquery script to convert a html table into a excel file. I've written this script inside a php file, but when i try to get the code working the only thing i get is a reference error in firefox:

ReferenceError: $datatype is not defined

I think the php interpreter doesn't ignore $datatype as jquery but try to use it as a php variable. Is there any kind of solution to get jquery code function inside a php file? Thanks for any suggestion

<?php 
        session_start();
        $_SESSION['locazione']= $_GET['locazione'];
        //echo  $_SESSION['locazione'];
        ?>
<html>
    <head>
                <meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>
                <script src="./js/jquery.btechco.excelexport.js"></script>
                <script src="./js/jquery.base64.js"></script>
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
                <link href="./css/style2.css" rel="stylesheet" type="text/css">
                <?php $Locazione = $_GET['locazione']; ?>

                <script type="text/javascript">
                $(document).ready(function () {
                    $("#btnExport").click(function () {
                        $("#tblExport").btechco_excelexport({
                            containerid: "tblExport",
                            datatype: $datatype.Table
                        });
                    });
                });
                </script>
        <!-- [...] -->
    </head>
        <body>      

        <INPUT type="button" value="Aggiungi Elemento" onClick="addRow('dataTable')" />
        <INPUT type="button" value="Elimina Elemento" onClick="deleteRow('dataTable')" />
        <form action="form.php" method="post" name="f" style="position: relative;">  
        <TABLE id="tblExport" border="0"style="width:100%;min-height:50px;top:10px">
        <thead>
        <tr>
            <th width="35"></th>
            <th width="54" align="left">TipoLocale</th>
            <th width="54" align="left">NomeLocale</th>
            <th width="54" align="left">NumeroLocale</th>
            <th width="54" align="left">TipoCarico</th>
            <th width="54">N°</th>
            <th width="54" align="left">Potenza</th>
            <th width="54" align="left">PotenzaTotale</th>
            <th width="54">Note</th>
        </tr>
        </thead>
        <tbody id="dataTable">
        </tbody>
        </TABLE>
        <div id="sendbtn">
        <INPUT type="submit" value="Invia" name="submit" target="_new"  onclick="return confirm('Confermi l\'inserimento?')"/>
            <input type="button" id="btnExport" value=" Export Table data into Excel " />       
        </div>
        </form>             
        </body>
        </html>

this is the jquery.btechco.excelexport.js:

(function ($) {

   var $datatype = {
        Table: 1
        , Json: 2
        , Xml: 3
        , JqGrid: 4
    }

var $defaults = {
    containerid: null
    , datatype: $datatype.Table
    , dataset: null
    , columns: null
};

var $settings = $defaults;

$.fn.btechco_excelexport = function (options) {
    $settings = $.extend({}, $defaults, options);

    switch ($settings.datatype) {
        case 1:
            Export($("#" + $settings.containerid).parent().html());
            break;
        case 2:
            Export(ConvertJsonToTable());
            break;
        case 3:
            Export(ConvertXmlToTable());
            break;
        case 4:
            Export(ConvertJqGridDataToTable());
            break;
    }

    function ConvertJsonToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";

        result += "<tbody>";
        $($settings.dataset).each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if (value.hasOwnProperty(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += value[this.datafield];
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function ConvertXmlToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";

        result += "<tbody>";
        $($settings.dataset).find("row").each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if ($(value).attr(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += $(value).attr(this.datafield);
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function ConvertJqGridDataToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";
        result += "<tbody>";

        $($settings.dataset).find("rows > row").each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if ($(value).find(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += $(value).find(this.datafield).text();
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function Export(htmltable) {
        var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
        excelFile += "<head>";
        excelFile += "<!--[if gte mso 9]>";
        excelFile += "<xml>";
        excelFile += "<x:ExcelWorkbook>";
        excelFile += "<x:ExcelWorksheets>";
        excelFile += "<x:ExcelWorksheet>";
        excelFile += "<x:Name>";
        excelFile += "{worksheet}";
        excelFile += "</x:Name>";
        excelFile += "<x:WorksheetOptions>";
        excelFile += "<x:DisplayGridlines/>";
        excelFile += "</x:WorksheetOptions>";
        excelFile += "</x:ExcelWorksheet>";
        excelFile += "</x:ExcelWorksheets>";
        excelFile += "</x:ExcelWorkbook>";
        excelFile += "</xml>";
        excelFile += "<![endif]-->";
        excelFile += "</head>";
        excelFile += "<body>";
        excelFile += htmltable.replace(/"/g, '\'');
        excelFile += "</body>";
        excelFile += "</html>";

        var base64data = "base64," + $.base64.encode(excelFile);
        window.open('data:application/vnd.ms-excel;filename=test;' + base64data);
    }
};
})(jQuery);

(function ($) {

   var $datatype = {
        Table: 1
        , Json: 2
        , Xml: 3
        , JqGrid: 4
    }

var $defaults = {
    containerid: null
    , datatype: $datatype.Table
    , dataset: null
    , columns: null
};

var $settings = $defaults;

$.fn.btechco_excelexport = function (options) {
    $settings = $.extend({}, $defaults, options);

    switch ($settings.datatype) {
        case 1:
            Export($("#" + $settings.containerid).parent().html());
            break;
        case 2:
            Export(ConvertJsonToTable());
            break;
        case 3:
            Export(ConvertXmlToTable());
            break;
        case 4:
            Export(ConvertJqGridDataToTable());
            break;
    }

    function ConvertJsonToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";

        result += "<tbody>";
        $($settings.dataset).each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if (value.hasOwnProperty(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += value[this.datafield];
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function ConvertXmlToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";

        result += "<tbody>";
        $($settings.dataset).find("row").each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if ($(value).attr(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += $(value).attr(this.datafield);
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function ConvertJqGridDataToTable() {
        var result = "<table>";

        result += "<thead><tr>";
        $($settings.columns).each(function (key, value) {
            if (this.ishidden != true) {
                result += "<th";
                if (this.width != null) {
                    result += " style='width: " + this.width + "'";
                }
                result += ">";
                result += this.headertext;
                result += "</th>";
            }
        });
        result += "</tr></thead>";
        result += "<tbody>";

        $($settings.dataset).find("rows > row").each(function (key, value) {
            result += "<tr>";
            $($settings.columns).each(function (k, v) {
                if ($(value).find(this.datafield)) {
                    if (this.ishidden != true) {
                        result += "<td";
                        if (this.width != null) {
                            result += " style='width: " + this.width + "'";
                        }
                        result += ">";
                        result += $(value).find(this.datafield).text();
                        result += "</td>";
                    }
                }
            });
            result += "</tr>";
        });
        result += "</tbody>";

        result += "</table>";
        return result;
    }

    function Export(htmltable) {
        var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
        excelFile += "<head>";
        excelFile += "<!--[if gte mso 9]>";
        excelFile += "<xml>";
        excelFile += "<x:ExcelWorkbook>";
        excelFile += "<x:ExcelWorksheets>";
        excelFile += "<x:ExcelWorksheet>";
        excelFile += "<x:Name>";
        excelFile += "{worksheet}";
        excelFile += "</x:Name>";
        excelFile += "<x:WorksheetOptions>";
        excelFile += "<x:DisplayGridlines/>";
        excelFile += "</x:WorksheetOptions>";
        excelFile += "</x:ExcelWorksheet>";
        excelFile += "</x:ExcelWorksheets>";
        excelFile += "</x:ExcelWorkbook>";
        excelFile += "</xml>";
        excelFile += "<![endif]-->";
        excelFile += "</head>";
        excelFile += "<body>";
        excelFile += htmltable.replace(/"/g, '\'');
        excelFile += "</body>";
        excelFile += "</html>";

        var base64data = "base64," + $.base64.encode(excelFile);
        window.open('data:application/vnd.ms-excel;filename=test;' + base64data);
    }
};
})(jQuery);
  • 写回答

2条回答 默认 最新

  • douzong0711 2014-05-23 08:59
    关注
    (function ($) {
    
       var $datatype = {
            Table: 1
            , Json: 2
            , Xml: 3
            , JqGrid: 4
        }
    

    In your library( jquery.battatech.excelexport.js), add var before the $datatype

    评论

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容