我想使用jquery将会话值存储在appTables的搜索框中

I want to store the session value in search box of appTables using jquery

提问人:tejaswini bnvl 提问时间:8/1/2023 最后编辑:CBroetejaswini bnvl 更新时间:8/2/2023 访问量:33

问:

我想使用jquery将会话值存储在appTables的搜索框中

<?php $session = \Config\Services::session(); ?>

<script type="text/javascript">
    $(document).ready(function () {
        var searchVals = "<?php echo $session->get('search_by_val'); ?>"; 
        $("#lead-table").appTable({
            source: '<?php echo_uri("leads/list_data") ?>',
            serverSide: true,
            bStateSave: true,
            additionalData: { search_value: searchVals }
         });
     });
</script>

在此 searchVals 中,变量应传递到 additionalData 方法中,当它重新加载时,它应该按照搜索参数工作。

PHP HTML JQUERY CSS, AJAX

评论

0赞 RiggsFolly 8/1/2023
我想要不是一个问题。它只是告诉我们,您希望我们为您完成繁重的工作。你被困在哪里了?你研究了什么?你试过什么?需要明确的是,我们会在 stackoverflow 为您提供帮助,但我们不是免费的 do-my-thinking 服务 查看如何提问和 最小、完整和可验证的例子
0赞 Barmar 8/1/2023
你写的代码有什么问题?除非会话值包含双引号,否则它看起来应该可以工作。为了防止这种情况,请使用var searchVals = <?php echo json_encode($session->get('search_by_val'); ?>;

答:

0赞 AdityaDees 8/1/2023 #1

您需要为搜索框输入添加事件处理程序,以便每当用户键入或更改搜索值时,它都会更新会话变量并重新加载 appTable。

<?php $session = \Config\Services::session(); ?>

<script type="text/javascript">
    $(document).ready(function () {
        // Function to reload appTable with the updated search value
        function reloadTable() {
            var searchVals = "<?php echo $session->get('search_by_val'); ?>";
            $("#lead-table").appTable({
                source: '<?php echo_uri("leads/list_data") ?>',
                serverSide: true,
                bStateSave: true,
                additionalData: { search_value: searchVals }
            });
        }

        // Get the initial search value from the session and reload the table
        reloadTable();

        // Add an event handler for the search box
        $("#search-box").on('input', function () {
            var searchValue = $(this).val();
            // Update the session variable with the new search value using AJAX
            $.ajax({
                url: '<?php echo_uri("your/update/session/endpoint") ?>',
                method: 'POST',
                data: { search_by_val: searchValue },
                success: function () {
                    // Reload the appTable with the updated search value
                    reloadTable();
                }
            });
        });
    });
</script>

评论

0赞 tejaswini bnvl 8/2/2023
我试过了,但它没有加载带有搜索标签的数据表