简体   繁体   English

在Concrete5中,如何更新块编辑视图以显示数据库中保存的当前值?

[英]In Concrete5 how do I update my block edit view to show the current values saved in the database?

I have code that dynamically creates a product features list and puts all the values together into one string seperated by newline characters and gets saved to the database. 我的代码可以动态创建产品功能列表,并将所有值放到一个由换行符分隔的字符串中,并保存到数据库中。 When this string is passed to the view I output as an unordered list. 当此字符串传递给视图时,我将输出为无序列表。 When I go to edit the block it does not show what is already in the list, so I have to retype all the features everytime I need to edit my block, even if it's not the feature list (I have some titles and other textareas). 当我去编辑块时,它不会显示列表中已经存在的内容,因此,即使不是功能列表,我也必须每次都需要重新输入所有功能,即使它不是功能列表(我有一些标题和其他文本区域) 。 If I put the php variable into my edit page it will show me what has been saved but I would need it to be instantly added to the list and not showing separate. 如果我将php变量放入编辑页面,它将显示已保存的内容,但我需要将其立即添加到列表中,而不要单独显示。 I basically need it to "auto appendTo" my <ul> . 我基本上需要它来“自动附加到”我的<ul> Here is my code... 这是我的代码...

controller function to save the list in controller.php 控制器功能将列表保存在controller.php中

public function save($args) {
        $args['features'] = implode("\n", $args['features']);//combine all feature items into one string, separated by "newline" characters
        parent::save($args);
    }

my edit.php - this will show it in a list. 我的edit.php-这将显示在列表中。 If I just put echo $features it outputs the string. 如果我只是把echo $features放进去,它将输出字符串。

echo '<div class="ccm-block-field-group">';
echo '<h2>' . t('Features') . '</h2>';
echo '<input type="text" id="inputList" />';
echo '<button type="button" id="addList">Add</button>';
echo $features = explode("\n", $features);
        foreach ($features as $feature) {
            echo '<li class="currentFeatures">' . $feature . '</li>';
        };

echo '<ul class="featureList">';
echo '</ul>';
echo '</div>';

my auto.js - handles creation of the list 我的auto.js-处理列表的创建

var listItemCounter = 0;
    $("#addList").click(function() {
        listItemCounter++;
        var text = $("#inputList").val(); //assign a unique id number to this button, so it knows which hidden field to remove when clicked
        var buttonDataId = text + '<button data-id="' + listItemCounter + '">x</button>';
        if(text.length){
            $('<li />', {html: buttonDataId}).appendTo('ul.featureList');
            $('<input type="hidden" name="features[]" value="' + text + '" data-id="' + listItemCounter + '" />').insertAfter('ul.featureList');
            };
    });
    $('ul').on('click','button', function(el){
        $('input[data-id="' + $(this).attr('data-id') + '"]').remove();//remove the hidden field so it does not get POSTed when user saves
        $(this).parent().remove()
    });

and finally part of my db.xml 最后是我的db.xml的一部分

<field name="features" type="X2"></field>

You'll want to do something similar to what I explained for the "view" in your other question ( https://stackoverflow.com/a/14968046/477513 ). 您将要执行与我在其他问题( https://stackoverflow.com/a/14968046/477513 )中对“视图”所做的解释类似的操作。

You want to change the list from a string to an array (via explode("\\n", $features) ), then run your javascript on each of those features. 您想要将列表从字符串更改为数组(通过explode("\\n", $features) ),然后在每个功能上运行javascript。 Since this is basically the same javascript that you'd do when someone adds a new item via the textbox, you will want to move all that code into a function and call the function in both situations (otherwise you will be copying and pasting the same code, which violates the "Don't Repeat Yourself" rule and inevitably leads to bugs in the future). 由于这基本上与您通过文本框添加新项目时使用的JavaScript相同,因此您将需要将所有代码移动到函数中并在两种情况下都调用该函数(否则,您将复制并粘贴相同的代码代码,该代码违反了“请勿重复自己”规则,并不可避免地导致将来出现错误。 So your auto.js code could look something like this: 因此,您的auto.js代码可能如下所示:

var listItemCounter = 0;

function addItem(text) {
    if (text.length) {
        listItemCounter++;
        var itemHtml = '<li data-id="' + listItemCounter + '">'
                     + text
                     + '<button data-id="' + listItemCounter + '">x</button>'
                     + '</li>';
        var inputHtml = '<input type="hidden" name="features[]" value="' + text + '" data-id="' + listItemCounter + '" />';
        $('ul.featureList').append(itemHtml);
        $('ul.featureList').after(inputHtml);
    }
}

function removeItem(id) {
    $('input[data-id="' + id + '"]').remove();
    $('li[data-id="' + id + '"]').remove();
}

$("#addList").click(function() {
    additem($("#inputList").val());
});

$('ul').on('click','button', function(el) {
    var id = $(this).attr('data-id');
    removeItem(id);
});

Then in your edit.php file, add this code to output the saved data and call the addItem function on each one: 然后在您的edit.php文件中,添加以下代码以输出保存的数据,并在每个代码上调用addItem函数:

<?php
//populate existing data when edit dialog is first opened
$featuresPHPArray = explode("\n", $features);
$featuresJSArray = Loader::helper('json')->encode($featuresPHPArray);
?>
<script>
var savedItems = <?php echo $featuresJSArray; ?>;
$.each(savedItems, function() {
    addItem(this);
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM