简体   繁体   English

数据表JS通过单列值获取行索引PHP

[英]Datatables JS get row index by single column value PHP

I'm working on a system of medical transportation. 我正在研究医疗运输系统。 Every day I have around 700 trips, so the table is pretty large. 每天我有700趟旅行,所以桌子很大。 I want the app, after the user adds a trip, the screen is positioned in the newly added trip. 我想要一个应用程序,在用户添加行程后,屏幕位于新添加的行程中。 (scroll) ,for the user to corroborate the trip successfully added (not my idea, my client wants it that way) . (滚动),为用户证实成功添加的旅程(不是我的主意,我的客户希望这样)。 For this, I am sending to the page by parameter the ID of the new trip added. 为此,我通过参数将添加的新行程的ID发送到页面。 I'm using a function "fnFindCellRowIndexes" that i found on datatables page, but i can't make it work. 我正在使用在数据表页面上找到的函数“ fnFindCellRowIndexes”,但无法使其正常工作。 The column i wait to search in is index 1. Here's my code: 我等待搜索的列是索引1。这是我的代码:

  <script type="text/javascript">
    $(document).ready(function() {

        var table = $('#tabla').DataTable({
            "deferRender": true,
            "language": {
                "url": "include/DataTables/Spanish.json"
            },
            "paging":   false,
            "ordering": false,
            "info":     false,
            "searching": true,
            "columnDefs": [
            {
                "targets": [ 0 ],
                "visible": false,
                "searchable": true
            },
            {
                "targets": [ 1 ],
                "visible": false,
                "searchable": false
            }
            ],
            "stateSave": true
        }); 

         var index = table.fnFindCellRowIndexes( '<?=$idNuevo?>', 1 );
         alert(index);

jQuery.fn.dataTableExt.oApi.fnFindCellRowIndexes = function ( oSettings, sSearch, iColumn )
        {
            var
                i,iLen, j, jLen, val,
                aOut = [], aData,
                columns = oSettings.aoColumns;

            for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
            {
                aData = oSettings.aoData[i]._aData;

                if ( iColumn === undefined )
                {
                    for ( j=0, jLen=columns.length ; j<jLen ; j++ )
                    {
                        val = this.fnGetData(i, j);

                        if ( val == sSearch )
                        {
                            aOut.push( i );
                        }
                    }
                }
                else if (this.fnGetData(i, iColumn) == sSearch )
                {
                    aOut.push( i );
                }
            }

            return aOut;
        };

</script>

I'm getting error "fnFindCellRowIndexes is not a function" Can anyone help me? 我收到错误消息“ fnFindCellRowIndexes不是函数”有人可以帮助我吗? if anyone has any idea to do it in a better way, suggestions are accepted. 如果有人有任何更好的想法可以接受建议。

  1. Declare fnFindCellRowIndexes before you instantiate your dataTable! 实例化数据fnFindCellRowIndexes 之前声明fnFindCellRowIndexes

  2. You must instantiate with dataTable() . 必须使用dataTable()实例化。 fnFindCellRowIndexes is an oldschool none-API plugin. fnFindCellRowIndexes是一个老式的非API插件。 As far as I can see in your code, you should not have problems with that. 据我在您的代码中看到的那样,您应该不会有任何问题。 If you still want to use the new API, you can always use table.api().<api-functions> 如果仍要使用新的API,则始终可以使用table.api().<api-functions>

  3. The PHP variable does not magically echo itself out (no offense :-) - you forget to actually print it : PHP变量不会神奇地回显自身(没有冒犯性:-)-您忘记实际打印它了:

var index = table.fnFindCellRowIndexes( '<? echo $idNuevo ?>', 1 ); var index = table.fnFindCellRowIndexes('<?echo $ idNuevo?>',1);

  1. Remember that fnFindCellRowIndexes is an equal / == search. 请记住, fnFindCellRowIndexes是一个等于/ ==的搜索。 If you search for 1 in column #1 it only return columns that hold the value 1 and nothing more, not rows holding 10 , 101 , 1.1 etc. 如果您搜索1列#1它只返回保存值列1持有,仅此而已,没有行101011.1等。

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

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