<p>That string that Craft stores is just JSON. When you try to save an array, Craft will automatically JSON-encode it, since MySQL doesn’t have an array-based data type.</p>

<p>To get that string to be automatically decoded back into an array for you, you need to give your field type a <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#defineContentAttribute-detail" rel="nofollow">defineContentAttribute()</a> method that returns <code>AttributeType::Mixed</code>:</p>

<pre class="lang-php prettyprint-override"><code>public function defineContentAttribute()
{
    return AttributeType::Mixed;
}
</code></pre>

<p>To get the values to actually output, you will need to make sure you’re passing the old value with the correct name. Your csvimport.html template is looping through a variable called <code>rows</code>, which is meant to represent the existing value, but in getInputHtml() you are sending $value to the template as a variable called <code>value</code>.</p>

<p>So if you change this line in your template:</p>

<pre><code>{% for rowId, row in rows %}
</code></pre>

<p>to:</p>

<pre><code>{% for rowId, row in value %}
</code></pre>

<p>the template will probably start outputting data.</p>

<p>If you’re not actually making any changes to the table HTML, you should probably just include the actual <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms/editableTable.html" rel="nofollow">_includes/forms/editableTable.html</a> template, though. Using your own HTML puts you at a little risk, if you also intend to keep using the Craft.EditableTable JavaScript class, since those two are very much tied together. We aren’t concerned about making breaking changes to the way the HTML is supposed to look, as long as we haven’t broken the way the actual editableTable.html template is supposed to be included.</p>

<p>So from csvimport.html, you should just replace your entire <code>&lt;table&gt;...&lt;/table&gt;</code> HTML with this:</p>

<pre class="lang-twig prettyprint-override"><code>{% from "_includes/forms" import editableTable %}

{{ editableTable({
    id: id,
    name: name,
    cols: cols,
    rows: value
}) }}
</code></pre>

<p>(In this case I’m using the editableTable() macro in _includes/forms.html instead of including the actual _includes/forms/editableTable.html template, mainly because I think using the macro looks nicer.)</p>

CC BY-SA 3.0
2014-10-31T08:40:24.393Z
Great, thank you Brandon! Bad blunder I made here...
But I still don't see how to solve my main problem. How do I get the Array values mapped to my table cols? Maybe I just lost the vision throughout my experiments of the last days but I just don't get it working.
LikeAJohny
CC BY-SA 3.0
2014-10-31T12:00:10.173Z
@JohN Hard to say without seeing your csvimport.html template. Can you update your question with that (or at least the parts of it that are relevant to this question)?
Brandon Kelly
CC BY-SA 3.0
2014-10-31T12:11:50.890Z
I more or less just copied the template from editableTable.html for testing issues, but sure I'll do. I'm pretty sure that it's just a matter of my thinking about the twig logic. I guess I'm too blind to wrap the value arrays for loop arround the columns one ^^
LikeAJohny
CC BY-SA 3.0
2014-10-31T12:28:26.657Z
@JohN Thanks - just updated the answer with more info for you.
Brandon Kelly
CC BY-SA 3.0
2014-10-31T12:40:39.780Z
Thank you very much. That was exactly the type of brain error I expected... So I hope I didn't steal too much of your time. Btw I just included the whole template for testing issues as I said above. I will ofc replace it if I don't have to do any changes to the actual template. Thanks again an keep up the great great work you're doing :) Greetz
LikeAJohny
CC BY-SA 3.0
2014-10-30T20:54:46.633Z
2014-10-31T12:28:07.710Z
2014-10-31T12:28:07.710Z
Brandon Kelly
<p>I'm still trying to process my csv data as mentioned in a former question. But I'm struggling with another part of this right now.</p>

<p>I decided to implement a custom field type which should display the data from my csv in a table to make it further editable for editors. The problem is that I don't even get something shown in this table even without processing a csv file before.</p>

<p>I tried something really simple, at least I thought it was...</p>

<pre><code>public function getInputHtml($name, $value)
{
    $id = 'test';
    $cols = array(
        'Sample' =&gt; array(
            'heading' =&gt; 'Heading',
            'type'    =&gt; 'singleline'
        )
    );

    return craft()-&gt;templates-&gt;render('csvImport/csvimport', array(
        'name'  =&gt; $name,
        'value' =&gt; $value,
        'id'    =&gt; $id,
        'cols'  =&gt; $cols,
    ));
}
</code></pre>

<p>This works until the point of saving the data. Craft stores everything into database as a string. No matter how many cols I use, it will always result in a string which looks like this:</p>

<pre><code>"{"1":"Sample":"The entered string"}}"
</code></pre>

<p>This is what craft stores in the database, so at least the data is processed. But how the pencil do I get this stuff to show up in the table of my template?</p>

<p>If you have any other idea how I could solve my problem, please let me know!</p>

<p>UPDATE:</p>

<pre><code>&lt;table id="{{ id }}" class="shadow-box editable"&gt;
&lt;thead&gt;
    &lt;tr&gt;
        {% for col in cols %}
            &lt;th scope="col" class="header"&gt;{{ col.heading ? col.heading : ' ' }}&lt;/th&gt;
        {% endfor %}
        {% if not static %}
            &lt;th class="header" colspan="2"&gt;&lt;/th&gt;
        {% endif %}
    &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
    {% for rowId, row in rows %}
        &lt;tr data-id="{{ rowId }}"&gt;
            {% for colId, col in cols %}
                {% set cellName = name~'['~rowId~']['~colId~']' %}
                {% set value = (row[colId] is defined ? row[colId] : null) %}
                {% set textual = (col.type in ['singleline','multiline','number']) %}
                &lt;td class="{% if textual %}textual{% endif %} {% if col.class is defined %}{{ col.class }}{% endif %}"{% if col.width is defined %} width="{{ col.width }}"{% endif %}&gt;
                    {%- if col.type == 'select' -%}
                        {% include "_includes/forms/select" with {
                            class:    'small',
                            name:     cellName,
                            options:  col.options,
                            value:    value,
                            disabled: static
                        } only %}
                    {%- elseif col.type == 'file' -%}
                        {% include "_includes/forms/file" with {
                            class:    'small',
                            name:     cellName,
                            value:    value
                        } only %}
                    {%- elseif col.type == 'checkbox' -%}
                        &lt;input type="hidden" name="{{ cellName }}"&gt;
                        &lt;input type="checkbox" name="{{ cellName }}" value="1"{% if value %} checked{% endif %}{% if static %} disabled{% endif %}&gt;
                    {%- else -%}
                        &lt;textarea name="{{ cellName }}" rows="1"{% if static %} disabled{% endif %}&gt;{{ dump(value) }}&lt;/textarea&gt;
                    {%- endif -%}
                &lt;/td&gt;
            {% endfor %}
            {% if not static %}
                &lt;td class="thin action"&gt;&lt;a class="move icon" title="{{ 'Reorder'|t }}"&gt;&lt;/a&gt;&lt;/td&gt;
                &lt;td class="thin action"&gt;&lt;a class="delete icon" title="{{ 'Delete'|t }}"&gt;&lt;/a&gt;&lt;/td&gt;
            {% endif %}
        &lt;/tr&gt;
    {% endfor %}
&lt;/tbody&gt;
</code></pre>

<p></p>

<p>I guess this is the relevant part for the ouput of my values.</p>

CC BY-SA 3.0
2015-10-27T13:50:39.240Z
How to set anchor link for delete button to a certain row?
Volatil3
<templating><custom-fieldtype><custom-field><table-field>
Why won't the values of an editableTableField show up in my template?
# Why won't the values of an editableTableField show up in my template?

(Asked by: username_0 on 2014-10-30)

I'm still trying to process my csv data as mentioned in a former question. But I'm struggling with another part of this right now.
I decided to implement a custom field type which should display the data from my csv in a table to make it further editable for editors. The problem is that I don't even get something shown in this table even without processing a csv file before.
I tried something really simple, at least I thought it was...
public function getInputHtml($name, $value)
{
    $id = 'test';
    $cols = array(
        'Sample' => array(
            'heading' => 'Heading',
            'type'    => 'singleline'
        )
    );

    return craft()->templates->render('csvImport/csvimport', array(
        'name'  => $name,
        'value' => $value,
        'id'    => $id,
        'cols'  => $cols,
    ));
}

