【问题标题】:Moving code around [closed]移动代码[关闭]
【发布时间】:2011-07-29 23:35:34
【问题描述】:

我只是有点好奇这里是否有任何可以放置在 dataTables 脚本中的编码。我在每个模块的 javascript 页面上都有大部分相同的编码,所以我认为使用类似的编码可以将所有相同的编码放回该 dataTables 页面。

$.fn.dataTableExt.oApi.fnLengthChange = function ( oSettings, iDisplay ) {
oSettings._iDisplayLength = iDisplay;
oSettings.oApi._fnCalculateEnd( oSettings );

/* If we have space to show extra rows (backing up from the end point - then do so */
if ( oSettings._iDisplayEnd == oSettings.aiDisplay.length )
{
    oSettings._iDisplayStart = oSettings._iDisplayEnd - oSettings._iDisplayLength;
    if ( oSettings._iDisplayStart < 0 )
    {
        oSettings._iDisplayStart = 0;
    }
}

if ( oSettings._iDisplayLength == -1 )
{
    oSettings._iDisplayStart = 0;
}

oSettings.oApi._fnDraw( oSettings );

$('select', oSettings.oFeatures.l).val( iDisplay );
};

$(document).ready(function() {

var pageName = $('#pageName').val();    

var oTable = $('#templatesPageList').dataTable( {
    "sDom": 'rti<"pagination"p>',
    "iDisplayLength": 10,
    "sPaginationType": "full_numbers"
});

if(oTable.fnSettings().fnRecordsTotal() <= 10) {
    $('div.pagination').remove();
} else {
    $('div.pagination').append();
}
if(oTable.fnSettings().fnRecordsTotal() == 0) {
    $('.bt_red').remove();
    $('.bt_blue').remove();
}
if(oTable.fnSettings().fnRecordsTotal() <= 10) {
    $('.bt_blue').remove();
}

var info = $('.dataTables_info');
$('tfoot tr td.rounded-foot-left').append(info);

$('.edit').live('click', function(e) {
    e.preventDefault();
    var templateID = $(this).attr('id');
    if(!$('div.right_content').hasClass("loading")){
        $('div.right_content').addClass("loading").load('modules/forms/edit/templates.php?templateID=' + templateID, 
        function(){
            $(this).removeClass("loading");
        });
    }
});

$('a.bt_green').click(function(e) {
    e.preventDefault();
    $('div.right_content').load('modules/forms/addnew/' + $(this).attr('id'));
});

$('table tr').click(function() {

    checkBox = $(this).children('td').children('input[type=checkbox]');

    if(checkBox.attr('checked'))
        checkBox.removeAttr('checked');
    else
        checkBox.attr('checked', 'checked');

});

$('.ask').jConfirmAction( {
    question : "Are you sure you want to delete the selected row?", 
    yesAnswer : "Yes", 
    cancelAnswer : "No", 
    onYes: function(evt) { 
      templates(evt.target); 
    }
});

$('.ask2').jConfirmAction( {
    question : "Are you sure you want to delete all selected rows?",
    questionClass: "question2", 
    onYes: function(evt){
        templatesArray(evt.target); 
    }
});

$('.viewAll').live('click', function(e) {
    e.preventDefault();
    oTable.fnLengthChange(-1);
    $(this).removeClass('viewAll').addClass('paginateRecords');
    $(this).find('strong').html('View Paginated Records');
    $('.pagination').hide();
}); 

$('.paginateRecords').live('click', function(e) {
    e.preventDefault();
    oTable.fnLengthChange(10);
    $(this).removeClass('paginateRecords').addClass('viewAll');
    $(this).find('strong').html('View All Site Templates'); 
    $('.pagination').show();          
});

function templates(whatsThis) {
    var templateID = $(whatsThis).parents('td').find('img').attr('id');
    var dataString = 'templateID=' + templateID + '&deleteTemplate=True'; 

    var iRow = oTable.fnGetPosition( $(whatsThis).parents('tr').get(0));

    $.ajax({ 
        type: "POST", 
        url: "processes/templates.php", 
        data: dataString,
        success: function(data) {
            if (data.errorsExist) {
            } else {
                oTable.fnDeleteRow(iRow);     // remove the row from the table
                if(oTable.fnSettings().fnRecordsTotal() == 0) {
                    $('.bt_red').remove();
                    $('.bt_blue').remove();
                }
                if(oTable.fnSettings().fnRecordsTotal() <= 10) {
                    $('.bt_blue').remove();
                }
                if(oTable.fnSettings().fnRecordsTotal() <= 10) {
                    $('div.pagination').remove();
                }

            } 
        }
    });
}


function templatesArray(whatsThis) {
    var myNewArray = new Array(); 
    var aRow = new Array();

    $('input:checkbox[name="templates"]:checked').each(function(i) { 
        myNewArray.push($(this).val());
        aRow.push($(this).closest('tr')[0]);
    }); 
    var dataString = 'templatesArray=' + myNewArray + '&deleteTemplatesArray=True'; 
    $.ajax({ 
        type: "POST", 
        url: "processes/templates.php", 
        data: dataString,
        success: function(data) {
            if (data.errorsExist) {
            } else {
                $(whatsThis).parents("tr").eq(0).hide(); 
                for (i in aRow)  // loop over the array of row indexes
                  oTable.fnDeleteRow(aRow[i]);
                if(oTable.fnSettings().fnRecordsTotal() == 0) {
                    $('.bt_red').remove();
                    $('.bt_blue').remove();
                }
                if(oTable.fnSettings().fnRecordsTotal() <= 10) {
                    $('.bt_blue').remove();
                }
                if(oTable.fnSettings().fnRecordsTotal() <= 10) {
                    $('div.pagination').remove();
                }                      
            } 
        }
    });

}

});

【问题讨论】:

  • 是的,您可以移动代码...只需复制它,然后将其粘贴到另一个位置(如果这是您的要求):)
  • 杰夫,您可能想澄清一下您要完成的工作(问题已经有 3 票赞成)。你到底有什么问题?
  • 你怎么看它有 3 票接近但我想知道的是我上面的代码有没有我可以从那一页代码中取出并移动进入 jquery.dataTables.js 文件,因为其中大部分用于许多其他页面,因此如果我想聊天任何代码,我不必返回每个页面并在每个页面中更改它,我只需要在 dataTables.js 页面中更改它。

标签: jquery datatables


【解决方案1】:

是的,您可以将该文件中适用于多个页面的任何内容移出到加载到这些页面上的脚本文件中。

通常,如果我决定将什么放入哪个文件中,我会标记可以两者兼有的功能/块:

  1. 无需设置或绑定任何其他内容即可运行

  2. 需要或在多个页面上使用

将这些东西复制到另一个js文件中,在相关页面上导入,感觉好一点,你不需要维护一个12000行的javascript文件而不是几个较小的文件。

关于具体文件,我不知道 jquery.dataTables.js 中有什么,但我建议将您自己的任何代码移出到文件中,这些文件的名称可以反映它们的作用或至少在哪里使用它们。

不确定这是否是您的要求,但希望对您有所帮助。

【讨论】: