【问题标题】:Auto load search results when page loads页面加载时自动加载搜索结果
【发布时间】:2014-05-15 17:02:06
【问题描述】:

我正在使用我想修改的 joomla 模块以自动加载默认结果列表。

目前,当页面加载时,不会显示任何结果。如果所有搜索字段都为空并且用户单击搜索按钮,则页面将加载所有数据。如果信息放在搜索字段中,结果将被分解以匹配输入的内容。

我希望页面在页面加载时自动加载所有数据,而无需用户单击搜索。 我如何做到这一点?

我相信该模块使用 ajax,并且我相信影响这一点的信息如下:

<?php 

    header('Access-Control-Allow-Origin: *');
    header('Content-Type: text/html');

    define('_JEXEC', 1);
    define('DS', DIRECTORY_SEPARATOR);

    ini_set("display_errors", "On");
    error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING);

    $my_path = dirname(__FILE__);
    $my_path = explode(DS.'modules',$my_path);  
    $my_path = $my_path[0];         

    if (file_exists($my_path . '/defines.php')) {
        include_once $my_path . '/defines.php';
    }

    if (!defined('_JDEFINES')) {
        define('JPATH_BASE', $my_path);
        require_once JPATH_BASE.'/includes/defines.php';
    }

    require_once JPATH_BASE.'/includes/framework.php';
    $app = JFactory::getApplication('site');
    $app->initialise();

    ///////////////////////////////////////////////////////////////////////////////////////////////

    $name = $_GET['name'];
    $value = mb_strtolower($_GET['value']);
    $next = mb_strtolower($_GET['next']);

    $db = JFactory::getDBO();
    $query = "SELECT * FROM #__k2_extra_fields WHERE published = 1";

    $db->setQuery($query);
    $results = $db->loadObjectList();

    $extra_val = '';
    $extra_id = 0;
    foreach($results as $result) {
        if(trim(mb_strtolower($result->name)) == trim($value) . " " . trim($next) || trim(mb_strtolower($result->name)) == trim($next) . " " . trim($value)) {
            $extra_val = $result->value;
            $extra_id = $result->id;
            break;
        }
    }

    require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'JSON.php');
    $json = new Services_JSON;
    $extra_val = $json->decode($extra_val);

    if($extra_val != '') {
        foreach($extra_val as $val) {
            echo "<option>" . $val->name . "</option>";
        }
        echo "<option>".$extra_id."</option>";
    }

?>

请帮忙!

【问题讨论】:

    标签: javascript jquery ajax joomla3.0 joomla-k2


    【解决方案1】:

    要自动加载搜索结果,我们必须将搜索查询存储在会话变量中,

    http://docs.joomla.org/How_to_use_user_state_variables

    http://docs.joomla.org/API15:JApplication/getUserStateFromRequest

    这些链接将很好地描述如何在会话中管理请求变量,因此请求中没有变量它将从会话中获取值。

    【讨论】:

      【解决方案2】:

      尝试使用类似的东西

      <html>
      <head>
      <script>
      function myFunction()
      {
      alert("Page is loaded");
      }
      </script>
      </head>
      
      <body onload="myFunction()">
      <h1>Hello World!</h1>
      </body>
      
      </html>
      

      然后您可以轻松更改 myFunction 以触发点击事件的搜索

      <script>
      function myFunction()
      {
         document.getElementById('YOUR-BUTTON-ID').onclick();
      }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        相关资源
        最近更新 更多