This works until the point of saving the data. Craft stores everything into database as a string. No matter how many cols I use, it will always result in a string which looks like this:
"{"1":"Sample":"The entered string"}}"

This is what craft stores in the database, so at least the data is processed. But how the pencil do I get this stuff to show up in the table of my template?
If you have any other idea how I could solve my problem, please let me know!
UPDATE:


    
        {% for col in cols %}
            {{ col.heading ? col.heading : ' ' }}
        {% endfor %}
        {% if not static %}
            
        {% endif %}
    


    {% for rowId, row in rows %}
        
            {% for colId, col in cols %}
                {% set cellName = name~'['~rowId~']['~colId~']' %}
                {% set value = (row[colId] is defined ? row[colId] : null) %}
                {% set textual = (col.type in ['singleline','multiline','number']) %}
                
                    {%- if col.type == 'select' -%}
                        {% include "_includes/forms/select" with {
                            class:    'small',
                            name:     cellName,
                            options:  col.options,
                            value:    value,
                            disabled: static
                        } only %}
                    {%- elseif col.type == 'file' -%}
                        {% include "_includes/forms/file" with {
                            class:    'small',
                            name:     cellName,
                            value:    value
                        } only %}
                    {%- elseif col.type == 'checkbox' -%}
                        
                        
                    {%- else -%}
                        {{ dump(value) }}
                    {%- endif -%}
                
            {% endfor %}
            {% if not static %}
                
                
            {% endif %}
        
    {% endfor %}



I guess this is the relevant part for the ouput of my values.

**Tags:** templating, custom-fieldtype, custom-field, table-field

> **Comments:**
> **username_1**: How to set anchor link for delete button to a certain row?  

---

## 1 answers

### Answer by username_2 on 2014-10-30. Score: 3 | Accepted answer

That string that Craft stores is just JSON. When you try to save an array, Craft will automatically JSON-encode it, since MySQL doesn’t have an array-based data type.
To get that string to be automatically decoded back into an array for you, you need to give your field type a defineContentAttribute() method that returns AttributeType::Mixed:
public function defineContentAttribute()
{
    return AttributeType::Mixed;
}

To get the values to actually output, you will need to make sure you’re passing the old value with the correct name. Your csvimport.html template is looping through a variable called rows, which is meant to represent the existing value, but in getInputHtml() you are sending $value to the template as a variable called value.
So if you change this line in your template:
{% for rowId, row in rows %}

to:
{% for rowId, row in value %}

the template will probably start outputting data.
If you’re not actually making any changes to the table HTML, you should probably just include the actual _includes/forms/editableTable.html template, though. Using your own HTML puts you at a little risk, if you also intend to keep using the Craft.EditableTable JavaScript class, since those two are very much tied together. We aren’t concerned about making breaking changes to the way the HTML is supposed to look, as long as we haven’t broken the way the actual editableTable.html template is supposed to be included.
So from csvimport.html, you should just replace your entire ... HTML with this:
{% from "_includes/forms" import editableTable %}

{{ editableTable({
    id: id,
    name: name,
    cols: cols,
    rows: value
}) }}

(In this case I’m using the editableTable() macro in _includes/forms.html instead of including the actual _includes/forms/editableTable.html template, mainly because I think using the macro looks nicer.)

> **Comments:**
> **username_0**: Great, thank you Brandon! Bad blunder I made here... But I still don't see how to solve my main problem. How do I get the Array values mapped to my table cols? Maybe I just lost the vision throughout my experiments of the last days but I just don't get it working.  
> **username_2**: @JohN Hard to say without seeing your csvimport.html template. Can you update your question with that (or at least the parts of it that are relevant to this question)?  
> **username_0**: I more or less just copied the template from editableTable.html for testing issues, but sure I'll do. I'm pretty sure that it's just a matter of my thinking about the twig logic. I guess I'm too blind to wrap the value arrays for loop arround the columns one ^^  
> **username_2**: @JohN Thanks - just updated the answer with more info for you.  
> **username_0**: Thank you very much. That was exactly the type of brain error I expected... So I hope I didn't steal too much of your time. Btw I just included the whole template for testing issues as I said above. I will ofc replace it if I don't have to do any changes to the actual template. Thanks again an keep up the great great work you're doing :) Greetz  

---

