survos/simple-datatables-bundle

整合simple datatables,使用twig和stimulus

资助包维护!
kbond

安装: 765

依赖: 1

建议者: 1

安全: 0

星级: 4

关注者: 3

分支: 0

开放问题: 1

类型:symfony-bundle

1.5.340 2024-09-24 13:44 UTC

This package is auto-updated.

Last update: 2024-09-24 13:45:16 UTC


README

将来自https://github.com/fiduswriter/simple-datatables/的Simple Datatables库集成为一个stimulus组件。

composer req survos/simple-datatables-bundle

添加stimulus控制器

要将任何HTML表格转换为数据表,只需简单地将stimulus控制器添加到标签中

     <table class="table" {{ stimulus_controller('@survos/simple-datatables-bundle/table', {perPage: 5, sortable: true}) }}>

完整项目

复制粘贴即可创建一个新的带有动态、可搜索数据表的Symfony项目,无需编写任何JavaScript代码!也没有webpack或构建步骤。

symfony new simple-datatables-demo --webapp  && cd simple-datatables-demo
rm .git -rf
composer config extra.symfony.allow-contrib true
composer req survos/simple-datatables-bundle 

bin/console make:controller Simple -i
cat > templates/simple.html.twig <<END
{% extends 'base.html.twig' %}

{% block body %}
     <table class="table" {{ stimulus_controller('@survos/simple-datatables-bundle/table', {perPage: 5, sortable: true}) }}>
        <thead>
        <tr>
            <th>abbr</th>
            <th>name</th>
            <th>number</th>
        </thead>
        <tbody>
        {% for j in 1..12 %}
            <tr>
                <td>{{ j |date('2023-' ~ j ~ '-01') |date('M') }}</td>
                <td>{{ j |date('2023-' ~ j ~ '-01') |date('F') }}</td>
                <td>{{ j }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
END
symfony server:start -d
symfony open:local --path=/simple

甚至更简单,使用twig组件。为了简化示例,我们将添加fetch和json_decode函数到twig中,通常这会在控制器中完成,但这样复制粘贴更快。

composer require zenstruck/twig-service-bundle
cat > config/packages/zenstruck_twig_service.yaml <<END
zenstruck_twig_service:
  functions:
    fetch: file_get_contents # available as "fn_fetch()" in twig
    json_decode: json_decode
END

cat > templates/simple.html.twig <<'END'
{% extends 'base.html.twig' %}

{% block body %}
    {% set columns = [
        {name: 'id'},
        {name: 'title', title: 'name'},
        'brand',
        'price'
    ] %}
    <twig:simple_datatables
            perPage="20"
            :caller="_self"
            :columns="columns"
            :data="fn_json_decode(fn_fetch('https://dummyjson.com/products')).products"
    >
        <twig:block name="price">
            ${{ row.price|number_format(2) }}
        </twig:block>

    </twig:simple_datatables>
{% endblock %}
END
symfony server:start -d
symfony open:local --path=/simple