简体   繁体   English

Kendo UI模板刷新

[英]Kendo UI Template refresh

I set up a hierarchical grid with Kendo UI, and I need a way to sort by names. 我使用Kendo UI设置了一个层次结构网格,我需要一种按名称排序的方法。

I have buttons of the alphabet set up to filter names where the first letter equals the letter of the button you clicked, but it won't filter it correctly. 我设置了字母按钮来过滤名称,其中第一个字母等于您单击的按钮的字母,但是它不能正确过滤它。

It loads fine, with all the data available. 它可以很好地加载所有可用数据。 When I click a letter, nothing changes in the first row you expand, and the filter starts working on the rows you expand after the first one, even though the variable gets set. 当我单击一个字母时,展开的第一行没有任何变化,即使变量已设置,过滤器也将在展开的第一行开始处理。 See image at the bottom for an illustration of the problem. 请参阅底部的图像以获取问题的说明。

The template 模板

<script type="text/x-kendo-template" id="client-report-detail-template">
    # var active, inactive; $.each(data.clients, function(key, client) { #

        #if(client.status == 20) { active = 1; } else { active = 0; } #
        #if(client.status != 20) { inactive = 1; } else { inactive = 0 } #

        #if(sortBy.length > 0 && sortBy == client.administration_name[0]) { #
            <div class="client-report-detail-container">
                <div class="client-report-detail-pane">#= client.administration_name #</p></div>
                <div class="client-report-detail-pane">#= client.is_new #</p></div>
                <div class="client-report-detail-pane">#= active #</p></div>
                <div class="client-report-detail-pane">#= inactive #</p></div>
            </div>
        # } #

        #if(sortBy.length == 0) { #
            <div class="client-report-detail-container">
                <div class="client-report-detail-pane">#= client.administration_name #</p></div>
                <div class="client-report-detail-pane">#= client.is_new #</p></div>
                <div class="client-report-detail-pane">#= active #</p></div>
                <div class="client-report-detail-pane">#= inactive #</p></div>
            </div>
        # } #
    # }); #
</script>

The buttons (using Laravel's Blade) 按钮(使用Laravel的剑)

@foreach(range('A', 'Z') as $letter)
    <button data-type="name" data-value="{{ $letter }}" class="k-button bizme-button-round filter-text fcl">{{ $letter }}</button>
@endforeach

What I got so far that happens when you click on one of the buttons 当您单击其中一个按钮时,我到目前为止所发生的一切

$(".fcl").on('click tap', function() {
    sortByLetter = $(this).data().value;

    $("#client-report-grid-container").data("kendoGrid").refresh();
});

The grid setup and detail init 网格设置和详细信息初始化

$("#client-report-grid-container").kendoGrid({
    dataSource: {
        data: gridData,
        pageSize: 15
    },
    detailTemplate: kendo.template($("#client-report-detail-template").html()),
    detailInit: detailInit,
    columns: [
        {
            field: "date",
            title: "{{ trans('form.month') }}"
        },
        {
            field: "new"
        },
        {
            field: "active"
        },
        {
            field: "inactive"
        }
    ]
});

function detailInit(){
    $.each(this.dataSource.data(), function(key, data) {
        data.sortBy = sortByLetter;
    });
};

Anyone know how to solve this issue? 有人知道如何解决这个问题吗?

不要判断我的绘画技能:)

What happens is that the detailTemplate runs before detailInit , the detail is rendered before the event. 发生的事情是detailTemplatedetailInit之前detailInit ,细节在事件之前呈现。 So in the template the sortBy is not set yet. 因此,尚未在模板中设置sortBy Then the event runs and sets it. 然后事件运行并进行设置。 That is why it works in the second time. 这就是为什么它第二次起作用。 To check that, add a console.log("detailInit") in the first row inside the detailInit event and a # console.log("template"); # 要进行检查,请在detailInit事件内的第一行中添加console.log("detailInit")# console.log("template"); # detailInit # console.log("template"); # inside the template and check the console. # console.log("template"); #在模板中检查控制台。

My suggestion to fix that is to set the sort values inside the grid's dataBound event instead of the detailInit event: 我的建议,以解决这个问题是设置网格内的排序值dataBound的事件,而不是detailInit事件:

dataBound: function()
{
    $.each(this.dataSource.data(), function(key, data) {
        data.sortBy = sortByLetter;
    });
}

Just like that. 就这样 dataBound is called anytime grid's refresh() method runs, so the sortBy values are set before the detail is rendered, thus the template can read the right sortBy values. dataBound运行grid的refresh()方法时都会sortBy ,因此在呈现明细之前设置sortBy值,因此模板可以读取正确的sortBy值。

Working demo . 工作演示

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

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