【问题标题】:Adding a Live Query to an OnDemandGrid-style dGrid Using a JsonRest Store?使用 JsonRest 存储向 OnDemandGrid 样式的 dGrid 添加实时查询?
【发布时间】:2026-01-08 00:40:01
【问题描述】:

当 dGrid 由 JsonRest 存储支持时,如何实现 dijit/TextBox 过滤 OnDemandGrid 样式 dGrid 中的数据?我想在框中搜索并在我输入时更新网格。

我在 dGrid 文档中找不到任何示例,虽然这看起来只是问题 - Is it possible to filter data in a dgrid like you can in a datagrid? If so, how? - 它使用 MemoryStore 并将其换成 JsonRest 存储不起作用。

我需要查询商店然后刷新网格吗?我需要 Observable 吗? dojo.store.util.SimpleQueryEngine 呢?这是答案的一部分吗?

大概服务器上也必须进行一些更改才能响应查询。

【问题讨论】:

    标签: javascript dojo dgrid


    【解决方案1】:

    事实证明这很容易。您只需在网格上设置查询属性并调用 refresh()。

    然后我必须对我的服务器端代码进行简单的更改以处理 ?search=whatever 查询字符串。

    这是我的代码:

    // assuming we have a declarative dijit/TextBox and a reference to our grid in myGrid                                           
    // wait for DOM before wiring up our textbox (when dijit parsed)
    ready( function() 
    {
        var timeoutId = null,
            searchTextBox = registry.byId( 'searchTextBox' );
    
        searchTextBox.watch( 'value', function( name, oldValue, newValue ) 
        {
            if( newValue.length == 1 )
            {
                return;
            }   
    
            if( timeoutId ) 
            {
                clearTimeout( timeoutId );
                timeoutId = null;
            };
    
            timeoutId = setTimeout( function() 
            {
                myGrid.query = { search: newValue };
                myGrid.refresh();
            }, 300 );
        } );
    } );
    

    【讨论】: