首页 > 其他分享 >DataTables ajax

DataTables ajax

时间:2023-11-06 18:48:17浏览次数:32  
标签:function DataTables object source ajax data

Description

DataTables can obtain the data that it is to display in the table body from a number of sources, including from an Ajax data source, using this initialisation parameter. As with other dynamic data sources, arrays or objects can be used for the data source for each row, with columns.data employed to read from specific object properties.

The ajax property has three different modes of operation, depending on how it is defined. These are:

  • string - Set the URL from where the data should be loaded from.
  • object - Define properties for jQuery.ajax.
  • function - Custom data get function

string

Description:

In its simplest form, ajax, when given as a string will simply load the data from the given remote file. Note that DataTables expects the table data to be an array of items in the data parameter of the object (use the ajax.dataSrc option of ajax as an object, if your data is formatted differently):

Javascript
1 2 3 4 5 6 7 {     "data": [         // row 1 data source,         // row 2 data source,         // etc     ] }

DataTables can use objects or arrays in almost any format as the data source for each row through use of the columns.data option. The default is to use an array data source.

Simple example:

Javascript
1 2 3 $('#example').dataTable( {   "ajax": "data.json" } );

 

object

Description:

As an object, the ajax object is passed to jQuery.ajax allowing fine control of the Ajax request. DataTables has a number of default parameters which you can override using this option. Please refer to the jQuery documentation for a full description of the options available, although the following parameters provide additional options in DataTables or require special consideration:

  • data (ajax.data) - As with jQuery, data can be provided as an object, but as an extension, it can also be provided as a function to manipulate the data DataTables sends to the server. The function takes a single parameter, an object where the parameters are name / value pairs that DataTables has readied for sending. An object may be returned which will be use used as the data to send to the server (therefore, if you wish to use the DataTables set parameters, you must merge them in your function), or you can add the items to the object that was passed in and not return anything from the function. This supersedes fnServerParams from DataTables 1.9-.
  • dataSrc (ajax.dataSrc) - By default DataTables will look for the property data (or aaData for compatibility with DataTables 1.9-) when obtaining data from an Ajax source or for server-side processing - this parameter allows that property to be changed, through two different forms:
    • As a string - define the property from the object to read. Note that if your Ajax source simply returns an array of data to display, rather than an object, set this parameter to be an empty string. Additionally you can use Javascript dotted object notation to get a data source for multiple levels of object / array nesting.
    • As a function - As a function it takes a single parameter, the JSON returned from the server, which can be manipulated as required. The returned value from the function is what will be used by DataTables as the data source for the table.
    • This supersedes sAjaxDataProp from DataTables 1.9-.
  • success - Must not be overridden as it is used internally in DataTables. To manipulate / transform the data returned by the server use ajax.dataSrc (above), or use ajax as a function (below).

Simple example:

Javascript
1 2 3 4 5 6 $('#example').dataTable( {   "ajax": {     "url": "data.json",     "type": "POST"   } } );

 

function ajax( data, callback, settings )

Description:

As a function, making the Ajax call is left up to yourself allowing complete control of the Ajax request. Indeed, if desired, a method other than Ajax could be used to obtain the required data, such as Web storage or a Firebase database.

When the data has been obtained from the data source, the second parameter (callback here) should be called with a single parameter passed in - the data to use to draw the table.

Simple example:

Javascript
1 2 3 4 5 6 7 $('#example').dataTable( {   "ajax": function (data, callback, settings) {     callback(       JSON.parse( localStorage.getItem('dataTablesData') )     );   } } );

Note that as of DataTables 1.10.6 the xhr event will be triggered when a function is used for ajax (even if there is no Ajax request). Prior to 1.10.6 no event would have been triggered.

Parameters:

 

 

 

 

 

 

 

 

 

 

 

 

   

标签:function,DataTables,object,source,ajax,data
From: https://www.cnblogs.com/chucklu/p/17813406.html

相关文章

  • DataTables Table plug-in for jQuery
    DataTablesTableplug-inforjQueryDataTablesisaplug-inforthejQueryJavascriptlibrary.Itisahighlyflexibletool,baseduponthefoundationsofprogressiveenhancement,andwilladdadvancedinteractioncontrolstoanyHTMLtable.Pagination,ins......
  • Ajax发送数据
    ajax发送json格式数据(contentType)"""前后端传输数据的时候一定要确保编码格式跟数据真正的格式是一致的不要骗人家!!!{"username":"jason","age":25} 在request.POST里面肯定找不到 django针对json格式的数据不会做任何的处理 request对象方法补充 request.is_a......
  • AJAX-事件循环(超详细过程)
    一.概念JS有一个基于事件循环的并发模型,事件循环负责执行代码、收集和处理事件以及执行队列中的子任务。定义:执行代码和收集异步任务的模型,在调用栈空闲,反复调用任务队列里回调函数的执行机制,就叫时间循环。原因:JS是単线程,为了让耗时的代码不阻塞其他代码运行,设计了事件循环模型二......
  • Ajax + java Servlet 制作Web进度条
     运行效果图  index2.jsp <%@pagelanguage="java"pageEncoding="UTF-8"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><scripttype="text/javascript"langua......
  • php: ajax请求,在返回信息前面出现ufeff红点,导致解析错误
    问题:ajax请求,在返回信息前面出现ufeff红点,导致解析错误原因:PHP文件格式编码位utf-8bom  文件编码是含BOM的会导致出现这种情况解决:  第一种方法:PHP文件格式编码转化为utf-8  第二种方法:在接口echo输出前面,添加一个ob_clean()函数           ......
  • 10、SpringMVC之处理Ajax请求
    创建名为spring_mvc_ajax的新module,过程参考9.1节和9.5节10.1、SpringMVC处理Ajax请求10.1.1、页面请求示例<inputtype="button"value="测试SpringMVC处理Ajax请求"onclick="testAjax()"><scripttype="text/javascript">functiontestA......
  • AJAX-解决回调函数地狱问题
    一、同步代码和异步代码1.同步代码浏览器是按照我们书写代码的顺序一行一行地执行程序的。浏览器会等待代码的解析和工作,在上一行完成之后才会执行下一行。这也使得它成为一个同步程序。总结来说:逐行执行,需原地等待结果后,才继续向下执行2.异步代码异步编程技术使你的程序可以在一个......
  • AJAX的引入之传统请求带来的问题------计算机网络经典
    AJAX在浏览器当中发送异步请求,请求之间是独立的,谁也不用等谁类似于多线程并发,不会出现页面清空然后展示新的页面的效果实现局部刷新(靠多种技术一起做到的,而不是一个技术叫ajax)它是异步的(类似于多线程)准备一个前端页面用来发送传统的请求方式<!DOCTYPEhtml><htmllang=......
  • 手搭手Ajax经典基础案例省市联动
    环境介绍技术栈springboot+mybatis-plus+mysql软件版本mysql8IDEAIntelliJIDEA2022.2.1JDK1.8SpringBoot2.7.13mybatis-plus3.5.3.2pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns......
  • AJAX学习(四)-(axios核心的原理)
    一、Promise1.定义Promise对象用于表示一个异步操作的最终完成(或失败)及其结果值我们用一张图来清晰的看Promise位于哪里2.好处1.逻辑更清晰2.了解axios函数内部运作机制3.能解决回调函数地狱问题3.使用语法及步骤示例代码如下:<!DOCTYPEhtml><htmllang="en"><head><metacha......