0

我希望在我的网站上添加一个页面,该页面将包含一个显示可用产品的表格。

我希望能够做的是使表格的内容可以从另一个页面(比如管理页面)更改。是否可以使用 JavaScript 或类似的东西来做这样的事情?如果是这样,请您指出正确的方向。

非常感谢感谢。

4

4 回答 4

1

You could use XMLHttpRequest for this , but I would recommend that you use jQuery which would then be the $.ajax() function. What this does is send data to the server without refreshing the page or without anybody knowing about it really.

So what you could do is on the admin side is send some changes data to the server and on the client side unless you use a Web Socket you would have to contact the server every so many seconds to see if there are any changes. then the server would send you any changes that would have been made by the admin then you would work with that result with javascript to display changes on the web page.

What is Ajax

jQuery Ajax

Here is the requested example using jQuery

in your php you would have something like this

if($_POST['type'] === 'updateProduct') {
   // update database with new price
   // You could have a field in the database that now say's 
   // that the product has been updated

   //  send the response 

   echo 'done';
}

// And the admin ajax something like this 
$.ajax({
    type: 'POST',
    url: 'linkToYourFile.php', // The php file that will process the request
    data: {
        type: 'updateProduct', // This is all the data you wan't to send to your php file
        productID: 8484737,
        newPrice: '$100.99'
    },
    success: function( result ) { // if we get a response 
      if(result === 'done') {
        // The product has been updated from the admin side 
      }
    }
});

// on the client side

if($_POST['type'] === 'checkForUpdates') {
   // Contact the database and check that $_POST['productID']
   // has been updated 
   // use php's json_encode function to echo the result
}

var checkForUpdates = function() {
    $.ajax({
        type: 'POST',
        url: 'LinkToYourFile.php',
        dataType: 'JSON',
        data: {
            type: 'checkForUpdates',
            productId: 8484737
        },
        sucess: function ( result ) {
            if( result.updated === true ) {
               someElementPrice.textContent = result.newPrice;
            } 
        }
    });
};

window.setInterval(checkForUpdates, 3000); // Send's the update request every 3 seconds

Note - it's not easy to do this stuff if your not familiar with it. but you will learn. Thats the fun part. and there is a lot more to it behind the scenes, but this is the idea of it

于 2013-07-10T12:36:02.157 回答
1

有几种方法。

不涉及服务器端

您需要对另一个窗口的引用。例如,您可以通过打开窗口来获得它 - window.open()

然后使用 HTML DOM 方法,例如document.getElementById(...),然后是特定元素的 API:

https://developer.mozilla.org/en/docs/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

http://www.w3schools.com/jsref/dom_obj_table.asp

涉及服务器端

这意味着一个页面将向服务器发送一些数据,而另一个页面将读取它们。

您可以为此使用 AJAX 概念 - 请参阅答案XMLHttpRequest

另一个页面必须定期检查,或者第一页必须给它一些信号——调用你编写的一些 JavaScript 方法,或者重新加载窗口。其他选择是使用推送概念,但这是目前的一种先进技术。

于 2013-07-10T12:33:35.717 回答
0

This is possible with some limitations:

You should change the table that displays the products, to fetch the values beeing displayed using ajax. Then you have 2 possibilities: (I'm pretty sure there are more but I only think of those 2 at the moment)

  • create a timer which will fetch the values for a product every X seconds
  • create a listener that will be called when a value in the db gets updated

This way you can change the values in an backend and the will be automatically updated in your frontend view. You can have a look at the meteor framework where they use 'Live HTML' excessive.

于 2013-07-10T12:41:34.210 回答
0

可能的解决方案

使用 AJAX 调用,您可以将表本身存储在 HTML 文件中。

这是一个示例页面。如果您有任何问题,请不要犹豫,问他们。我添加了一些功能,例如Add Row

现场演示

管理员管理页面 (index.html)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery AJAX</title>
    <link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <style rel="stylesheet" type="text/css">
    tr, td,th{
        border-collapse: collapse;
        border:1px solid;
    }
    td{
        height:22px;
        min-width:125px;
    }
    </style>

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">"http://jquery.com"</script>
    <script type="text/javascript">
        /* ---------------------------------- */
        /* See the SCRIPT part of this answer */
        /* ---------------------------------- */
    </script>
</head>
<body>
    <h1>jQuery AJAX Table</h1>
    <section>
        <article>
            <div id="myTable" contenteditable></div>
            <nav data-type="table-tools">
                <ul>
                    <li>
                        <a href="#" id="addRow">New row</a>
                    </li>
                    <li>
                        <a href="#" id="addColumn">New column</a>
                    </li>
                    <li>
                        <a href="#" id="saveTable">Save table</a>
                    </li>
                </ul>
            </nav>
        </article>
    </section>
</body>
</html>

脚本

    /* On page load */
    $(document).ready(function () {
        var getTable = function () {
            /*We empty the div */
            $('#myTable').html('');
            /*We load the table */
            $.get('myTable.html', function (callback_data) {
                var table = callback_data;
                document.getElementById('myTable').innerHTML = table;
            });
        };
        getTable();

        /* ----- */

        /* New row button */
        $('#addRow').click(function (event) {
            /* Prevents the real anchor click event  (going to href link)*/
            event.preventDefault();

            /* We get the number of columns  in a row*/
            var colNumber = $($('#myTable tbody tr')[0]).children('td').length;

            var tr = document.createElement('tr');
            var td = "";
            for (var i = 0; i < colNumber; i++) {
                td = document.createElement('td');
                td.appendChild(document.createTextNode("\n"));
                tr.appendChild(td);
            }
            $('#myTable tbody').append(tr);
        });

        $('#addColumn').click(function (event) {
            event.preventDefault();
            $.each($('#myTable table thead tr'), function () {
                $(this).append('<th></th>');
            })
            $.each($('#myTable table tbody tr'), function () {
                $(this).append('<td></td>');
            });
        });

        $('#saveTable').click(function (event) {
            event.preventDefault();
            var table = $('#myTable').html();
            $.post('saveTable.php', {
                'myTable': table
            }, function (callback_data) {
                console.log(callback_data);
                $('#myTable').slideToggle('fast');
                setTimeout(function () {
                    getTable();
                    $('#myTable').slideToggle();
                }, 100);

            });
        });
    });

保存表.php

<?php
    if(!isset($_POST['myTable']))
        die('No data provided.');

    $table = $_POST['myTable'];

    $handle = fopen('myTable.html','w');
    $result = fwrite($handle,$table);
    if($result)
        fclose($handle);
    else
        die('Error writing file');
?>

myTable.html

<table>
    <thead>
        <tr>
            <th>Lorem ipsum dolor.</th>
            <th>Velit, vero, quis.</th>
            <th>Id, dolore, commodi!</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Voluptatibus, maiores.</td>
            <td>Quod, et.</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Ex, assumenda!</td>
            <td>Qui, pariatur!</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Alias, amet.</td>
            <td>Delectus, itaque!</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Praesentium, quod.</td>
            <td>Dolor, praesentium?</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Beatae, perferendis!</td>
            <td>Voluptates, earum!</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Ratione, quis.</td>
            <td>Cupiditate, repellendus.</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Porro, labore.</td>
            <td>Eligendi, nemo!</td>
        </tr>
        <tr>
            <td>Lorem ipsum.</td>
            <td>Soluta, suscipit.</td>
            <td>Dolorem, dolores.</td>
        </tr>
    </tbody>
</table>
于 2013-07-10T14:21:36.873 回答